使用printf函数组装无限循环

时间:2016-08-31 16:43:43

标签: assembly att

任何人都可以解释为什么这段代码片段会进入无限循环吗?

我认为它与printf函数有关。

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 个答案:

答案 0 :(得分:0)

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.