如何测试输入字符是否在1-9之间?

时间:2017-02-25 22:52:44

标签: assembly nasm x86-16

我用8086汇编语言编写一个程序,要求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

1 个答案:

答案 0 :(得分:2)

我还没有测试过,但一般来说,代码应该检查BL是否小于31小时或大于39小时。这些是19的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