问题
can anyone explain why this code snippet goes into an infinite loop?
I presume it would have something to do with the printf function.
q1: .asciz "Hello World\n"
.global main
main:
movq %rsp, %rbp
movq $3, %rcx
jmp bottom
loop:
movq $0, %rax
movq $q1, %rdi
call printf
bottom:
decq %rcx
cmpq $0, %rcx
jne loop
end:
movq $0, %rdi
call exit
回答1:
The only registers that the called function is required to preserve are: rbp, rbx, r12, r13, r14, r15. All others are free to be changed by the called function.
Therefore, the likelihood is that printf is modifying the rcx register, so it never goes to 0.
If you push rcx and pop it later, that would prevent it from being modified.
Note it does not appear you are pushing args for printf. I think printf takes 2 args.
来源:https://stackoverflow.com/questions/39254931/assembly-infinite-loop-with-printf-function