简体   繁体   中英

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. I have set the counter variable to 1. I have also set %edx to 1 and then I increment it for every iteration. If it is equal to 10, then we should exit the loop. But now the loop is infinite. I have debugged with gdb and %edx seems to be overwritten in the printf function. That is why I push %edx to the stack and the pop it back after the printf call, but it doesn't work. 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 . it is Stack and it is LIFO. in your code output will be in edx after you pop it. to solve it put two pops before popl edx :

popl output
popl ebx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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