Task and Background: For the past couple of week i've been learning assembly language, and as project i decided to build a 32bit calculator, that can multiplty, subtract, divide, and add numbers. I get as an input 2 16bit numbers (word, 2^16 -1 max), apply the mathmatical function on them, and then storing the answer in a 32bit variable (double word).
The Problem and My Solution: is that when trying to print the answer, i divide the number by 10 and push the remainder into the stack, and keep until the quotient is 0. But some times the quotient is bigger than the size of a word (2^16 -1). I can't manage to find a way around it.
What I want it to do: I want to divde the number (that is stored in the 32bit var) by 10, store the remainder into the stack, and store the quotient into a 32bit variable. Then looping this action until the quotinet is equal to zero.
Currently: I'm stuck, I am yet to find a way to divde two number without storing the quintent into ax.
print:
mov dx, [word ptr answer+2]
mov ax, [word ptr answer]
mov bx,10 ;CONST
push bx ;Sentinel
a:
div bx
push dx
xor dx,dx
cmp ax,0
JNE a
; first one in the stack one must be a digit
pop dx
b:
add dx, '0'
mov ah, 2
int 21h
pop dx
cmp dx, bx
jne b
P.s. I found this code that manages to print a 32bit number, but i can't fully understand How it works... If someone can explain me how it works?, and especially why does it divides (by 10) the top 16bits first and uses the reminder, in the division of the low 16bits. While learnig a new coding language I Have a rule, i can't use code from the web unless i fully understand it.
mov bx,10 ;CONST
push bx ;Sentinel
.a: mov cx,ax ;Temporarily store LowDividend in CX
mov ax,dx ;First divide the HighDividend
xor dx,dx ;Setup for division DX:AX / BX
div bx ; -> AX is HighQuotient, Remainder is re-used
xchg ax,cx ;Temporarily move it to CX restoring LowDividend
div bx ; -> AX is LowQuotient, Remainder DX=[0,9]
push dx ;(1) Save remainder for now
mov dx,cx ;Build true 32-bit quotient in DX:AX
or cx,ax ;Is the true 32-bit quotient zero?
jnz .a ;No, use as next dividend
pop dx ;(1a) First pop (Is digit for sure)
.b: add dl,"0" ;Turn into character [0,9] -> ["0","9"]
mov ah,02h ;DOS.DisplayCharacter
int 21h ; -> AL
pop dx ;(1b) All remaining pops
cmp dx,bx ;Was it the sentinel?
jb .b ;Not yet
Thanks