我正在嘗試檢查字串是否為回文。我正在嘗試為此使用堆疊,即,將字串推入堆疊并將其彈出到另一個字串中,然后將它們兩個進行比較。但是我的函式最終總是說“不是回文”,即使它是。
編輯:我將 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
uj5u.com熱心網友回復:
Irvine32 不處理以 $ 結尾的字串。那是 DOS 的東西!
鑒于您的 all-$ 定義str1 BYTE 30 DUP('$'),并以例如。“ABBA”的輸入,緩沖區看起來像:
65, 66, 66, 65, 0, 36, 36, 36, 36, 36, ...
您的第一個回圈將在找到 '$' 字符后退出之前將 5 個專案推入堆疊。
00000041
00000042
00000042
00000041
00000000 <-- ESP
sub ecx, 30 neg ecx mov lengthStr, ecx sub ecx, 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://www.uj5u.com/shujuku/369163.html
上一篇:在陣列中存盤整數的MIPS問題
