存储8位无符号整数

时间:2014-11-09 05:30:57

标签: assembly

我正在解决如何存储用户从提示输入的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我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

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

这里说它读取完全一个字符。 20或255由两个和三个字符组成。 如果您想要读入多个字符,则必须将其置于循环中或使用上述注释中的其他API / INT调用。

循环变体可能如下所示 - 循环展开最多三个字符

.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:

答案 1 :(得分:0)

您可以强迫用户始终按3键。体育数字25需要" 0"," 2"和" 5"。
收到第一把钥匙并将其翻译成[0,9]乘以100后再存入NUM 收到第二把钥匙并将其翻译成[0,9]乘以10后再加上NUM 收到第三个密钥并将其翻译为[0,9]后添加到NUM。