如何测试输入字符是否在1-9之间?
问题描述:
我正在写一个8086汇编语言的程序,要求1-9之间的单个数字,然后存储它。如果它不在1-9之间,则应该循环回去。如何测试输入字符是否在1-9之间?
什么是一种很好的方式来测试它,并让它循环回来(并允许你输入另一个数字),直到满足要求为止?
我迄今为止代码:
section .data
prompt1 db "Enter a single digit digit between 1-9 --> $"
section .text
;Display prompt
mov ah,9 ; print prompt
mov dx,prompt1 ; load register with prompt1
int 21h ; display it
; Input character and store.
mov ah,1 ; reach char fcn
int 21h ; read character into al
mov bl,al ; store character into bl
答
我没有测试过,但总的来说,代码应该检查是否BL
比39H小于31H或更大。这些是1
和9
的ASCII值。
所以一些示例代码看起来是这样的:
; Input character and store.
loop1: ; added label
mov ah,1 ; read char fcn
int 21h ; read character into AL
mov bl, al ; store character into BL
; now comes the additional code
cmp bl, 31h ; compare BL to the ASCII value of '1'
jb loop1 ; jump back if ASCII value is less than '1' = 31h
cmp bl, 39h ; compare BL to the ASCII value of '9'
ja loop1 ; jump back if ASCII value is greater than '9' = 39h
; BL contains an ASCII value between '1' and '9' which integer value can be acquired by subtracting the value 30h
哪一部分是你造成的问题?你知道比较,条件分支和ASCII码吗? – Jester
我了解条件分支,但不熟悉比较。 – user3394363
然后阅读关于'cmp'的参考页面。 TL; DR:你可以做一些像'cmp bl','1''然后使用你已经知道的条件分支。 – Jester