简体   繁体   中英

Checking if a string is alphanumeric in M68k

I feel like this is something that is relatively easy to do, but I have no idea what to do. For my assignment you cant use the structured control commands (If - Then - Else - Endif; If - Then - Endif). So I have to use branches. Below is what I've been using to check bounds on characters (A0 contains the address of the end of the string I'm checking, and D0 is the length of the string)

str_chk  MOVE.B     -(A0),D1     ; get current character from memory
         CMP.B      #$30,D1      ; check if character is less than ASCII '0'
         BLO        err_range
         CMP.B      #$39,D1      ; check if character is greater than ASCII '9'
         BHI        err_range
         SUBQ       #1,D0
         BNE        str_chk

since lowercase and uppercase letters are above this range they will result in an error. Is there something that I can do to get around this? Should I just have gross code and have a bunch of statements like

CMP.B    #$3A,D1
BEQ      err_range

for the 13 or so non-letter characters between 30 and 7A.

How about something like this:

str_chk  MOVE.B     -(A0),D1     ; get current character from memory
         CMP.B      #$30,D1      ; check if character is less than ASCII '0'
         BLO        err_range
         CMP.B      #$39,D1      ; check if character is greater than ASCII '9'
         BLS        is_alnum
         ; It wasn't a digit. Check if it's in the range 'A'..'Z' or 'a'..'z'
         ANDI       #$DF,D1      ; convert to uppercase
         CMP.B      #65,D1       ; 'A'
         BLO        err_range    ; > '9' but < 'A': not alphanumeric
         CMP.B      #90,D1       ; 'Z'
         BHI        err_range    ; > 'Z': not alphanumeric
is_alnum
         SUBQ       #1,D0
         BNE        str_chk

First D1 is checked to see if it contains a digit. If it doesn't, we check if it contains 'A'..'Z' or 'a'..'z' . The only difference between upper- and lowercase letters is that bit 5 is set for the lowercase ones, so we clear bit 5 with the ANDI instruction so that we only need to compare against 'A'..'Z' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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