本文介绍了汇编语言x8086-获取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个家庭作业问题,该问题要求我询问用户1位数到5位数之间的数字(例如,他们可以输入1、12、123、1234),我知道如何使用循环,然后使用mov ah,1h函数询问用户他们想要的数字,但是我想接受用户的输入(例如123),然后将该数字存储在我创建的变量Y中然后,我想处理它,我已经知道如何处理数字,但是只有当我已经在变量中声明了数字(Y dw 123)时,但是由于我必须要求用户输入,所以我保留未初始化的变量(Y dw?).现在,由于卡住了,我决定只创建它,而不是创建"Y dw?","Y dw 0,0,0,0,0",因此我可以手动将输入数字存储到该Y变量中./p>

基本上,我想知道如何将用户输入的每个数字存储在我的Y变量中,即使它是"Y dw 123",也可以在其中使用它

这是我到目前为止所拥有的:

title lab6 (lab6.asm)

.model small
.stack 100h
.data

    Y dw 0,0,0,0,0 ,0dh, 0ah
    W dw ?
    Sum dw ?
    printSum db "The Sum is: "
    sumMessage db 0,0,0,0,0     ,0dh, 0ah
    printW db "W is: "
    wMessage db 0,0,0,0,0   ,0dh, 0ah, '$'


.code
main proc
    mov ax,@data
    mov ds,ax

    mov bx, 0
        mov si, 1
        loop1:
            mov ax, 0
            mov ah, 1h
            int 21h
            cmp al, 0dh
            je endloop
            mov bl, al
            mov Y+si, ax
            inc si
        loop loop1
        endloop:
        mov ax, 0
       mov Y, bx



    mov ax,Y     ;Store Y in ax register
    sub ax,1
    mov Y, ax
    mov ax, 0

    mov Sum,36         ; add 36 to Sum
    mov bx,Y
    add Sum,bx         ; add 36 and Y into Sum
    mov ax,Y

    mov bx,4            ; take Y and divide by 4
    mov dx,0
    idiv bx
    add Sum,ax

    mov ax,Y           ;take Y and divide by 100
    mov bx,100
    mov dx,0
    idiv bx
    add Sum,ax

    mov bx,7
    mov dx,0                        ; calculate W
    idiv bx
    mov W,dx
    add W,1
    mov dx, W

    add dl, 30h
    mov wMessage+1, dl

    mov ax, 0
    mov dx, 0
    mov ax,Sum
    mov cx, 10            ;start modding the number 2553
    idiv cx

    mov si, 4

    sumLoop:                   ;Loop to mod and store 2553 into sumMessage
         add dl, 30h
         mov sumMessage+[si], dl
         mov dx, 0
         mov cx, 10
         idiv cx
         dec si
         cmp si, 0h
         je endSum

    loop sumLoop

    endSum:


    mov si, 0
    mov cl, printSum

    L1:                              ;Loop to print out "Sum is : 2553
        mov al, 0
        mov al, printSum[si]
        inc si
        cmp al, '$'
        je end_loop
        mov dl, al
        mov ah, 2h
        int 21h

    loop L1:

    end_loop:


    mov ax,4C00h
    int 21h

main endp
end main

对于我现在拥有的代码,如果我输入123作为用户输入,它给我的总和是:0098,而W是1,事实并非如此,总和实际上应该是0189,并且W是6.另外,我也很想知道如何取出开头的0.

这是此作业的说明:

编写一个计算以下内容的程序:

Y =(获取用户输入)

Y = Y-1

总和= 36 + Y +(Y/4)+(Y/100)

W =总和%7 + 1

输出W,总和

注意:您不得使用任何库函数

如果我的问题仍然不清楚,请告诉我,以便我可以尝试清楚地问我的问题,以便其他人可以理解.

谢谢!

解决方案

尝试类似这样的操作以输入数字:

        mov     cx, 10
        mov     bx, 0
loop1:
        mov     ax,0100h
        int     21h
        cmp     al,0dh
        je      endloop
        and     ax,0fh
        xchg    ax,bx
        mul     cx
        add     bx,ax
        jmp     loop1
endloop:
        mov Y, bx

I am stuck on a problem I have for a homework assignment that is asking me to ask the user fora digit ranging from 1 digit to 5 digits (eg. they can input 1, 12, 123, 1234) I know how to ask the user for whatever number they want, using a loop and then using the mov ah, 1h function, but I want to take the user's input, let's say 123, and then store that number in a variable that I've created, Y. Then I want to process it, I already know how to process the number, but only when I've already declared the number in the variable ( Y dw 123), but since I have to ask the user for an input, I have to leave the variable uninitialized ( Y dw ?). Now since I was stuck I decided to just create this instead of "Y dw ?", "Y dw 0,0,0,0,0", I did this so that I can manual store enter number into that Y variable.

Basically, I am wondering how I can take each number the user inputs and store it in my Y variable where I can use it just if it was "Y dw 123"

Here is what I have so far:

title lab6 (lab6.asm)

.model small
.stack 100h
.data

    Y dw 0,0,0,0,0 ,0dh, 0ah
    W dw ?
    Sum dw ?
    printSum db "The Sum is: "
    sumMessage db 0,0,0,0,0     ,0dh, 0ah
    printW db "W is: "
    wMessage db 0,0,0,0,0   ,0dh, 0ah, '$'


.code
main proc
    mov ax,@data
    mov ds,ax

    mov bx, 0
        mov si, 1
        loop1:
            mov ax, 0
            mov ah, 1h
            int 21h
            cmp al, 0dh
            je endloop
            mov bl, al
            mov Y+si, ax
            inc si
        loop loop1
        endloop:
        mov ax, 0
       mov Y, bx



    mov ax,Y     ;Store Y in ax register
    sub ax,1
    mov Y, ax
    mov ax, 0

    mov Sum,36         ; add 36 to Sum
    mov bx,Y
    add Sum,bx         ; add 36 and Y into Sum
    mov ax,Y

    mov bx,4            ; take Y and divide by 4
    mov dx,0
    idiv bx
    add Sum,ax

    mov ax,Y           ;take Y and divide by 100
    mov bx,100
    mov dx,0
    idiv bx
    add Sum,ax

    mov bx,7
    mov dx,0                        ; calculate W
    idiv bx
    mov W,dx
    add W,1
    mov dx, W

    add dl, 30h
    mov wMessage+1, dl

    mov ax, 0
    mov dx, 0
    mov ax,Sum
    mov cx, 10            ;start modding the number 2553
    idiv cx

    mov si, 4

    sumLoop:                   ;Loop to mod and store 2553 into sumMessage
         add dl, 30h
         mov sumMessage+[si], dl
         mov dx, 0
         mov cx, 10
         idiv cx
         dec si
         cmp si, 0h
         je endSum

    loop sumLoop

    endSum:


    mov si, 0
    mov cl, printSum

    L1:                              ;Loop to print out "Sum is : 2553
        mov al, 0
        mov al, printSum[si]
        inc si
        cmp al, '$'
        je end_loop
        mov dl, al
        mov ah, 2h
        int 21h

    loop L1:

    end_loop:


    mov ax,4C00h
    int 21h

main endp
end main

For the code that I have now if I enter 123 as the user input it gives me that the Sum is : 0098, and W is 1, which shouldn't be the case, the Sum should actually be 0189, and the W is 6. Also I was wondering how I would take out the leading 0's.

Here are the instructions for this assignment:

Write a program that computes the following:

Y = (Get user input)

Y= Y-1

Sum = 36 + Y + (Y/4) + (Y/100)

W = Sum % 7 + 1

Output W, Sum

Note: You may not use any library functions

If my question is still unclear please tell me so I may attempt to ask my question clearly so what others may understand.

Thanks!

解决方案

Try something like this to input a number:

        mov     cx, 10
        mov     bx, 0
loop1:
        mov     ax,0100h
        int     21h
        cmp     al,0dh
        je      endloop
        and     ax,0fh
        xchg    ax,bx
        mul     cx
        add     bx,ax
        jmp     loop1
endloop:
        mov Y, bx

这篇关于汇编语言x8086-获取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:41