问题描述
因此,我刚刚开始学习汇编语言,我进行了此练习,编写了一个代码,该代码从3位数字的用户那里获取输入,加999并打印4位数字的结果。 (例如:用户输入100,代码将数学加999,输出1099)
这是我想出的:
section .data
numToAdd db "999",0x00
userPrompt db "Please enter a 3 digit number (000-999):",0x0a,0x00
len_userPrompt equ $-userPrompt
section .bss
userNum resb 4 ; user input: (3 digit num + \n)
resultNum resb 4 ; Result with a 4 digit number
section .text
global main
main:
mov eax,0x04
mov ebx,0x01
mov ecx,userPrompt
mov edx,len_userPrompt
int 0x80
mov eax,0x03
mov ebx,0x00
mov ecx,userNum
mov edx,4
int 0x80
mov edx,0x02 ; Digit counter
mov ecx,0x03 ; No. of times to loop 3 digits - 3 looops
clc
loop_here:
mov al,[numToAdd+edx] ; Moves: 99|9 -> AL
adc al,[userNum+edx] ; Adds: AL(9) + 3rd number\n
aaa ; Adjust after ADC ( It just adds 96h ? )
pushf
add al,0x30
popf
mov [resultNum+edx],al ; Moves the result to the corresponding index in resultNum
dec edx
loop loop_here
mov eax,resultNum
mov edx,0x04
int 0x80
mov eax,0x01
int 0x80
此行mov [resultNum+edx],al ; Moves the result to the corresponding index in resultNum
上的问题仍然存在,因为它将仅捕获2字节的AL,并将其放入resultNum var中。意思是如果我加9 + 1,即39h + 31h = 6ch,那么AAA将返回102,而我将al加30h将返回132。上面的命令将仅取'32'并放入resultNum内存位置,而不是完整的表示形式实际结果(为10)
在这里总结出什么问题。
TL; DR: 我需要在用户输入的任意3位数字前加上999小数,然后将实际的数学结果打印到stdout。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)