0

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?

elijah
  • 13
  • 3
  • First of all, one byte at a time is very slow. Using pushfd / popfd is pointless because you're using `inc` (good on modern CPUs), and `loop` (bad on modern Intel), neither of which modify CF, for this reason. See [Problems with ADC/SBB and INC/DEC in tight loops on some CPUs](https://stackoverflow.com/q/32084204) / [Why is the loop instruction slow? Couldn't Intel have implemented it efficiently?](https://stackoverflow.com/q/35742570) – Peter Cordes Dec 23 '21 at 11:42
  • 2
    Re: the final display in hex, `'5' + '5'` = `0x35 * 2` = `0x6A`. Remember you read ASCII strings into your buffers, not bigint directly. I assume the extra `00` between `'\n' * 2 = 0xa * 2 = 0x14` is some kind of printing bug. Check memory contents with a debugger to see what input you're adding, and what output you actually wrote, and single-step to see how you got there. – Peter Cordes Dec 23 '21 at 11:46
  • To efficiently convert lots of bytes to hex digits at once into an ASCII string, see [How to convert a binary integer number to a hex string?](https://stackoverflow.com/q/53823756) – Peter Cordes Dec 23 '21 at 11:47

0 Answers0