如何在汇编中从用户输入多位数? [英] How to input from a user multi digit number in assembly?
问题描述
我需要找到一个可以从用户那里接收超过 1 位数字的中断.;代码
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
组合.当用户按下q"时,无限循环停止.然而,我几乎确信我看到过一段用中断来做同样事情的代码.
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
推荐答案
在大多数情况下,如果将输入作为字符串接收然后转换为整数,这会变得容易得多.int 21h/ah=0ah
可以将缓冲的输入读入指向 DS:DX
的字符串.
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
.
一旦有了它,您就可以将该字符串转换为整数.这听起来像是一个家庭作业问题,所以这里不是给你代码,而是一个高级算法,用于将包含基数为 10 的数字的 ASCII 字符串转换为实际整数(伪代码):
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++
健壮的代码也会检查溢出和无效字符.你在这里很幸运,因为在 ASCII 中,代表数字的字符 ('0'...'9') 是连续存储的,而 x86 有一个 FLAGS
寄存器,您可以检查是否溢出.
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.
这篇关于如何在汇编中从用户输入多位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!