assembly - 多位 nasm32 计算器给出错误结果
问题描述
(初步信息:作为一个新手,我最终设法做了一个一位数计算器,但要制作一个多位数计算器,我必须从互联网上获得一些“灵感”,正如代码中所见。)问题是当我硬编码输入数字计算器可以正常工作。但是当我尝试从用户那里获取输入时,结果完全是混乱的。我正在解决这个问题将近 3 天,但没有解决方案。如果有人可以帮助我,我将不胜感激。谢谢大家
section .data
msg2 db 'Enter the first number: ',0xA,0xD
lmsg2 equ $- msg2
msg3 db 'Enter the second number: ',0xA,0xD
lmsg3 equ $- msg3
msg9 db 10,'Result= ',0
lmsg9 equ $- msg9
section .bss
; Spaces reserved for storing the values provided by the user.
num1 resd 4 ;;;;;;;;;;;UPDATED
num2 resd 4 ;;;;;;;;;;;UPDATED
result resd 8 ;;;;;;;;;;;UPDATED
result_len resd 1 ;;;;;;;;;;;UPDATED
section .text
global _start
_start:
; Print on screen the message 2
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, lmsg2
int 80h
; We get num1 value.
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 4
int 80h
; Print on screen the message 3
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, lmsg3
int 80h
; We get num2 value.
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 4
int 80h
mov eax, 12 ;;;;;;;[num1] WHEN BEING PUT A NUMBER DIRECTLY IT WORKS
mov ebx, 55 ;;;;;;;;[num2] UPDATED- WHEN BEING PUT A NUMBER DIRECTLY IT WORKS
; Convert from ascii to decimal
sub eax, '0' ;;;;;;;;;;; UPDATED- COMMENTED OUT
sub ebx, '0' ;;;;;;;;;;; UPDATED- COMMENTED OUT
; Add
add eax, ebx
; Conversion from decimal to ascii
add eax, '0' ;;;;;;;;;;; UPDATED- COMMENTED OUT
; THIS IS THE AREA PROBLEMS HAPPEN
mov edi, result ; argument: Address of the target string
call int2str ; Get the digits of EAX and store it as ASCII
sub edi, result ; length of the string
mov [result_len], edi
; Output "The sum is: "
mov eax, 4
mov ebx, 1
mov ecx, msg9
mov edx, lmsg9
int 0x80
; Output sum
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, [result_len] ;;;;;;;;;;;UPDATED
int 0x80
; Exit code
mov eax, 1
mov ebx, 0
int 0x80
;LOOPS FOR DOING THE MULTI DIGITS OPERATION
int2str: ; Converts an integer in EAX to a string pointed to by EDI
xor ecx, ecx
mov ebx, 10
.LL1: ; First loop: Save the remainders
xor edx, edx ; Clear EDX for div
div ebx ; EDX:EAX/EBX -> EAX Remainder EDX
push dx ; Save remainder
inc ecx ; Increment push counter
test eax, eax ; Anything left to divide?
jnz .LL1 ; if Yes: loop once more.. jump if not zero
.LL2: ; Second loop: Retrieve the remainders
pop dx ; In DL is the value
or dl, '0' ; To ASCII
mov [edi], dl ; Save it to the string (
inc edi ; Increment the pointer to the string
loop .LL2 ; Loop ECX times
mov byte [edi], 0 ; Termination character
ret ; RET: EDI points to the terminating NULL
解决方案
推荐阅读
- php - 为什么我不能将我的表单添加到我的 MySQL 数据库中?
- python - 如何将训练好的模型转换为函数?
- r - 无法使 sen2r 函数工作,缺少一些参数?
- javascript - 给定一个数组,编写一个返回该数组所有可能的三元组/序列的函数?JavaScript
- python - 仅支持有效的不记名身份验证 - Python - Spotify API
- ruby-on-rails - Heroku 环境变量未在 javascript 中为 React/Rails 应用程序设置
- sql - 使用 SQL 提取 XML 数据
- flutter - Flutter 从“Future”返回“Widget”
" - asp.net-core - 在 asp.net core 3.1 中具有密码保护链接的匿名用户 - ClaimsPrinciple/cookie 是最好的方法吗?
- python - 迭代数据框行时将列表添加到列