Pergunta

I need to a program in assembly to read a two digit number from the user, store it in a variable and later print it. I have tried a lot, but could not get through. Here is my coding.

  .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
        mov num1,al

        mul ten
        mov temp,al

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

        mov ah,01h
        int 21h
        mov num2,al
        mov dl,num2
        add dl,temp     
        mov readNum,dl
        ret
    endp

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

        mov al,readNum
        mov ah,00
        div ten

        mov t1,ah
        mov t2,al

        mov dl,t1
        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 

According to the above program, if I enter 42, it gives me the entered number as 85. I could not find the error in my program. Can somebody help me please.

Foi útil?

Solução

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 

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top