Skip to content

Instantly share code, notes, and snippets.

@maliubiao
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maliubiao/b585d777742cc57fc760 to your computer and use it in GitHub Desktop.
Save maliubiao/b585d777742cc57fc760 to your computer and use it in GitHub Desktop.
section .data
OUTPUT_FMT: db "timeit: %ds %dus", 0x0a, 0x0
;引用的库函数与导出的符号
section .text
global main
extern fork
extern time
extern sleep
extern wait
extern execvp
extern printf
extern gettimeofday
main:
;{
push rbp
mov rbp, rsp
push rdi
push rsi
cmp rdi, 1
je normal_end
;gettimeofday;
sub rsp, 0x10
mov rdi, rsp
mov rsi, 0
mov rax, 0
call gettimeofday wrt ..plt
;fork()
mov rax, 0
call fork wrt ..plt
cmp rax, 0
jne wait_child_process
add rsp, 0x10
pop r8
add r8, 8
;path -> argv[1]
mov rdi, [r8]
;argv -> argv[1]...
mov rsi, r8
call execvp wrt ..plt
leave
ret
wait_child_process:
;wait()
mov rax, 0
call wait wrt ..plt
sub rsp, 0x10
mov rdi, rsp
mov rsi, 0
mov rax, 0
call gettimeofday wrt ..plt
;s
mov rsi, qword [rsp]
sub rsi, qword [rsp+16]
;ms
mov rdx, qword [rsp+8]
sub rdx, qword [rsp+24]
cmp rdx, 0
jl fix_tv_sec
mov rdi, OUTPUT_FMT
mov rax, 0
call printf wrt ..plt
jmp normal_end
fix_tv_sec:
dec rsi
neg rdx
mov rdi, OUTPUT_FMT
mov rax, 0
call printf wrt ..plt
normal_end:
mov rax, 0
leave
ret
;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment