Daniel Bartov
Daniel Bartov

Reputation: 177

How to input from a user multi digit number in assembly?

I need to find an interrupt that can receive from a user a number with more than 1 digit. ;code

mov [0],0
mov si,0
lop:    
    mov ah,1
    int 21h
    cmp al,'q'
    je finishedInput
    xor ah,ah 
    add [word ptr si], ax
    jmp lop

finishedInput:

I have already tried to do an endless loop that each time uses the

mov ah,1 
int 21h 

combination. When the user press 'q' the endless loop stops. However, I am almost convinced that I have seen a code that do the same thing with interrupt instead.

I want to stop using this block and use short interrupt that does the work better

Upvotes: 4

Views: 1758

Answers (1)

Govind Parmar
Govind Parmar

Reputation: 21542

In most cases, it makes it a lot easier if input is taken in as a string and then converted to an integer. int 21h/ah=0ah can read buffered input into a string pointed to at DS:DX.

Once you have that, you can take that string and convert it to an integer. This sounds like a homework problem, so rather than give you code for it, here is a high level algorithm for converting a string of ASCII characters containing a number in base-N into an actual integer (pseudocode):

accum = 0
i = 0
while(string[i] != '\r')
    accum *= N
    accum += (string[i] - '0')
    i++

Robust code would check for overflow and invalid characters as well. You're in luck here, since in ASCII the characters representing numbers ('0'...'9') are stored consecutively, and the x86 has a FLAGS register that you can check for overflow.

Upvotes: 5

Related Questions