vs2017环境中使用汇编语言实现字符串中大小写字母转换
伪代码描述
for(i = 0; i < n; i++)
{
if(msg[i] >= 'A' && msg[i] <= 'Z')
{
msg[i] += 32;
continue;
}
if(msg[i] >= 'a' && msg[i] <= 'z')
{
msg[i] -= 32;
}
}
汇编代码实现
include vcIO.inc
.data
msg byte "Hello, gensokyo123",10, 0
frmStr byte "before change: %s", 10, 0 ;format
frmStr1 byte "after change : %s", 10, 0 ;format
.code
main proc
mov ecx, lengthof msg
mov esi, offset msg
pushad
invoke printf, offset frmStr, offset msg
popad
again:
mov dl, [esi] ;judge if msg[i] >= 'A' && msg[i] <= 'Z'
cmp dl, 'A' ;if msg[i] < 'A', then continue
jb next
cmp dl, 'Z' ;if msg[i] >'Z', then judge if msg[i] >= 'a' && msg[i] <= 'z'
jg lower
add dl, 20h
jmp next ;continue
lower:
cmp dl, 'a' ;judge if msg[i] >= 'a' && msg[i] <= 'z'
jb next
cmp dl, 'z'
jg next
sub dl, 20h
next:
mov [esi], dl
inc esi
loop again
pushad
invoke printf, offset frmStr1, offset msg
popad
ret
main endp
end main
实验结果
![实验结果](https://i-blog.csdnimg.cn/blog_migrate/3b07cdc04145db41e2caa5975bced5d4.png)