Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active September 30, 2025 05:07
Show Gist options
  • Save oguz-ismail/97c10dd1b87074512b67e1e67cd8c944 to your computer and use it in GitHub Desktop.
Save oguz-ismail/97c10dd1b87074512b67e1e67cd8c944 to your computer and use it in GitHub Desktop.
.globl _start
.section .rodata
0: .asciz "/dev/null"
.bss
.skip 23
1: .skip 1
.text
_start:
mov w19, 0xca00
movk w19, 0x3b9a, lsl 16 /* ns in a sec */
mov w20, 0x4240
movk w20, 0xf, lsl 16 /* us in a sec */
mov x21, 0xf7cf
movk x21, 0xe353, lsl 16
movk x21, 0x9ba5, lsl 32
movk x21, 0x20c4, lsl 48
mov x22, 0xcccccccccccccccc
movk x22, 0xcccd
mov x23, 10 /* also '\n' */
adr x24, 1b
mov w25, '\t'
ldr x0, [sp]
add x1, sp, 16 /* argv+1 */
add x2, x1, x0, lsl 3 /* envp */
stp x1, x2, [sp]
mov w8, 24 /* dup3 */
mov w0, 1
mov w1, 3
mov w2, 0x80000 /* O_CLOEXEC */
svc 0
mov w8, 56 /* openat */
mov w0, wzr
adr x1, 0b
mov w2, 1 /* O_WRONLY */
movk w2, 8, lsl 16 /* O_CLOEXEC */
svc 0
mov w8, 24
mov w1, 1
mov w2, wzr
svc 0
parent:
mov w8, 62 /* lseek */
mov w0, wzr
mov w1, wzr
mov w2, wzr /* SEEK_SET */
svc 0
mov w8, 113 /* clock_gettime */
mov w0, 1 /* CLOCK_MONOTONIC */
sub x1, sp, 16
svc 0
mov w8, 220 /* clone */
mov w0, 0x4111 /* CLONE_VM|CLONE_VFORK|SIGCHLD */
mov w1, wzr
mov w2, wzr
mov w3, wzr
svc 0
cbz w0, child
cmn w0, 1
b.eq 1f
mov w8, 260 /* wait4 */
mov w0, -1
mov x1, xzr
mov w2, wzr
sub x3, sp, 160
svc 0
mov w8, 113
mov w0, 1
sub x1, sp, 32
svc 0
ldp q1, q0, [sp, -32]
sub v0.2d, v1.2d, v0.2d
fmov x1, d0
mov x2, v0.d[1]
madd x2, x1, x19, x2
umulh x2, x2, x21
asr x1, x2, 7
add x0, x1, x2, lsr 63
strb w25, [x24]
bl print
ldp x1, x2, [sp, -160] /* ru_utime */
madd x0, x1, x20, x2
strb w25, [x24]
bl print
ldp x1, x2, [sp, -144] /* ru_stime */
madd x0, x1, x20, x2
strb w23, [x24]
bl print
b parent
child:
mov w8, 221 /* execve */
ldp x1, x2, [sp]
ldr x0, [x1]
svc 0
1: mov w8, 93 /* _exit */
mov w0, 1
svc 0
print:
mov x1, x24
mov w2, 1
0: umulh x3, x0, x22
lsr x3, x3, 3
msub w4, w3, w23, w0
orr w4, w4, '0'
strb w4, [x1, -1]!
add w2, w2, 1
cmp x0, 9
mov x0, x3
b.hi 0b
mov w8, 64 /* write */
mov w0, 3
svc 0
ret
.globl _start
.section .rodata
0: .asciz "/dev/null"
.bss
.skip 23
1: .skip 1
.text
_start:
mov (%rsp), %rax
lea 16(%rsp), %rbx /* argv+1 */
lea (%rbx, %rax, 8), %rbp /* envp */
sub $160, %rsp
mov $292, %eax /* dup3 */
mov $1, %edi
mov $3, %esi
mov $0x80000, %edx /* O_CLOEXEC */
syscall
mov $2, %eax /* open */
mov $0b, %rdi
mov $0x80001, %esi /* O_WRONLY|O_CLOEXEC */
syscall
mov %eax, %edi
mov $292, %eax
mov $1, %esi
xor %edx, %edx
syscall
parent:
mov $8, %eax /* lseek */
xor %edi, %edi
xor %esi, %esi
xor %edx, %edx
syscall
mov $228, %eax /* clock_gettime */
mov $1, %edi /* CLOCK_MONOTONIC */
mov %rsp, %rsi
syscall
mov $58, %eax /* vfork */
syscall
test %eax, %eax
je child
cmp $-1, %eax
je 2f
mov $61, %eax /* wait4 */
mov $-1, %edi
xor %rsi, %rsi
xor %edx, %edx
xor %ecx, %ecx
lea 32(%rsp), %r10
syscall
mov $228, %eax
mov $1, %edi
lea 16(%rsp), %rsi
syscall
imul $1000000000, (%rsp), %rdx
add 8(%rsp), %rdx
mov $1000, %ecx
imul $1000000000, 16(%rsp), %rax
add 24(%rsp), %rax
sub %rdx, %rax
cqto
idiv %rcx
mov %rax, %rdi
movb $'\t', 1b
call print
imul $1000000, 32(%rsp), %rdi /* ru_utime */
add 40(%rsp), %rdi
movb $'\t', 1b
call print
imul $1000000, 48(%rsp), %rdi /* ru_stime */
add 56(%rsp), %rdi
movb $'\n', 1b
call print
jmp parent
child:
mov $59, %eax /* execve */
mov (%rbx), %rdi
lea (%rbx), %rsi
mov %rbp, %rdx
syscall
2: mov $60, %eax /* _exit */
mov $1, %edi
syscall
print:
mov $1b, %rsi
mov $0xcccccccccccccccd, %r8
0: sub $1, %rsi
mov %rdi, %rax
mul %r8
shr $3, %rdx
mov %rdi, %rax
lea (%rdx, %rdx, 4), %rcx
add %rcx, %rcx
sub %rcx, %rax
add $'0', %eax
mov %al, (%rsi)
mov %rdi, %rax
mov %rdx, %rdi
cmp $9, %rax
ja 0b
mov $1, %eax /* write */
mov $3, %edi
mov $1b+1, %rdx
sub %rsi, %rdx
syscall
ret
if ! abspath=$(command -v "$1") ||
! case $abspath in */*) ;; *) false; esac then
echo "$1: bad command"
exit 1
fi
shift
trap 'printf "\33[?25h\n"' EXIT
printf '\33[?25l'
stty -echoctl </dev/tty
"${0%/*}/_${0##*/}" "$abspath" "$@" | {
case $TWARMUP in
''|*[!0-9]*) ;;
*) printf 'warming up...'
head -n $TWARMUP >/dev/null
esac
printf '\r\33[Kreal\tuser\tsys\n'
awk -v OFS='\t' '{
for (i = 1; i <= NF; i++) {
mean[i] *= (NR-1)/NR
mean[i] += $i/NR
$i = int(mean[i])
}
printf "\r\33[K%s", $0
}'
}
# vim: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment