我想编写程序,提示用户从键盘输入一个字符。对于输入的字符,将字符分类为数字,字母 如果用户输入" *"然后再次循环获取新值 问题总是显示结果是数字,这在我输入字母时不对。我感谢任何帮助。
ORG $1000
*read user input
START:
MOVEQ #18,D0
LEA PR1,A1
TRAP #15
*COMPARING
MOVE.B D1,D2
CMP.B #$30,D2 ;if ch less than ASCII '0'
BGE number
CMP.B #$39,D2 ;check if ch greater than ASCII '9'
BLE number
ANDI #$DF,D2 ;CONVERT TO UPPERCASE
CMP.B #65,D2
BGE letter
CMP.B #90,D2
BLE letter
CMP.B #$2a,D1 ;if user enter *
BEQ START ;then loop again to enter new value
number LEA n,A1
JSR P_STR
MOVE.B #5,D0
TRAP #15
letter LEA l,A1
JSR P_STR
MOVE.B #5,D0
TRAP #15
PR1 DC.B 'Enter value: ',0
n DC.B 'Number',0
l DC.B 'Letter',0
INCLUDE 'sample.x68' ; it is the file Prints a string withCR/LF
END START
答案 0 :(得分:1)
你的逻辑错误:
CMP.B #$30,D2 ;if ch less than ASCII '0'
BGE number
CMP.B #$39,D2 ;check if ch greater than ASCII '9'
BLE number
这转化为:
if (ch >= 0x30 || ch <= 0x39) goto number;
你想要的是:
if (ch >= 0x30 && ch <= 0x39) goto number;
这看起来像是:
CMP.B #$30,D2
BLT not_number
; We've established that ch>=0x30, not make sure that it's also <=0x39
CMP.B #$39,D2
BLE number
not_number:
您的信件检查可能需要进行类似的更改;我没有chekc那部分代码。