本文介绍了带有 printf 函数的汇编无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释为什么这段代码会进入无限循环?

can anyone explain why this code snippet goes into an infinite loop?

我认为这与 printf 函数有关.

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

推荐答案

被调用函数需要保留的唯一寄存器是:rbp、rbx、r12、r13、r14、r15.所有其他的都可以由被调用的函数自由更改.

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.

因此,很可能是 printf 正在修改 rcx 寄存器,所以它永远不会变为 0.

Therefore, the likelihood is that printf is modifying the rcx register, so it never goes to 0.

如果你推送 rcx 并稍后弹出它,那将阻止它被修改.

If you push rcx and pop it later, that would prevent it from being modified.

请注意,您似乎没有为 printf 推送参数.我认为 printf 需要 2 个参数.

Note it does not appear you are pushing args for printf. I think printf takes 2 args.

这篇关于带有 printf 函数的汇编无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 15:16