Search code examples
emu8086

Multiplication error not getting proper result?


What am I trying to do?

I am taking two integers from the user and trying to multiple them using mul instruction.

What is the problem?

Each time I try to multiple the 2 integers and try to display the result I get alphabet T as output.

data segment 

    msg1 db "Enter first number:$" 
    msg2 db 10, 13,"Enter second number:$" 
    msg3 db 10, 13,"The product is:$"
    n1 db ? 
    n2 db ?
    pkey db "press any key...$" 

ends

stack segment

    dw 128 dup(0) 

ends

code segment 
start: ; set segment registers: mov ax, data mov ds, ax mov es, ax

; add your code here

mov ax, @data 
mov ds, ax

mov dx, offset msg1 
mov ah,09h 
int 21h

mov ah, 01h 
int 21h

sub al, 48 
mov n1, al

mov dx, offset msg2 
mov ah, 09h 
int 21h

mov ah, 01h 
int 21h

sub al, 48 
mov n2, al

mul n1

mov bl, al

mov dx, offset msg3 
mov ah, 09h 
int 21h  

add bl, 48 
mov dl, bl 
mov ah, 02h 
int 21h

mov ah, 4ch 
int 21h

lea dx, pkey 
mov ah, 9 
int 21h ; output string at ds:dx

; wait for any key....
mov ah, 1 
int 21h

mov ax, 4c00h ; exit to operating system. int 21h
ends

end start ; set entry point and stop the assembler.

Solution

  • Why do you get alphabet T as output ?

    These instruction:

    mov dx, offset msg3 
    mov ah, 09h 
    int 21h
    

    causes the value of AL to be altered to 24 (Its actually INT 21h instruction which changes the value of AL to 24) and the instruction mov ah, 09h sets the content of AH to 09. Then the next instruction

    add ax, 48 
    

    add 48 to contents of AX which gives 84(54 in hex) in AL which is the ASCII value of T and then you display the contents of AL with the following code:

    mov dl, al 
    mov ah, 02h 
    int 21h
    

    which displays the alphabet T.

    To fix it do the following:

    After mul instruction add this:

    mov bl, al     ; save the content of AL to BL
    

    and after displaying msg3 change occurrence of al to bl like this:

    add bl, 48   
    mov dl, bl 
    mov ah, 02h 
    int 21h