[英]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.我需要找到一个可以从用户那里接收超过 1 位数字的中断。 ;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.当用户按下“q”时,无限循环停止。 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
. int 21h/ah=0ah
可以将缓冲的输入读入指向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):这听起来像是一个家庭作业问题,所以这里没有给你代码,而是一个高级算法,用于将包含以 10 为基数的数字的 ASCII 字符串转换为实际整数(伪代码):
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.你在这里很幸运,因为在 ASCII 中代表数字的字符('0'...'9')是连续存储的,并且 x86 有一个FLAGS
寄存器,您可以检查是否溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.