hamxa rajput
hamxa rajput

Reputation: 1

Assembly,add function

I've a problem in my assembly code.I want to add user input two numbers after the swapping the numbers but when i add those numbers add function did not work well.Thanks

This the code

.model small 
.stack 100h
.data
        msg1 db 'Enter the number1:$'
        msg2 db 'Enter the number2:$'
        msg3 db 'After swap numbers are:$'
        msg4 db 'Sum is:$'
        num1 db ?
        num2 db ?
        sum db ?
        diff db ?
.code
MAIN PROC
        mov ax,@data
        mov ds,ax

        mov ah,09h         ;display first msg
        mov dx,offset msg1
        mov ah,01h         ;taking input
        int 21h
        mov num1,al


        mov ah,09h         ;display second msg
        mov dx,offset msg2
        int 21h
        mov ah,01h         ;taking input
        int 21h
        mov num2,al

        mov bl,num1
        mov cl,num2
        mov num1,cl
        mov num2,bl

        mov ah,09h         ;display third msg
        mov dx,offset msg3
        int 21h
        mov ah,02h
        mov dl,num1
        int 21h
        mov ah,02h
        mov dl,num2
        int 21h

        mov bl,num1
        add bl,num2
        mov sum,bl

        mov ah,09h       ;display fourth msg
        mov dx,offset msg4
        int 21h
        mov ah,02h
        mov dl,sum
        int 21h

        mov ah,4ch
        int 21h
MAIN ENDP 

END MAIN

Upvotes: 1

Views: 5187

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

Your program inputs two 1-digit numbers and thus it's possible for the sum to be as high as 18. Your code doesn't deal with this possibility but it could be this is intentional.

When you took the input you (hopefully) recieved ASCII characters in the range 48 to 57 (They represent the numbers 0 to 9). Before assigning these values to your variables num1 and num2 you should have got rid of the character nature of these values by subtracting 48.

mov ah, 09h         ;display first msg
mov dx, offset msg1
mov ah, 01h         ;taking input
int 21h
sub al, 48
mov num1, al
mov ah, 09h         ;display second msg
mov dx, offset msg2
int 21h
mov ah, 01h         ;taking input
int 21h
sub al, 48
mov num2, al

This way your sum later on will be the true sum of both numbers.

When ready to output any results you have to turn the values into their text representation. Just add 48.

mov ah, 09h         ;display third msg
mov dx, offset msg3
int 21h
mov ah, 02h
mov dl, num1
add dl, 48
int 21h
mov ah, 02h
mov dl, num2
add dl, 48
int 21h

mov ah, 09h         ;display fourth msg
mov dx, offset msg4
int 21h
mov ah, 02h
mov dl, sum
add dl, 48
int 21h

Upvotes: 1

Related Questions