Home > Software engineering >  How do I fix this code to that the error is the quotient and remainder answer?
How do I fix this code to that the error is the quotient and remainder answer?

Time:06-03

using tasm/tlink/dosbox in notepad

want: how do i fix 2 digit divide to 2 digits and when if the quotient or remainder answer is also 2 digits? and also what is the possible logic?

my flow is: the flow I made was first I took the first digit of the first number or dividend then I multiplied by 10 to be and I added to the second digit the same as the second number or divisor so that it would be 2 digits in the register after that I'll divide them both.

My Code in notepad :

.model small
.stack 100h
.data
            d0  db 0dh,0ah,"Base 03$"
            d1  db 0dh,0ah,"Enter Dividend    : $"  ;string
            d2  db 0dh,0ah,"Enter Divisor     : $"
            d3  db 0dh,0ah,"Display Quotient  : $"
            d4  db 0dh,0ah,"Display Remainder  : $"
            
.code
main proc   ;main program here
            
            mov ax,@data                ;initialize ds
            mov ds,ax
            
            Div1:
                mov ah,09h
                lea dx,d0
                int 21h
                
                lea dx, d1
                int 21h
                mov ah,01h
                int 21h                     ;1st Digit
                
                mov ch,al
                
                mov ah,01h
                int 21h                     ;2nd Digit
        
                mov cl,al
                
                or cx,3030h
                
                mov al,ch
                mov bl,10h
                mul bl                      ;02*10 = 20
                
                mov bh,al
                add bh,cl                   ;dividend
        
            Div2:
                mov ah,09h
                lea dx, d2
                int 21h
                mov ah,01h
                int 21h                     ;1st Digit
                
                mov ch,al
                
                mov ah,01h
                int 21h                     ;2nd Digit
        
                mov cl,al
                
                or cx,3030h
                
                mov al,ch
                mul bl                      
                mov dh,al
                add dh,cl                   ;divisor
                
                mov ah,00h
                mov al,bh
                aad
                div dh
                mov cx,ax
                or  cx,3030h
                
                mov ah,09h
                lea dx, d3
                int 21h
                mov ah,02h
                mov dl,cl
                int 21h
                
                mov ah,09h
                lea dx, d4
                int 21h
                mov ah,02h
                mov dl,ch
                int 21h
                
                mov ah,4Ch                  ;end here
                int 21h
            
main endp
end main    

Output:

enter image description here

Want Output:

Enter Dividend     : 22
Enter Divisor      : 02
Display Quotient   : 02
Display Remainder  : 00

proj

Enter Dividend     : 21
Enter Divisor      : 02
Display Quotient   : 10
Display Remainder  : 01

proj

Enter Dividend     : 21
Enter Divisor      : 11
Display Quotient   : 01
Display Remainder  : 10

like this calculation but the only i need is the quotient and remainder.

enter image description here

CodePudding user response:

Are you sure about "Want output" ?

Enter Dividend     : 22   <<< 8d
Enter Divisor      : 02   <<< 2d
Display Quotient   : 02   ??? 4d == 11
Display Remainder  : 00   <<< 0d

or cx,3030h does nothing to your code because those bits will already have been set, they're part of the ASCII codes from 48 to 50. For conversion purposes you would normally use sub cx,3030h.

Next your multiplication with 10h (16 in decimal) is trying to turn the input into packed BCD (binary coded digits) format. Later the code will use the aad instruction that operates on unpacked BCD and importantly uses 10 as its number base where your program is going for number base 3.

You seem to expect a 2-digit output for both quotient and remainder. Then just printing 2 characters from CL and CH is not enough.

To do

mov ah,01h
int 21h                     ;1st Digit
mov ch,al
mov ah,01h
int 21h                     ;2nd Digit
mov cl,al
sub cx,3030h
mov al, 3
mul ch
add al, cl
mov bh,al                   ;dividend = (D1 * 3)   D2

Similarly for the divisor, but put it in BL

mov ah, 0
mov al, bh    ; Dividend in BH
div bl        ; Divisor in BL
mov cx, ax    ; Quotient in CL, Remainder in CH

Ready to print. See my answer I need to show 3 digit answer Using tasm/tlink/dosbox in notepad

CodePudding user response:

Your algorithm for conversion is confused:

first I took the first digit of the first number or dividend then I multiplied by 10 to be and I added to the second digit

In fact you have multiplied the first digit by 16 because of mov bl,10h. See also questions 8086 simple decimal addition from user input or How to convert an ASCII char to a decimal representation?. Or use some more general method to how to convert input string with more than two decimal numbers to a binary value. See the macro LodD for inspiration.

What you need is Turbo Debugger which allows to execute you program instruction by instruction and watch if the registers change as expected.

  • Related