我正在检查字符串是否是回文。我正在尝试使用堆栈,也就是,将字符串推到堆栈上,并将其弹出到另一个字符串中,然后将它们进行比较。但我的函数总是说‘不是回文’,即使是这样。
编辑:我把str1作为用户的输入。
str1 BYTE 30 DUP('$')
下面是我写的函数
checkPalindrome PROC
pop address
mov esi , offset str1
mov ecx, lengthof str1
;push till last index of str1
L1:
cmp BYTE PTR [esi], '$'
je exitLoop
push [esi]
inc esi
loop L1
exitLoop:
mov edi, 0
sub ecx, 30
neg ecx
mov lengthStr, ecx
sub ecx, 1
L2:
pop eax
mov str2[edi], al
inc edi
loop L2
mov str2[edi], '$'
;this displays nothing when i assemble
mov edx, offset str2
call writeString
mov esi, offset str1
mov edi, offset str2
mov ecx, lengthStr
sub ecx, 1
L0:
mov eax, [esi]
mov ebx, [edi]
cmp al, bl
jne notPalind
inc esi
inc edi
loop L0
isPalind:
mov edx, offset isPalindrome
call writeString
jmp quit
notPalind:
mov edx, offset notPalindrome
call writeString
quit:
push address
ret
checkPalindrome ENDP发布于 2021-11-27 16:18:47
Irvine32不执行以$结尾的字符串。那是个无聊的东西!
给出你所有的定义,str1 BYTE 30 DUP('$'),并采取了。在输入"ABBA“时,缓冲区如下所示:
65, 66, 66, 65, 0, 36, 36, 36, 36, 36, ...第一个循环将在堆栈找到“$”字符之前将5项推到堆栈中。
00000041
00000042
00000042
00000041
00000000 <-- ESP子ecx,30 neg ecx mov lengthStr,ecx sub,1
上面的计算将设置lengthStr=5,您发现它已经比实际的输入多了1,因此您减去了1。尽管如此,这还是没有帮助的,因为堆栈仍然包含5个项,而第一个得到的项将是终止零,这会导致回文比较的混乱。
在您的$结束后,str2将是这样的:
0, 65, 66, 66, 34你写过关于str2的文章“当我组装的时候,这显示不出任何东西”。这是因为Irvine32只看到一个空字符串。以0开头的字符串。
检查回文将失败,因为这是两个字符串最后的样子(您比较的部分):
str1 65, 66, 66, 65
str2 0, 65, 66, 66解决方案
将cmp BYTE PTR [esi], '$'转换为cmp byte ptr [esi], 0
删除sub ecx, 1
将mov str2[edi], '$'转换为mov str2[edi], 0
删除sub ecx, 1
https://stackoverflow.com/questions/70135009
复制相似问题