[英]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.