2

I am making a game in assembly using EMU8086 for a project in school. In this game I need to allow the user to enter a string in order to progress. While he enters the string he might type something incorrectly and use the backspace to correct it. The problem is that the backspace moves the cursor over the previous character, but the character previously entered remains. Why isn't the backspace clearing the previous character? How can I fix my program so that the previous character on the screen is removed?

My code is:

data segment

ends

stack segment
dw   128  dup(0)
ends    
StringHelper db 20 dup(?)
Line db 13,10,'$'
FullInput db 'You cant type more than 20 letters!!! please try again!!',13,10,'$'
t db '$'
code segment
PROC PrintMessage
  ;BX MUST have OFFSET OF MESSAGE
  ; if you want to go down a line do (lea bx,line)      
  mov dx,bx      
  mov ah,09h
  int 21h     
  ret 
 endp printMessage
 proc InputString          
;askes the user to input chars untill he press (enter) then puts it in StringHelper
b:
lea bx, StringHelper
mov cx,5  
xor dx,dx
a:                 ;restarts string helper
mov [bx],00
inc bx
loop a
lea bx, StringHelper
up:
cmp dx,20        ;cheacks if you wrote more than 20 chars
jz TryAgain
deleted:
xor ax,ax
mov ah,01h
int 21h
xor ah,ah
mov cx,08h        ;checks if the user inputed the backspace key
cmp al,cl
jz BackSpace
mov cx,0dh
cmp al, cl                ;checks if the user enters enter
jz InputIsOver
inc dx
mov [bx],al
inc bx
jmp up
TryAgain:
lea bx, line
call PrintMessage 
lea bx, FullInput
call PrintMessage
jmp b:
BackSpace:
cmp dx,0            ;checks if te user didnt just BackSpaced nothing
jz deleted
lea bx,stringhelper  ;gets the start of the array
add bx,dx            ;adds dx which is the indexer to how many chars  you     already wrote
mov [bx],00h         ;puts 0(nothing) at that place
dec bx
dec dx
jmp deleted          ;returen to get an extra input
InPutIsOver:

ret
endp
start:
  mov  ax, @data
 mov  ds, ax
mov al,13h
int 10h 
call InputString
 ; add your code here

mov ax, 4c00h
int 21h  

ends

end start
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    That giant wall of not-indented code would be more readable if you left blank lines between separate blocks of code, and stuff like that. At least the comments look reasonable (they say why the code is doing whatever). – Peter Cordes Mar 31 '16 at 19:39

2 Answers2

5

For typical screens and terminal emulators, printing the backspace character just moves the cursor one position to the left. To clear a character, try printing backspace+space+backspace.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

Keith is correct, even in DOS if you print a backspace character it is non-destructive. This means the cursor will go back but the character underneath remains. This is normal behaviour.

I haven't had time to look at the code closely but after initially printing the first backspace you can use Int 21h/Ah=2 to print out another space character followed by another backspace.

In the code of Backspace you have:

BackSpace:
cmp dx,0            ;checks if te user didnt just BackSpaced nothing
jz deleted

To fix it, I think you can change the code to something like:

BackSpace:
cmp dx,0            ;checks if te user didnt just BackSpaced nothing
jz deleted

mov ah, 02h         ; DOS Display character call 
mov dl, 20h         ; A space to clear old character 
int 21h             ; Display it  
mov dl, 08h         ; Another backspace character to move cursor back again
int 21h             ; Display it 
Michael Petch
  • 46,082
  • 8
  • 107
  • 198