简体   繁体   English

检查字符串是否为M68k中的字母数字

[英]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). 对于我的任务,您不能使用结构化的控制命令(如果-然后-其他-Endif;如果-然后-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) 以下是我用来检查字符范围的内容(A0包含我正在检查的字符串结尾的地址,而D0是字符串的长度)

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. 用于30到7A之间的13个左右的非字母字符。

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. 检查第一个D1以查看它是否包含数字。 If it doesn't, we check if it contains 'A'..'Z' or 'a'..'z' . 如果没有,我们检查它是否包含'A'..'Z''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' . 大写字母和小写字母之间的唯一区别是,将第5位设置为小写字母,因此我们使用ANDI指令清除了第5位,因此我们只需要与'A'..'Z'进行比较。

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

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