简体   繁体   中英

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

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-10 into an actual integer (pseudocode):

accum = 0
i = 0
while(string[i] != '\r')
    accum *= 10
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM