title: Removing a character from a string in MASM
body: <p>I have to write a program that</p>
  
  <ul>
  <li>takes a string from the key board buffer and puts it into a character array</li>
  <li>asks for a character to remove</li>
  <li>removes the character from the character array while shifting everything else over</li>
  </ul>
  
  <p>I want to use a stack to accomplish this. So here is my logic.</p>
  
  <p>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.</p>
  
  <p>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". </p>
  
  <pre><code>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
  </code></pre>
  
  <p>Figured it out.</p>
  
  <p>Answer:</p>
  
  <p>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</p>
  
  <pre><code>mov edx, OFFSET char
  mov ecx, 1
  call    readString
  </code></pre>
  
  <p>and replacing it with</p>
  
  <pre><code>call readChar
  mov  char,al
  </code></pre>
  
  <p>then adding this after the L1 loop.</p>
  
  <pre><code>movzx   edx , string[ecx]
  cmp dl,char
  jz  L4
  push edx
  inc eax
  L4:
  </code></pre>
  
  <p>It now works as designed. I just have some formatting issues to clear up.</p>