Assembly infinite loop with printf function [duplicate]

六月ゝ 毕业季﹏ 提交于 2021-02-05 08:01:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!