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!
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!