多位数的nasm32计算器给出错误的结果

时间:2018-12-21 22:52:00

标签: assembly nasm 32-bit

(初步信息:作为一个新手,我最终设法制作了一个数字计算器,但是要制作一个多位数的计算器,我必须从代​​码中看到互联网的一些“启发”。) 问题是,当我对输入数字计算器进行硬编码时,可以正常工作。但是,当我尝试从用户那里获得输入时,结果完全是混乱的。我正在努力解决此问题将近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

0 个答案:

没有答案