Reputation: 123
I have to write a program that
I want to use a stack to accomplish this. So here is my logic.
Starting from the end of the string compare that character with the character that is to be removed. If it isn't the character push it on to the stack. If it is ignore it and move on through the string. Then starting from the beginning pop everything into place.
I'm supposed to use a procedure to accomplish this. When I'm stepping through everything seems to be working ok until I attempt to leave the procedure and return to main. I'm fairly sure my logic in my procedure is the problem. Right now when I attempt to work with the string "The" and remove the e I get "he".
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
Figured it out.
Answer:
I was not dealing with getting a character from the console correctly. I also was not dealing with the case when ecx = 0. This is the first position of the character array. So I was not comparing the correct character, and not pushing the first character onto the array when necessary. I have fixed it by removing
mov edx, OFFSET char
mov ecx, 1
call readString
and replacing it with
call readChar
mov char,al
then adding this after the L1 loop.
movzx edx , string[ecx]
cmp dl,char
jz L4
push edx
inc eax
L4:
It now works as designed. I just have some formatting issues to clear up.
Upvotes: 2
Views: 6375
Reputation: 123
Answer:
I was not dealing with getting a character from the console correctly. I also was not dealing with the case when ecx = 0. This is the first position of the character array. So I was not comparing the correct character, and not pushing the first character onto the array when necessary. I have fixed it by removing
mov edx, OFFSET char
mov ecx, 1
call readString
and replacing it with
call readChar
mov char,al
then adding this after the L1 loop.
movzx edx , string[ecx]
cmp dl,char
jz L4
push edx
inc eax
L4:
It now works as designed. I just have some formatting issues to clear up.
Upvotes: 1