I wrote this code to display a current value after some calculations. But the program gives wrong output when it should gave decimal 10 result it gives 13107 result and i cant find the reason why. Before this label what I did was basically getting inputs from the user and storing those values at si register.
islem:
MOV AX,[SI+14]
MOV BX,[SI+10]
MOV CX,[SI+2]
SUB AX,BX
SUB AX,CX
MOV BX,[SI+8]
MOV [SI+16],AX
MOV DX,00H
MOV AX,[SI+16]
DIV BX
MOV [SI+18],OFFSET IE
MOV [SI+18],AX
MOV [SI+20],DX
CALL NEWLINE
mov dx, offset Ie_msg
mov ah,9
int 21h
MOV AX,[SI+18]
MOV DX,[SI+20]
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
ret
MOV AX,[SI+18] MOV DX,[SI+20]
The result of your calculation came from a division (div bx
). The quotient is held in the AX
register only. The DX
register contains the remainder from the division.
Just load AX
with mov ax, [si+18]
and then zero DX
with xor dx, dx
.
Hereafter you can run my conversion code...