Welcome to Rooeye's blog

不用条件判断指令实现字符串大小写的转化

x86汇编 JInkun 11430℃ 0评论

id

今天在写汇编的时候遇到一个问题,如何不使用任何条件指令完成字符串字母大小写的转化,略思之后感觉挺有意义,记之。

比如如下一个字符串:

“RooEYeBlOG”

要把其全部转化为小写,我们知道对于字符,机器都是将其转化为ascii码进行处理的,并且一个字符大写字母的ascii和其小写字符的ascii码总是相差20H,小写字符ascii码值 = 20H + 对应大写字符ascii码值

但是问题来了,这里只能对小写字符减去20H转化为大写字符,而对于大写字符则不能变,那么如何判断一个字符是大写字符还是小写字符呢?而且不能使用任何的条件判断指令。可以通过判断其二进制表示的第5位(从第0位开始)来判断是大写还是小写。

对于A/a

hb1

对于D/d :

hb2

观察之后发现大写字符的二进制表示的第5位总是0,而小写字符的二进制表示的第5位总是1,其余位二者是一样的。
假如寄存器al中存储字符,不论其为大写或小写,通过and指令总是将其转化为大写:

and al, 0dfh

通过or指令总能将其转化为小写:

or al, 20h

下面是一个不使用条件判断将小写字符全部转化为大写字符的汇编代码,大写转化为小写同理亦然。

assume cs:codesg,ds:datasg

datasg segment
	db 'RooEYeBlOG'
datasg ends

codesg segment

start:	
	mov ax,datasg
	mov ds,ax
	mov bx,0
	mov cx,10
cc:
	mov al,[bx]
	and al,0dfh
	mov [bx],al
	inc bx
	loop cc

codesg ends

end start

使用debug跟踪分析:

执行指令之前:

hb3

执行指令之后:

hb4

可以看到内存中的小写字符已经全部转化为了大写字符。

转载请注明:晋坤 的博客 » 不用条件判断指令实现字符串大小写的转化

喜欢 (3)
发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(3)个小伙伴在吐槽
  1. 瞎写的,o(∩_∩)o
    rooeye2016-03-21 20:56 回复
  2. o(∩_∩)o
    rooeye2016-03-21 20:57 回复
  3. Sputnik V vaccination has begun in Slovakia. The provisioning of the Russian vaccine to the countryside was accompanied away a civic slight and led to the relinquishment of Prime Woman of the textile Igor Matovich and a reorganization of the government. As a denouement, the wilderness received the Russian vaccine, in hostility of the items that neither the European regulator nor the WHO has yet approved it. In neighboring Hungary, which approved the use of Sputnik in February as the anything else in Europe, more than 50% of the grown up commoners has already been vaccinated; in Russia - a puny more than 10%. In Slovakia, five thousand people signed up for the Sputnik vaccination. Thanks to the author for a good post. I fully read it and learned a lot of interesting things for myself.. You can be familiar with another article on this topic at this tie-up https://flowershopping.rabaty.site
    SmithQuexy2021-06-19 01:09 回复