Home > Software design >  I don't know why the division produces a quotient and a remainder that are wrong
I don't know why the division produces a quotient and a remainder that are wrong

Time:05-14

How to fix this one: I don't know why both the quotient and the remainder are wrong.

My DOSBox Code:

.model small
.stack 100h
.data

Dividend        db 0dh,0ah,"Enter Dividend    : $"  ;string
Divisor         db 0dh,0ah,"Enter Divisor     : $"
Quotient        db 0dh,0ah,"Display Quotient  : $"
Remainder       db 0dh,0ah,"Display Remainder : $"

.code
main proc   ;main program here
            
mov ax,@data                ;initialize ds
mov ds,ax
            
mov ah,09h                  ;Show Enter Dividend
lea dx, Dividend
int 21h
            
mov ah,01h                  ;Input Dividend
int 21h
mov bh,al
            
mov ah,09h                  ;Show Enter Divisor
lea dx, Divisor
int 21h
            
mov ah,01h                  ;Input Divisor
int 21h
mov bl,al
            
mov ah,00h                  ;Divide
mov al,bh
div bl
mov cx,ax
add cx,3030h
            
mov ah,09h                  ;Show Display Quotient
lea dx, Quotient
int 21h
            
mov ah,02                   ;Display Quotient
mov dl,cl
int 21h
            
mov ah,09h                  ;Show Display Remainder
lea dx, Remainder
int 21h
            
mov ah,02                   ;Display Remainder
mov dl,ch
int 21h
            
mov ah,4Ch                  ;end here
int 21h
            
main endp
end main    

Error Result:

Enter Dividend    : 7
Enter Divisor     : 3
Display Quotient  : 1
Display Remainder : 4

CodePudding user response:

The strange results that you show us stem from the fact that the program executes the division 55 / 51 which will indeed produce a quotient of 1 and a remainder of 4.

The DOS.GetCharacter function 01h that you use for input returns an ASCII code in AL. When you push 7 on the keyboard, the AL register will hold the ASCII code 37h or 55 in decimal. Before using this input in a calculation you need to convert from character into the value that the digit represents. A simple sub al, 30h does that. Of course you need to do the same thing for the second input too:

...
mov ah, 09h                  ;Show Enter Dividend
lea dx, Dividend
int 21h
            
mov ah, 01h                  ;Input Dividend
int 21h
sub al, 30h
mov bh, al
            
mov ah, 09h                  ;Show Enter Divisor
lea dx, Divisor
int 21h
            
mov ah, 01h                  ;Input Divisor
int 21h
sub al, 30h
mov bl, al
...
  • Related