【发布时间】:2021-10-15 17:57:05
【问题描述】:
我正在尝试在 MASM 中为 ASCII 编写一个简单的 toUpper 过程。但是,只要遇到空白字符,它就可以工作,它不会继续更改该字符及其后面的字符的大小写。我无法弄清楚发生这种情况的原因。 所以我所做的是放置一个条件语句来测试一个字符是否是一个空格 ' ' (32) 并因此跳过它。这足够了吗,还是我应该测试任何不在 97 和 122 之间的值?
; EDX - INPUT - The offset to the BYTE Array
; ECX - INPUT - The Size of the BYTE Array
; EDX - OUTPUT- The offset to the new BYTE Array toUpper PROC
PUSH ECX ; we will modify this so push it to the stack
DEC ECX ; we don't want the null terminating char
loopMe:
MOV AL, [EDX] ; otherwise move what the EDX points to to AL
CMP AL, 32
JNE goodChar
INC EDX ; increment the pointer by one byte since this is a space
loop loopMe ; go to the next char
goodChar:
AND AL , 11011111b ; make AL capital
MOV [EDX], AL
INC EDX ; increment the pointer by one byte
LOOP loopMe
done:
POP ECX ; return it back to its original value
RET toUpper ENDP
【问题讨论】:
-
代码中的一个错误是,如果字符串中的最后一个字符是空格,那么您将陷入
goodChar部分并执行这些指令。另请注意,直接编写 ASCII 代码是没有意义的。汇编器理解CMP AL, ' '就好了。 -
只有小写字符才能使逻辑到达
goodChar。代替CMP AL, 32和紧随其后的内容,您可以使用cmp al, 'a'; jb skip; cmp al,'z'; ja skip,然后skip:将在您的代码中当前第二个INC EDX之前。 -
请参阅What is the idea behind ^= 32, that converts lowercase letters to upper and vice versa? 以获得更好的用于检查字母字符的 asm 算法。另请参阅Convert a String In C++ To Upper Case 了解逻辑,How to access a char array and change lower case letters to upper case, and vice versa 了解汇编中的类似逻辑,这比两个单独的 cmp/jb cmp/ja 检查更有效。