2
我必須寫一個程序,從一個字符串中刪除的字符在MASM
- 需要從鍵板緩衝液中的字符串並把它放入一個字符數組
- 要求一個字符以除去
- 從字符數組中移除字符,同時移動其他所有內容
我想使用堆棧來實現此目的。所以這是我的邏輯。
從字符串的末尾開始,將該字符與要刪除的字符進行比較。如果不是角色將它推到堆棧上。如果它忽略它並繼續穿過字符串。然後從一開始就把所有東西都放到位。
我應該使用一個過程來完成這個。當我正在逐步完成時,似乎一切正常,直到我試圖離開程序並返回主程序。我很確定我的程序中的邏輯是問題。現在,當我嘗試使用字符串「The」並刪除e時,我得到了「他」。
TITLE String Manipulation
INCLUDE Irvine32.inc
.data
prompt byte "Please enter a string to manipulate : ",0
prompt2 byte "Please enter a character to remove: ",0
answerMSG byte "The new string is: ",0
string BYTE 51 DUP (0)
char BYTE ?
byteCount dword ?
.code
main PROC
call clrscr
push eax ;perserve the registers
push ecx
push edx
mov edx, OFFSET prompt ;prints the prompt
call writeString
mov edx, OFFSET string ;moves the register to the first location for the string
mov ecx, SIZEOF string ;Sets the max characters
call readString
mov byteCount,eax ;places actual count into a counting register
call crlf
mov edx, OFFSET prompt2 ;prints the prompt
call writeString
mov edx, OFFSET char
mov ecx, 1
call readString
call clrscr
mov ecx, byteCount
mov edx, OFFSET string
call stringMan
mov edx, OFFSET string
call writeString
pop edx
pop ecx
pop eax
main ENDP
;
stringMan PROC USES eax ecx edx
mov eax,0
L1:
movzx edx , string[ecx]
cmp dl, char
jz L2
push edx
inc eax
L2:
mov string[ecx],0
LOOP L1
mov ecx,eax
mov eax,0
L3:
pop edx
mov byte ptr string[eax],dl
inc eax
loop L3
ret
stringMan ENDP
END main
想通了。
回答:
我沒有正確處理從控制檯獲取角色。當ecx = 0時,我也沒有處理這種情況。這是字符數組的第一個位置。所以我沒有比較正確的字符,也沒有在必要時將第一個字符推到數組上。我已經通過去除
mov edx, OFFSET char
mov ecx, 1
call readString
並用
call readChar
mov char,al
則L1循環之後加入此替換它固定它。
movzx edx , string[ecx]
cmp dl,char
jz L4
push edx
inc eax
L4:
它現在按設計工作。我只是有一些格式問題需要解決。
將答案添加爲答案,而不是對問題的編輯。 – hjpotter92
是的,我會有,但作爲一個新用戶,堆棧溢出不會讓我在發佈後儘快回答我自己的問題。 –