【问题标题】:How can I test if an input char is between 1-9?如何测试输入字符是否在 1-9 之间?
【发布时间】:2017-02-25 22:52:43
【问题描述】:

我正在用 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

【问题讨论】:

  • 是什么部分导致了您的问题?你知道比较、条件分支和ASCII码吗?
  • 我知道条件分支,但不熟悉比较。
  • 然后阅读关于cmp的参考页面。 TL;DR:你可以只做类似cmp bl, '1' 的事情,然后使用你已经知道的条件分支。

标签: assembly nasm x86-16


【解决方案1】:

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

【讨论】:

  • jl 已签名,ja 未签名。这将起作用,因为您不需要从输入到工作的值高于 127,但是在阅读源代码时它看起来像错误,所以它会让我很烦恼,因为我每次都必须考虑它是如何工作的。 jb 未在“下方”签名。
  • 通过将AL 移动到BL 两个比较之后(使用AL),您将获得免费的代码大小优化。
  • @Ped7g:谢谢。你当然是对的。我修正了我的错误。
  • @SepRoland:当然你也是对的。但我的目标是尽可能少地改变问题的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 2019-07-27
  • 2021-04-21
  • 2022-01-11
  • 2017-09-08
相关资源
最近更新 更多