在装配读两位数字,并将其存储在变量中 [英] Reading a two digit number in assembly and storing it in a variable

查看:294
本文介绍了在装配读两位数字,并将其存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个程序汇编从用户读取两个数字,将其存储在一个变量稍后打印。
我已经尝试了很多,但打不通。
这里是我的编码。

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 

根据上述方案,如果我输入42,它给了我输入的号码为85。
我找不到我的程序错误。有人可以帮我请。

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.

推荐答案

看看下面的程序。我编辑了自己的之一。这里是已经被弗兰克·科特勒提到的一个小错误。那是你没有用户输入转换为数字。您已经添加48到所述用户输入。但是你必须减去它48。

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 

这篇关于在装配读两位数字,并将其存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆