我一直在尝试调试一个小型程序,在这个程序中,我要求一个除数和一个除数,并且必须输出商和剩余部分。然而,由于某种原因,我的商数和余数没有输出到屏幕上。这是我的代码:
segment .data
prompt db "Please enter a number: ", 10
promptLen equ $-prompt
prompt2 db "Please enter the divisor: ", 10
prompt2Len equ $-prompt2
prompt3 db "Your quotient is: ", 10
prompt3Len equ $-prompt3
prompt4 db "Your remainder is: ", 10
prompt4Len equ $-prompt4
segment .bss
inputNum resb 2
inputDiv resb 2
quotient resb 2
remainder resb 2
segment .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, prompt
mov edx, promptLen
int 80h
mov eax, 3
mov ebx, 0
mov ecx, inputNum
mov edx, 2
int 80h
mov eax, 4
mov ebx, 1
mov ecx, prompt2
mov edx, prompt2Len
int 80h
mov eax, 3
mov ebx, 0
mov ecx, inputDiv
mov edx, 2
int 80h
xor edx, edx
mov ax, [inputNum]
mov bx, [inputDiv]
sub ax, '0'
sub bx, '0'
div bx
add ax, '0'
add dx, '0'
mov [quotient], ax
mov [remainder], dx
mov eax, 4
mov ebx, 1
mov ecx, prompt3
mov edx, prompt3Len
int 80h
mov eax, 4
mov ebx, 1
mov ecx, [quotient]
mov edx, 2
int 80h
mov eax, 4
mov ebx, 1
mov ecx, prompt4
mov edx, prompt4Len
int 80h
mov eax, 4
mov ebx, 1
mov ecx, [remainder]
mov edx, 2
int 80h
jmp exit
exit:
mov eax, 1
xor ebx, ebx
int 80h如果有人能帮助我理解我做错了什么,我将不胜感激。
发布于 2014-10-20 02:23:21
系统调用4(写)需要一个char *作为ecx中的缓冲地址。使用您的代码:
mov eax, 4 ; sys_write
mov ebx, 1 ; standard output
mov ecx, [quotient] ; here, ecx <- your character code
mov edx, 2 ; two bytes (hmmm)
int 80h您正在用输入的实际数据(而不是它的地址)加载ecx。
您需要做的是用字符的地址加载ecx,它可能就是这样的:
mov ecx, quotient ; here, ecx <- your character address您还可能希望将字节数减少到一个而不是两个。无论如何,将整数转换为字符(添加'0')的方式只适用于一个字符数字,因此您只需要最小的有效字节(x86内存地址最低的字节)。
其余的也是。
顺便说一句,当你解决这个问题时,你还是会得到一个有趣的结果。将5除以2,得到的商为1,余数为3,这显然是错误的。
这与您如何输入数据并将值加载到ax和bx中有关。
因为您要为每个数字读取两个字节,所以内存中会加载数字本身'5'或0x35,后面跟着换行符0x0a。
然后,当您将两个字节的单词加载到ax中时,它将以0x0a35结束。
在分别减去0x0030 ('0')后,您将得到0x0a05和0x0a02,以小数表示,即2565和2562。当你除以这些数字时,你确实得到了1的商数和3的余数。
要解决这个问题,您可以简单地丢弃0x0a00,但是在加载值时,可以更改:
xor edx, edx
mov ax, [inputNum]
mov bx, [inputDiv]
sub ax, '0'
sub bx, '0'转入:
xor edx, edx
mov ax, [inputNum]
mov bx, [inputDiv]
and ax, 0xff ; add these
and bx, 0xff ; two lines.
sub ax, '0'
sub bx, '0'https://stackoverflow.com/questions/26457647
复制相似问题