0

I want to write program that prompt the user for one character from the keyboard. For the character entered, classify the character as a number, a letter If user entered "*" then loop again for new value The problem is always show the result is number, which not right when I enter letter. I appreciate any help.

             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
nas2016
  • 15
  • 5

1 Answers1

1

Your logic is incorrect:

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                  

This translates into:

if (ch >= 0x30 || ch <= 0x39) goto number;

What you want is:

if (ch >= 0x30 && ch <= 0x39) goto number;

Which would look something like:

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:

Your letter check might need a similar change; I didn't check that part of the code.

Community
  • 1
  • 1
Michael
  • 57,169
  • 9
  • 80
  • 125