故事(我是新手):我开始阅读关于使用著名的 nasm 汇编器在汇编(x86 英特尔)中编程的 pdf 教程,但我在执行非常基本的汇编代码时遇到了问题(受教程中关于循环的代码的启发)。
问题(JE FAILS):这个汇编代码应该从标准输入读取一个数字(一个字符(这意味着'0'+数字)),然后写入屏幕数字乘以“Hello world\n”。非常简单的循环:减少数字如果数字等于零('0' 不是字符的整数)跳转(je)到出口(mov eax,1\nint 0x80)。
听起来很简单,但是当我尝试执行输出时很奇怪。(真的很奇怪而且很大)它在循环中运行了很多次,当数字等于“0”时停止(很奇怪,因为直到程序停止条件数字==“0”测试了很多次,应该是真的)
实际上我的问题是当数字=='0'时代码无法跳转
代码(很大):
segment .text
global _start
_start:
;Print 'Input a digit:'.
mov eax,4
mov ebx,1
mov ecx,msg1
mov edx,len1
int 0x80
;Input the digit.
mov eax,3
mov ebx,0
mov ecx,dig
mov edx,2
int 0x80
;Mov the first byte(the digit) in the ecx register.
;mov ecx,0
mov ecx,[dig]
;Use ecx to loop dig[0]-'0' times.
loop:
mov [dig],ecx
mov eax,4
mov ebx,1
mov ecx,dig
mov edx,1
int 0x80
mov eax,4
mov ebx,1
mov ecx,Hello
mov edx,Hellolen
int 0x80
;For some debuging (make the loop stop until return pressed)
;mov eax,3
;mov ebx,0
;mov ecx,some
;mov edx,2
;int 0x80
;Just move dig[0](some like character '4' or '7') to ecx register and compare ecx with character '0'.
mov ecx,[dig]
dec ecx
cmp ecx,'0'
;If comparison says ecx and '0' are equal jump to exit(to end the loop)
je exit
;If not jump back to loop
jmp loop
;Other stuff ...(like an exit procedure and a data(data,bss) segment)
exit:
mov eax,1
int 0x80
segment .data
msg1 db "Input a digit:"
len1 equ $-msg1
Hello db ":Hello world",0xa
Hellolen equ $-Hello
segment .bss
dig resb 2
some resb 2
输出:
输入一个数字:4 4:Hello world 3:Hello world 2:Hello world 1:Hello world 0:Hello world ... ...(许多循环之后) ... 5:Hello world 4:Hello world 3:Hello世界2:你好世界1:你好世界$
那是我的问题:这段代码有什么问题?你能解释一下吗?而且我不需要会神奇地(无需解释)运行的替代代码,因为我尝试学习(我是新手)
这是我的问题(也是我在 Stackoverflow.com 中的第一个问题)