繁体   English   中英

68k汇编语言的决策结构

[英]Decision Structures in 68k assembly language

我想编写一个程序来提示用户从键盘输入一个字符。 对于输入的字符,将字符分类为数字,字母如果用户输入“ *”,则再次循环输入新值问题总是显示结果是数字,当我输入字母时不正确。 感谢您的帮助。

             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

您的逻辑不正确:

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, now also make sure that it's also <=0x39                  
CMP.B   #$39,D2                 
BLE     number
not_number:

您的信函支票可能需要进行类似的更改; 我没有检查代码的那部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM