比如我想把字符串转成整数,当我在字符串中输入1234时,它会转成整数1234。但是,当我输入1234时,结果只有12出来,我不知道是什么问题。
%include "asm_io.inc"
segment .bss
string resb 32
segment .text
global main
main:
enter 0,0 ; setup stack frame
pusha
mov edx, 0
mov ecx, 0
mov ebx, 0
repeat: call read_char
sub eax, 48
mov esi, eax
mov eax, ecx
mov ebx, 10
mul ebx
mov ecx, eax
add ecx, esi
mov byte [string+edx], al
cmp al, 0x0a
jne repeat
mov byte [string+edx-1], 0
mov eax, ecx
call print_int
call print_nl
popa
mov eax, 0 ; return value
leave ; leave stack frame
ret
单单分析,不跑,看来你的逻辑是错误的。在第二次循环迭代中,您将eax
等于 1,因此在将其乘以10
( ebx
) 后,您将产生等于 Enter 的 ascii 值 - 0x0a
(10dec) 的结果。
您应该在阅读字符后立即移动检查输入值。所以试着让你的循环像这样
repeat:
call read_char
cmp al, 0x0a
je exit_loop // exit the loop if enter
//code as before
jmp repeat //jump unconditionally to the beginning of the loop
exit_loop:
mov byte [string+edx-1], 0
我认为可能还有其他一些问题,因为我不知道哪里会edx
增加。
但是正如我所写的那样 - 这只是通过分析没有实际运行的情况。你有程序和调试器。调试一下!单步执行代码,分析寄存器并按照 Michael Petch 的建议确认发生了什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句