Reading a two digit number in assembly and storing it in a variable

北慕城南 提交于 2019-12-01 13:34:18

Check out the following program. I have edited your one. There is a small mistake as already mentioned by Frank Kotler. That is you didn't convert the user input to digit. You have added 48 to the user input. But you have to subtract 48 from it.

.model small
    .stack 100h
    .data
        msg db "Enter a number: $"
        msg2 db "You have entered: $"
        num1 db 0
        num2 db 0
        temp db 0
        ten db 10
        readNum db 0
        t2 db 0
        t1 db 0
    .code
        mov ax,@data
        mov ds,ax

        call read
        call endL
        call write


        proc endL
            mov dl,0ah
            mov ah,02h
            int 21h
            ret
        endp

        proc read
            mov dx,offset msg
            mov ah,09h
            int 21h

            mov ah,01h
            int 21h
            sub al,48
            mov num1,al

            mov ah,01h
            int 21h
            sub al,48
            mov num2,al     

            mov al,num1
            mul ten
            add al,num2

            mov readNum,al
            ret
        endp

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

            mov al,readNum
            mov ah,00
            div ten

            mov dl,ah
            mov t2,dl

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

            mov dl,t2
            add dl,48
            mov ah,02h
            int 21h
        endp

    mov ax,4c00h
    int 21h

    end 

Apart from what others have pointed out about not converting ASCII value to number I noticed that you are using too many variables unnecessarily. If you just need to take one number from the user and display it, you just need space to store that one number.

You might want to check out this page as a reference.
Hope it'll solve your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!