【发布时间】:2014-03-11 13:03:27
【问题描述】:
故事(我是新手): 我开始阅读有关使用著名的 nasm 汇编器在汇编(x86 英特尔)中编程的 pdf 教程,但我在执行非常基本的汇编代码时遇到了问题(受教程中关于循环的代码的启发)。
问题(JE 失败): 这个汇编代码应该从标准输入读取一个数字(一个字符(表示'0'+数字)),然后写入屏幕数字乘以“Hello world\n”。非常简单的循环:减少数字并且如果数字等于零(' 0' 不是字符的整数)跳转(je)到出口(mov eax,1\nint 0x80)。
听起来很简单,但是当我尝试执行输出时很奇怪。(真的很奇怪而且很大) 它在循环中运行多次,并在 digit 等于 '0' 时停止(很奇怪,因为在程序停止之前,条件 digit == '0' 被测试了很多次,它应该是真的)
其实我的问题是 digit == '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:你好世界 3:你好世界 2:你好世界 1:你好世界 0:你好世界 ... ...(很多循环之后) ... 5:你好世界 4:你好世界 3:你好世界 2:你好世界 1:你好世界 $
这是我的问题:这段代码有什么问题? 你能解释一下吗? 而且我不需要会神奇地(无需解释)运行的替代代码,因为我尝试学习(我是新手)
这是我的问题(也是我在 Stackoverflow.com 中的第一个问题)
【问题讨论】:
标签: linux assembly nasm intel-syntax