2014-11-09 73 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

相關問題