i am trying to add 256-bit(32-byte) numbers in masm x86 using visual studio 2019. It almost gives me the results but with some weird numbers i can't figure out. i've been on this for so long please help me figure out what am i doing wrong.
INCLUDE Irvine32.inc
.data
num1 byte 32 DUP(?) ; num1 = 32byte number
num2 byte 32 DUP(?) ; num2 = 32byte number
sum byte 33 DUP(?) ;sum = 32byte number
getNum1 BYTE "Enter Number1 : ",0
getNum2 BYTE "Enter Number2 : ",0
dispSum BYTE "Sum = ", 0
.code
main PROC
call getInput ; get input for num1 and num2
call addNum ; add num1 and num2
call displaySum ; display the sum
exit
main ENDP
getInput PROC
;get num1
mov edx, offset getNum1
call writeString
mov edx, offset num1
mov ecx, sizeof num1
call readString
;get num2
mov edx, offset getNum2
call writeString
mov edx, offset num2
mov ecx, sizeof num2
call readString
ret
getInput ENDP
addNum PROC
mov ecx, lengthof num1 ; counter
mov esi, offset num1
mov edi, offset num2
mov edx, offset sum
clc ; clear the carry flag
addition:
mov al, [esi]
adc al, [edi] ; add with carry in al
pushfd
mov [edx], al ; store the partial sum in sum
;increasing the offsets
inc esi
inc edi
inc edx
popfd
loop addition
;if there is a carry from the heighest bit
mov byte ptr [edx], 0
adc byte ptr [edx], 0
ret
addNum ENDP
displaySum PROC
pushfd
mov esi, offset sum
mov ecx, lengthof sum ;
add esi, ecx ; to start from the end of the string i.e, little endian order
sub esi, 1 ; to point to the last byte of the number
mov ebx, type byte ; for the display format for WriteHexB
;displaying sum by traversing through each byte
;mov edx, offset dispsum
;call writeString
disp:
mov al, [esi] ; get each byte of the sum
call writeHexB;for writing the byte integer to the console window in hexadecimal format
sub esi, 1
loop disp
call crlf
popfd
ret
displaySum ENDP
END main
Output:
5 + 5 = 00000000000000000000000000000000000000000000000000000000000014006A
where did the 14 and 6 come from?