简体   繁体   中英

How to check if a number represents an uppercase character in NASM Assembly?

Suppose that you have a number stored in EAX . How can I check whether this number represents an uppercase character or not?

Frankly, I haven't tried anything. The closest idea I had was to create an array of upper case characters ('A','B','C,'D',...) and then check if EAX was equal to any of these. Is there a simpler way to do this in NASM Assembly?

I'm using 64-bit CentOS, for a 32-bit program.

If your character is encoded in ASCII then you could just check EAX is in the range 65 to 90 ('A' to 'Z'). For other encodings (Unicode in primis, think about diacritics) I think the answer is not trivial at all and you should eventually use an API from the OS.

For ASCII characters, something like this would work:

cmp eax,'A'
setnc bl    ; bl = (eax >= 'A') ? 1 : 0
cmp eax,'Z'+1
setc bh     ; bh = (eax <= 'Z') ? 1 : 0
and bl,bh   ; bl = (eax >= 'A' && eax <= 'Z')
; bl now contains 1 if eax contains an uppercase letter, and 0 otherwise

A somewhat simpler version of Michael's answer, assuming you can clobber al :

sub al, 'A'
cmp al, 'Z' + 1 - 'A'
setc al ; al now contains 1 if al contained an uppercase letter, and 0 otherwise

If you want to branch, then replace the setc with jc or jnc as appropriate.

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