[英]Assembly - jmp and cmp result in infinite loop
Here is my code:这是我的代码:
%include "io.inc"
section .data
var DB 0
var2 DB 0
section .text
global CMAIN
print:
PRINT_DEC 1, var
inc BYTE [var]
mov eax, [var]
ret
forLoop:
call print
cmp eax, [var2]
jle forLoop
ret
CMAIN:
GET_DEC 1, var2
call forLoop
ret
This uses Simple-ASM's default library.这使用 Simple-ASM 的默认库。
When given with the input 5
(which is then placed into var2
), I expect an output of:当给定输入
5
(然后放入var2
)时,我期望输出:
012345
However, when given the input 5
, I get this instead:但是,当给出输入
5
,我得到了这个:
01234567891011...127128-127-126...-10123...
It appears that the cmp
and jle
don't work properly when checking the two numbers, and forLoop
never stops calling itself, which results in var
being continuously inc
ed.看来,
cmp
和jle
不能正常工作检查两个数字的时候,和forLoop
永远不会停止自称,这导致var
正在不断inc
版。
When I placed a PRINT_DEC 1, var2
after the GET_DEC
statement, like so:当我在
GET_DEC
语句之后放置PRINT_DEC 1, var2
,如下所示:
CMAIN:
GET_DEC 1, var2
PRINT_DEC 1, var2
call forLoop
ret
And comment out the other PRINT_DEC
line, there's no output at all.并注释掉另一行
PRINT_DEC
,根本没有输出。
How can I fix this?我怎样才能解决这个问题?
mov eax, [var]
eax
is a 32-bit register, so this instruction copies 4 bytes from the label var
into eax
. eax
是一个 32 位寄存器,因此该指令将 4 个字节从标签var
复制到eax
。 Similarly,相似地,
cmp eax, [var2]
compares eax
with the 4 bytes at var2
.将
eax
与var2
处的 4 个字节进行比较。 This is a problem because var
and var2
only store 1 byte each.这是一个问题,因为
var
和var2
每个只存储 1 个字节。
Fix:使固定:
mov al, [var]
and和
cmp al, [var2]
respectively.分别。
al
is an 8-bit register (it's the lowest byte of eax
), so this way we properly copy/compare 1-byte quantities. al
是一个 8 位寄存器(它是eax
的最低字节),因此我们可以通过这种方式正确复制/比较 1 字节数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.