Search code examples
assemblyx86masmirvine32

Input numbers with assembly language


I am trying to input two numbers from user in assembly language using Irvine32 library but don't know how. This is what I have so far:

INCLUDE Irvine32.inc
.data

number1 WORD
number2 WORD

.code
main PROC



exit
main ENDP
END main

Solution

  • i'm not familiar with irvine, but how about writing the input and decode routine yourself?

    DOS int 21/a reads a line from stdin, and put's it to your buffer

    the decoding from ascii to register is a but more tricky; you have to walk thoug each digit and add them one by one, by shifting the curent value

    here's an approach, just to have an idea: (sorry for syntax, max be incompatible to masm, I still use Eric Isaacson's A86 assembler)

    .org 0100
    JMP start
    
    buffer: db 10,"          "  ; space for 10 digits
    
    ; read input to buffer
    input:  mov ah, 0ah         ; input value
            mov dx, buffer
            int 21h
            ret
    
    ; decode buffer to CX
    decode: mov dx,0
            mov si, buffer+2
            mov cl, [buffer+1]    ; while (numChars>0)
    
    decLoop: cmp cl,0
            je decEnd
    
            mov ax, dx          ; mul DX by 10
            shl ax, 2
            add ax, dx
            shl ax, 1
            mov dx, ax
    
            mov al,[si]        ; get current digit
            inc si             ; and point to next one
            sub al,'0'
            mov ah, 0
            add dx, ax          ; add the digit read
    
            dec cl              ; numChars--
            jmp decLoop
    decEnd: ret
    
    
    ; main()
    start:  call input
            call decode
            push dx
            call input
            call decode
            pop cx
    
            ; CX now holds first, DX second number
            ; feel free to do with em what you feel like
    
            int 20h             ; quit