title: Assembly - Program works like expected, but when ran a second time, prints gibberish
body: <p>I'm making a program in TASM assembly (i honestly have no idea if it's 16 bit, x86, 64 bit maybe 8086?. I'm using TASM inside DOSBox for assembling, linking, and testing.</p>
  
  <p>The user inputs characters until the user presses <kbd>Enter</kbd> key. then echos what they typed back to the console. This is my code:</p>
  
  <pre><code>IDEAL
  model small
  STACK 100h
  
  DATASEG
  wholeinput db 00
  
  CODESEG
  start:
  
  mov ax,@data
  mov ds,ax
      mov bx, 0 ; bx is the counter
  
      input:
      mov ah, 1 ; input moves the input to al
      int 21h
  
      cmp al, 13 ; 0xD is enter key
      je enterPressed ; if its not 'enter' then continue
      add [wholeinput+bx], al ; add the given character to the end of the string
  
      inc bx    ; inc counter of string length
      jmp input ; continue loop
  
  
      enterPressed:
      add [wholeinput+bx], '$' ; add to the last byte of the thing a string terminator
      mov ah, 9
      mov dx, offset wholeinput ; print the whole thing
      int 21h
  
  
  exit:
  mov ax,4c00h
  int 21h
  END start
  </code></pre>
  
  <p>The first time I run the program it works as expected. When I run the same program a second time with the same input it prints gibberish to the console.</p>
  
  <p>My theory is that the memory from my previous run is somehow copied to the next run. this could be a very stupid thing to say but I'm a noob...</p>
  
  <p><a href="https://i.stack.imgur.com/lWA1j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lWA1j.png" alt="Screenshot"></a></p>
  
  <p>What could be my problem? How can I solve it?</p>
  
  <p>Edit:
  Thanks to Mike Nakis and Peter Cordes for this solution:<br>
  The problem was that I did not reserve enough space for the input.</p>
  
  <blockquote>
    <p>wholeinput db 00    </p>
  </blockquote>
  
  <p>only reserve one byte. Fix:     </p>
  
  <blockquote>
    <p>wholeinput db 100 dup(0)     </p>
  </blockquote>