简体   繁体   English

我简单的汇编程序中的无限循环

[英]Infinite loop in my simple assembly program

I am learning loops and jumps in assembly and I try to make a simple loop. 我正在学习循环和汇编中的跳转,我尝试做一个简单的循环。 I want the printf command to be called 10 times. 我希望printf命令被调用10次。 I have set the counter variable to 1. I have also set %edx to 1 and then I increment it for every iteration. 我将counter变量设置为1。我还将%edx设置为1,然后在每次迭代中将其递增。 If it is equal to 10, then we should exit the loop. 如果等于10,则应该退出循环。 But now the loop is infinite. 但是现在循环是无限的。 I have debugged with gdb and %edx seems to be overwritten in the printf function. 我已经用gdb调试过了, %edx似乎在printf函数中被覆盖了。 That is why I push %edx to the stack and the pop it back after the printf call, but it doesn't work. 这就是为什么我push %edx堆栈,并在printf调用后将其pop回去的原因,但它不起作用。 What have I missed? 我错过了什么?

.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

you pushed output as the last push so the first pop will pop output . 您将output作为最后一次推送,因此第一个弹出窗口将弹出output it is Stack and it is LIFO. 它是Stack,它是LIFO。 in your code output will be in edx after you pop it. 弹出代码后,代码output中的代码将在edx to solve it put two pops before popl edx : 为了解决它,在popl edx之前放两个pops:

popl output
popl ebx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM