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

冷暖自知 提交于 2019-12-13 04:47:07

问题


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.


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/19533884/how-to-check-if-a-number-represents-an-uppercase-character-in-nasm-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!