0

I created a calculator that get two numbers and returns the complete and remaining part of the result of the division between the large number and the small number, The program work fine for me for single digit results after the division, But when i get more then one digit result or in the complete part or in the remainder i was able to identify where the problem was occurring but I couldn't understand why... The problem start at ~Convert1~ Label when i try to div my multi digit number by 10H i get wrong results in ax and dx, For example when my first number is 100 and the second num is 3 when i try to divide the complete part (33) in hex 0021 by (10) in hex 000A i get in ax the value 199C and in dx the value 0009 and i dont understand why... D:



.MODEL SMALL
.STACK  100h
.DATA
msg1 db "Please Enter Your First Number:",'$'
msg2 db "Please Enter The Second Number:", '$'
msg3 db "The result of the division is:", '$'
resultstr db 12 dup ('$'),0DH
msg4 db "The rest of the division is:", '$'
reststr db 12   dup ('$'),0DH
Ten dw 10
num1 dw 0
num2 dw 0
num1str db 12,?,12 dup('$'),0DH
num2str db 12,?,12 dup('$'),0DH

.Code

start:
    mov ax, @data
    mov ds, ax
    ; Printing Message1 with a lineBreak
    lea dx, msg1
    mov ah, 09
    int 21H
    mov dl,0AH
    mov ah, 02
    int 21H

    ; Getting The first num1    
    mov ah, 0AH
    lea dx, num1str
    int 21H

    ; Printing Message1 with a lineBreak
    lea dx, msg2
    mov ah, 09
    int 21H
    mov dl,0AH
    mov ah, 02
    int 21H

    ; Getting The first num2    
    mov ah, 0AH
    lea dx, num2str
    int 21H

    mov cl,byte ptr num1str + 1 ; puting in cx the len of the num1str
    mov ch,0

    mov si, offset num1str + 2
    xor ax, ax
    FirstConvert: ; Here we convert the num1 String input from his ascii value into a intger value...
        mul Ten
        xor bx, bx
        mov bl, [si]
        sub bx, '0'
        add ax, bx
        inc si
     loop   FirstConvert

    add num1, ax ; after we calculate his intger value we put him in num1


    mov cl,byte ptr num2str+1 ; puting in cx the len of num2str
    mov ch,0

    mov si, offset num2str + 2
    xor ax, ax
    SecondConvert: ; Here we convert the num2 String inpurt from his ascii value into intger value ... 
        mul Ten
        xor bx, bx
        mov bl, [si]
        sub bl, '0'
        add ax, bx
        inc si
        loop    SecondConvert

    add num2, ax ;after we calculate his intger value we put him in num2
    mov ax, num1 ; In the next 5 lines im going to check who is the bigger number...
    mov bx,num2 ; ^^
    cmp ax,bx ; ^^
    jge BigNum1 ; The case num1 is bigger
    jmp BigNum2 ; The case num2 is bigger

    BigNum1: ; Here I first organize my numbers and then divide them and then send them to my converters to get their ascii value for the printing...
        mov ax,num1
        mov bx,num2
        div bx
        mov num1,ax ; using our values to keep the results. 
        mov num2,dx ; using our values to keep the results.
        jmp Convert1



    BigNum2: ; Here I first organize my numbers and then divide them and then send them to my converters to get their ascii value for the printing...
        mov ax,num2
        mov bx,num1
        div bx  
        mov num1,ax ; using our values to keep the results. 
        mov num2,dx ; using our values to keep the results. 
        jmp Convert1

    ; now im going to convert the integers to their ascii values for the print.
    ; first lets start with the intger part. 

    Convert1:
    lea si, resultstr ; Si pointing into my resultstr that im going to use for the print...
    xor ax,ax
    xor bx,bx
    mov ax, num1 ; Convert to ascii value
    cmp ax,10
    jl Low10
    mov bx, Ten
    div bx
    add dx,'0'
    mov [si],dx
    inc si
    cmp ax , 0 ; If ax == 0 that mean we dont need to convert anymore digits... 
    jg Convertor1 ; if ax!=0 go to Convertor1 that going to this action until we got all our digits..
    je Convert2

    Low10:
    add ax,'0'
    mov [si], ax
    jmp Convert2



    Convertor1: ; he same steps we took earlier
        cmp ax,10
        jl Low10
        xor dx,dx
        mov bx,Ten
        div bx
        add dx,'0'
        mov [si],dx
        inc si
        cmp ax,0
        jg Convertor1
        je Convert2

    Convert2: ; Now we doing the same procedure we did for the integer part of the division result
        lea di, reststr 
        mov ax,num2
        cmp ax,10
        jl Low10B
        xor dx,dx
        mov bx, Ten
        cmp ax, 0 ; in case we have zero rest.
        je zero
        div bx
        add dx,'0'
        mov [di],dx
        inc di
        cmp ax, 0
        jg Convertor2
        je TheEnd


        Low10B:
            add ax,'0'
            mov [di],ax
            jmp TheEnd

    Convertor2: ; Allmost same loops ecxept here in the last line we jump into the TheEnd procedure...
        cmp ax,10
        jl Low10B
        xor dx,dx
        mov bx,Ten
        div bx
        zero:
        add dx,'0'
        mov [di],dx
        inc di
        cmp ax,0
        jg Convertor2
        je TheEnd


    TheEnd: ; Here we printing our converted^2 results and 
        lea dx, msg3
        mov ah, 09
        int 21H
        lea dx, resultstr
        mov ah,09
        int 21H
        lea dx, msg4
        mov ah, 09
        int 21H
        lea dx, reststr
        mov ah, 09
        int 21H



    ;end the program
    mov ah, 4ch
    mov al, 0
    int 21H

END start



Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ZzToP
  • 33
  • 4
  • 1
    As usual, you forgot to zero `dx` before the division. `div bx` uses `dx` for the top 16 bits of the dividend. Consult an instruction set reference. – Jester May 15 '20 at 14:22
  • Thank you so much!! what a rookie mistake.. Wish you always have all your programs working in the first run! – ZzToP May 15 '20 at 15:27

0 Answers0