【发布时间】:2021-11-27 12:59:41
【问题描述】:
我正在尝试检查一个字符串是否为回文。我正在尝试为此使用堆栈,即,将字符串推入堆栈并将其弹出另一个字符串,然后将它们两者进行比较。但我的函数最终总是说“不是回文”,即使它是。
编辑:我将 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
【问题讨论】:
-
你试过单步调试吗?对于初学者,请尝试一个非常小的示例,例如空字符串或只是“a”,然后是“aa”或“ab”。
-
根据您输入字符串的方式,最后可能有一个零终止符。该代码似乎可以工作,即使它过于复杂。
标签: assembly masm palindrome irvine32