How do i check whether a character is between 0-9, A-Z, and a-z? I understand that you can use cmp char, 'A' or cmp char, '0', etc. However if i have to check three different ranges, how do i do that?
If i need to check whether 'A'<= C <= 'Z', then i would have to check whether the character value is below A first, and then whether it's less than or equal to Z. But since 0-9 are below A, how do i account for that without messing up logic? The same goes for Z, since a-z are above Z. Posting with my logic I have so far. I feel so dumb for not getting simple stuff, but i'm a beginner and I've been working on this for several days and now I'm having to start over again, so any help would be greatly appreciated.
_asm
{
mov ecx, 127
mov esi, 0
mov ebx,LocalBuffer[esi] ;LocalBuffer is a c++ array
Loop1:
cmp ebx, 'a' ;ebx is the 0'th index value of LocalBuffer
jb notLowercase ;If character value is below 'a'
cmp ebx,'z'
jbe CharCount ;if it's less than or equal to 'z'
cmp ebx,'A'
jb notUpperCase ;If less than 'A', but then won't this discard 0-9?
cmp ebx,'Z'
jb CharCount ;If it's less than 'Z', but what about greater than Z?
cmp ebx,'0'
jb NotDigit ;If less than '0'
cmp ebx,'9'
jb CharCount ;What if it's greater than 9?
notLowerCase:
;DO I LOOP BACK TO LOOP1, MOVE ON TO THE NEXT CHARACTER OR SOMETHING ELSE?
notUpperCase:
;SAME ISSUE AS NotLowerCase
notDigit:
;SAME ISSUE AS LAST 2
CharCount:
;Do something
An easy approach is to order the ranges in an ascending (or descending) way. Then you can use the
cmps in an ON/OFF style:As you can see, the values that are checked do ascend. For example, a value of
3(=51) will first check if it is below 48 (=NO), then check if it is below 57 (=YES), so the second jump is taken.An alternative is using a jump table with indexed addressing. In this approach you define the ranges as a table of boolean values (0=NotInSet, 1=CharCount):
The table should be set up like this in the
.datasegment for your scenario (note the alternating values of0and1, the ON/OFF style mentioned above):Then the code could look like this:
The table has 256 values, the full ASCII range, either 0 or 1.
In both cases you can move the
inc esito the beginning, right after the value has been read bymovzx ebx, byte ptr LocalBuffer[esi].