I wrote a procedure to read integers(unsigned) from stdin and store it in a variable.

I wanted it to handle backspaces too. so I made some adjustments but unfortunately it didn't work.--see line 14 in the code

``````get_num:		;(function get_num: read integers from stdin)
push ax		;save all registers
push bx
push cx
push dx
mov bx,10d	;bx=10(decimal)
xor dx,dx
GNloop1:xor ax,ax
mov ah,01h	;get the input from stdin
int 21h
xor ah,ah	;ah all clear!
cmp al,0Dh	;check if the input was <return>
je GNloop2	;if yes then goto GNloop2
cmp al,08h	;check if BACKSPACE was pressed
jne _continue_	;if not, then continue

xor ax,ax       ;otherwise ...
mov ax,tmp	;take the value in tmp(a word variable) and-
sub ax,cx	;remove what was entered just before the backspace
push ax		;save it in stack,it'll be restored in dx afterwards
div bx		;dx=ax/10
mov tmp,dx	;and save to tmp
cmp dx,0	;if it was first digit
jz GNloop1	;then carry on
pop dx		;if not first digit , then don't forget-
;to restore what was in dx before the(backspaced) digit was pressed

jmp GNloop1	;and then jump back.

_continue_:
sub al,48d	;convert from ascii to decimal,ascii value of 0 is 48d(i.e. 30 in hex)
xor cx,cx	;cx=0
mov cl,al	;save al in cl(used when implementing BACKSPACE)
add ax,dx	;add (ax*10=)dx and ax,in first loop dx is Zero,hence ax=ax+0
xor dx,dx	;dx=0
mov tmp,ax	;save what's in ax to tmp (a word variable),
;1st loop:digit in ones place,2nd loop: digit in tens place and so on.
mul bx		;ax=ax*10
mov dx,ax	;save ax*10 in dx(it will be added to ax in next loop-
;see two instructions above)
jmp GNloop1	; loop

GNloop2:pop dx		;restore registers and return
pop cx
pop bx
pop ax
ret``````

It gives 'division overflow' error when BACKSPACE is pressed.
I'm finding very hard to eliminate this particular bug.
Can anyone please look at this code and help me find the culprit!

I found it myself.Here's the corrected code.

``````get_num:		;(function get_num: read integers from stdin)
push ax		;save all registers
push bx
push cx
push dx
mov bx,10d	;bx=10(decimal)
xor dx,dx
GNloop1:xor ax,ax
mov ah,01h	;get the input from stdin
int 21h
xor ah,ah	;ah all clear!
cmp al,0Dh	;check if the input was <return>
je GNloop2	;if yes then goto GNloop2
cmp al,08h	;check if BACKSPACE was pressed
jne _continue_	;if not, then continue

xor ax,ax       ;otherwise ...
mov ax,tmp	;take the value in tmp(a word variable) and-
sub ax,cx	;remove what was entered just before the backspace
push ax		;save it in stack,it'll be restored in dx afterwards
xor dx,dx       ;empty dx first
div bx		;ax=ax/10
mov tmp,ax	;and save to tmp
cmp ax,0	;if it was first digit
jz GNloop1	;then carry on
pop dx		;if not first digit , then don't forget-
;to restore what was in dx before backspaced digit

jmp GNloop1	;and then jump back.

_continue_:
sub al,48d	;convert from ascii to decimal,ascii value of 0 is 48d(i.e. 30 in hex)
xor cx,cx	;cx=0
mov cl,al	;save al in cl(used when implementing BACKSPACE)
add ax,dx	;add (ax*10=)dx and ax,in first loop dx is Zero,hence ax=ax+0
xor dx,dx	;dx=0
mov tmp,ax	;save what's in ax to tmp (a word variable),
;1st loop:digit in ones place,2nd loop: digit in tens place...
mul bx		;ax=ax*10
mov dx,ax	;save ax*10 in dx(it will be added to ax in next loop-
;see two instructions above)
jmp GNloop1	; loop

GNloop2:pop dx		;restore registers and return
pop cx
pop bx
pop ax
ret``````