简体   繁体   English

如何在NASM程序集中检查数字是否代表大写字符?

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

Suppose that you have a number stored in EAX . 假设您有一个数字存储在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. 我最接近的想法是创建一个大写字符数组('A','B','C,'D',...),然后检查EAX是否等于这些字符中的任何一个。 Is there a simpler way to do this in NASM Assembly? 在NASM组件中,有没有更简单的方法可以做到这一点?

I'm using 64-bit CentOS, for a 32-bit program. 我使用的是32位程序的64位CentOS。

If your character is encoded in ASCII then you could just check EAX is in the range 65 to 90 ('A' to 'Z'). 如果您的字符是用ASCII编码的,则只需检查EAX的范围是65到90(“ A”到“ 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. 对于其他编码(以Unicode为基数,请考虑变音符号),我认为答案根本不容易,您最终应使用操作系统中的API。

For ASCII characters, something like this would work: 对于ASCII字符,类似的方法将起作用:

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 : 假设您可以破坏al ,则是Michael的答案的较简单版本:

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. 如果要分支,请根据setcjcjnc替换setc

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

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