2

I have a program reading characters in raw mode. That is, any characters input are read immediately instead of being buffered.

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.

I have tried outputting a Backspace character followed by a Space character. This deletes the character on the left, but moves the cursor two spaces to the right, for some reason.

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.

My assembler is YASM and I am using 64-bit Linux. Below is some relevant code.

First, here's the function I use to output a character:

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

The following code prints a Backspace. It doesn't move the cursor one place to the left, like your regular Backspace does.

    mov     al, 0x08 ; ASCII for Backspace
    call    printchar

This code prints a Backspace followed by a Space. It moves the cursor TWO spaces to the right instead of just one.

    mov     al, 0x08 ; ASCII for Backspace
    call    printchar
    mov     al, 0x20 ; ASCII for Space
    call    printchar

Len suggested printing a Backspace, followed by a Space, followed by a Backspace. This seems to work:

    mov     al, 0x08 ; ASCII for Backspace
    call    printchar
    mov     al, 0x20 ; ASCII for Space
    call    printchar
    mov     al, 0x08 ; ASCII for Backspace
    call    printchar

Please note that, as I previously mentioned, I am reading characters in raw mode. Thanks in advance!

InvalidBrainException
  • 2,312
  • 8
  • 32
  • 41

1 Answers1

5

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.

You're almost there! Try outputting a backspace, then a regular space to black out the character the cursor is now on top of, then another backspace to move the cursor back once again.

It seems like a hack, but it's actually quite common!

Asherah
  • 18,948
  • 5
  • 53
  • 72
  • Ok how could I have been so stupid as to not think of that!! :) Thanks a lot. By the way, why does outputting a Backspace followed by a Space result in moving the cursor ahead TWO spaces instead of one? Does this mean that a "real" ASCII Backspace does not move the cursor? – InvalidBrainException May 26 '12 at 12:09
  • @Terribad: that's very strange! I somehow glossed over that paragraph when giving my answer, but my only thought is that perhaps some other output is confusing the process somehow. If my suggested solution doesn't work, if you could supply some code that exhibits the issue, I'd be happy to try hacking around with it until it works. :) – Asherah May 26 '12 at 12:11
  • But it does work! I will update my original post with some code. – InvalidBrainException May 26 '12 at 12:12
  • Oh, great! I'll try without the second space and see if I can work out why it would move two spaces, I'm interested now. – Asherah May 26 '12 at 12:12
  • I think I worded my comments poorly. The cursor doesn't move two spaces. It just appears so because outputting a Backspace character doesn't actually move the cursor one space to the left. I tried to type an example here but the comments box doesn't seem to allow advanced formatting, so I deleted it. – InvalidBrainException May 26 '12 at 12:35
  • 1
    @Terribad: okay, I've given it a try with your code, when outputting a backspace and a space (and not another backspace)—[here's what it looks like on my terminal](http://dl.dropbox.com/u/52674764/Screenshots/iu2t.png). It's got an extra space, but to me, I was actually mislead for a second and thought there were two spaces, because the cursor being on an empty space of its own does give the impression that it's "empty" too (even though the cursor is always on an empty space). I'm not sure if you possibly ran into the same issue! – Asherah May 26 '12 at 13:35
  • (And no, you didn't word your comment poorly; actually, I understood exactly what you meant, but worded my comment-in-reply poorly! Two spaces right of the text we were backing up to is precisely what you meant/said/what I read.) – Asherah May 26 '12 at 13:36
  • Yes! That's exactly how it looks for me too. Glad to know I'm not crazy. I guess I'm used to thinking of Backspace as "delete the last character and move the cursor one space to the left", but I guess regular everyday Backspace is different from ASCII backspace. Thanks for your time! – InvalidBrainException May 27 '12 at 05:54
  • No probs! Glad I was able to help. :) – Asherah May 27 '12 at 05:59