Search code examples
assemblyinputx86-16operations

Assembly Language x8086 - Getting User input


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!


Solution

  • 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