title: Performing a Backspace in YASM or NASM assembly
body: <p>I have a program reading characters in raw mode. That is, any characters input are read immediately instead of being buffered.</p>
  
  <p>I would like to know how to perform a backspace. That is, when I press the Backspace key, it should delete the character on the left and move the cursor one place to the left. </p>
  
  <p>I have tried outputting a Backspace character followed by a Space character. This deletes the character on the left, but moves the cursor <i>two</i> spaces to the right, for some reason.</p>
  
  <p>I have also tried outputting the Backspace character by itself. This moves the cursor one space to the left, but doesn't delete the character.</p>
  
  <p>My assembler is YASM and I am using 64-bit Linux. Below is some relevant code.</p>
  
  <p>First, here's the function I use to output a character:</p>
  
  <pre><code>printchar:                       
          mov     [buf], al      ; Backup whatever was in al.
                                 ; buf is declared as resb 1 in section .bss
          mov     edx, 1
          mov     ecx, buf
          mov     ebx, 1
          mov     eax, 4
          int     0x80
          mov     al, [buf]    ; restore char that was previously in al
          ret
  </code></pre>
  
  <p>The following code prints a Backspace. It doesn't move the cursor one place to the left, like your regular Backspace does.</p>
  
  <pre><code>    mov     al, 0x08 ; ASCII for Backspace
      call    printchar
  </code></pre>
  
  <p>This code prints a Backspace followed by a Space. It moves the cursor TWO spaces to the right instead of just one.</p>
  
  <pre><code>    mov     al, 0x08 ; ASCII for Backspace
      call    printchar
      mov     al, 0x20 ; ASCII for Space
      call    printchar
  </code></pre>
  
  <p>Len suggested printing a Backspace, followed by a Space, followed by a Backspace. This seems to work:</p>
  
  <pre><code>    mov     al, 0x08 ; ASCII for Backspace
      call    printchar
      mov     al, 0x20 ; ASCII for Space
      call    printchar
      mov     al, 0x08 ; ASCII for Backspace
      call    printchar
  </code></pre>
  
  <p>Please note that, as I previously mentioned, I am reading characters in raw mode. Thanks in advance!</p>