4

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

  • 4
    There isn't a DOS system call for it, or BIOS interrupt. You need a loop, or string input and convert to integer yourself. See https://stackoverflow.com/tags/x86/info (search for multi-digit number in that page) – Peter Cordes Feb 14 '19 at 18:03

1 Answers1

5

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.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • For future readers, there's a NASM x86-64 version on [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057), trivially portable to 32-bit. (Or with some tweaking, to 16-bit, and with even more tweaking to 8086 to avoid `movzx` and `imul ax, 10`) – Peter Cordes Jun 12 '21 at 00:32