2014-11-09 37 views
0

我有一个问题,让我的脑海里面如何存储用户从提示输入的8位无符号整数。我目前的代码是:存储一个8位无符号整数

lea dx, StrPrompt ;load prompt to display to the user 
    mov ah, 9h   ;display string subroutine 
    int 21h    ;interrupt for MS-DOS routine 

    mov ah, 1h   ;Read character subroutine (will be stored in al) 
    int 21h    ;Interrupt for MS-DOS 
    sub al, 30h   ;Translate al from ASCII code to number 
    mov num, al   ;Copy number to num (al will be overwritten later) 

    lea dx, StrMsg  ;display the results to the user 
    mov ah, 9h 
    int 21h 

    mov al, num   ;move the n value to the al 
    mov dl, al   ;display the number 
    add dl, 30h   ;add 30h to the ASCII table 
    mov ah, 2h   ;store interrupt code 
    int 21h    ;interrupt for MS-DOS routine 

现在的问题是,我每次运行这段时间只会让我进入像1,2,3,等我无法输入一个整数在一个双或三位数字像20或255.我怎么去解决这个问题?

+0

随着“中间体21/AH = 0AH”我们可以得到一个缓冲输入回路:[链接] HTTP:// WWW .ctyme.com/intr/rb-2563.htm – 2014-11-09 09:09:16

回答

0

您可以强制用户总是按3个键。体育数字25将需要“0”,“2”和“5”。
收到第一个密钥并将其翻译为[0,9]之后乘以100,然后再存储到NUM中。
收到第二个关键字并将它翻译为[0,9]之后,再乘以10,然后再添加到NUM。
接收到第三个密钥并将其翻译为[0,9]后,添加到NUM。

1
mov ah, 1h   ;Read character subroutine (will be stored in al) 

这表示它的内容正好一个字符。 20或255由两个和三个字符组成。 如果你想阅读多个字符,你必须把它放在一个循环中,或者使用上面注释中的其他API/INT-call。

循环变异可能是这样的 - 展开了多达三个字符

.data 
    num1 db 0 
    num2 db 0 
    num3 db 0 
    numAll dw 0 
.code 
    [...] 
mov ah, 1h   ;Read character subroutine (will be stored in al) 
int 21h    ;Interrupt for MS-DOS 
sub al, 30h   ;Translate al from ASCII code to number 
mov num1, al   ;Copy number to num (al will be overwritten later) 

mov ah, 1h   ;Read character subroutine (will be stored in al) 
int 21h    ;Interrupt for MS-DOS 
cmp al,13   ;check for return 
je exit1   ;if return, do not ask further and assume one digit 
sub al, 30h   ;Translate al from ASCII code to number 
mov num2, al   ;Copy number to num (al will be overwritten later) 

mov ah, 1h   ;Read character subroutine (will be stored in al) 
int 21h    ;Interrupt for MS-DOS 
cmp al,13   ;check for return 
je exit2   ;if return, do not ask further and assume two digits 
sub al, 30h   ;Translate al from ASCII code to number 
mov num3, al  ;Copy number to num2 (al will be overwritten later) 
[...] 
exit2: 
[...] 
exit1: 
+0

谢谢。这是解决这个问题的好方法。然而,我现在有一个问题是,如何将所有整数添加到numAll变量? – user2961971 2014-11-10 05:35:11

+0

我不确定您是否喜欢整数值或字符串作为结果。给出的解决方案输出一个从num1开始的字符串。 LEA EDX,num1和调用INT21零终止字符串输出将执行此操作。 – zx485 2014-11-11 12:04:05

相关问题