我的简单装配程序中的无限循环

时间:2014-03-02 10:17:09

标签: loops assembly x86

我正在学习循环和跳转程序集,我尝试做一个简单的循环。我希望调用printf命令10次。我已将counter变量设置为1.我还将%edx设置为1,然后在每次迭代时递增它。如果它等于10,那么我们应该退出循环。但现在循环是无限的。我已使用gdb调试,%edx似乎在printf函数中被覆盖。这就是为什么我push %edx到堆栈而popprintf调用之后回来,但它不起作用。我错过了什么?

.section .data
output:
    .asciz "Value is %d\n"
val1:
    .int 123
counter:
    .int 1
.section .text
.globl _start
_start:
    nop
    movl counter, %edx   # start at 1
gohere:
    movl val1, %ebx      # move value 123 to %ebx
    pushl %edx           # push %edx to stack
    pushl %ebx           # push %ebx to stack 
    pushl $output
    call printf          # call printf
    popl %edx            # pop %edx value
    inc %edx
    cmp $10, %edx        # if %edx is less than 10...
    jl gohere            # ... go to gohere, otherwise exit

    movl $0, %ebx
    movl $1, %eax
    int $0x80

1 个答案:

答案 0 :(得分:2)

你推了output作为最后一次推送,所以第一个pop会弹出output。它是Stack,它是LIFO。在弹出它之后,代码output将在edx中。 解决它在popl edx之前放了两个pop:

popl output
popl ebx