1 Replies - 3464 Views - Last Post: 08 November 2012 - 06:55 AM

#1 jprin72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 19-March 12

Using Backspace to Correct signed integer input

Posted 04 November 2012 - 12:16 AM

Hi all,

I am having some trouble using backspace to correct a signed integer input. This code works if the user enters a number, then backspace, '-', and another number. But not when trying to correct from a negative number to a positive. I am also having some trouble if the user backspaces more than once. This code is written for a NASM assembler for an x86 processor.

Thanks,
jprin72

DEC_IN:
	push ax		;save register
	push dx		;save register

	xor bx, bx	;sets bx = 0, bx will hold sum
		
		;read char
SNCHK:	mov ah, 1	;prep for char read
	int 21h		;reads char into al
		;is number - (store in stack)
	cmp al, 2Dh	;al = -?
	jne ELSE1	;No, proceed to else,
	mov dx, 1 	;Yes, store one in dx (number -)
	mov [sign], dx	
	int 21h		;read another char
	jmp TOPW1	;Proceed to further processing

ELSE1:
	mov dx, 0	;move 0 in dx (number +)
	mov [sign], dx

TOPW1:
	cmp al, CR	;al = CR? 
	je ENDW1	;Yes, exit while loop

	cmp al, 08h	;al = BS?
	je BCKSP	;Yes, process backspace
	
		;prep for mul
	push ax		;saves ax (al contains char) before mul
	mov ax, 10	;prep for mul by 10 
	mul bx		;mul bx by 10 result stored in dx:ax (ax has it)
	mov bx, ax	;store value back in bx

	pop ax		;restore char
	
	and ax, 000Fh	;cnvt to digit (masks all but bottom 4 bits)
	add bx, ax	;add digit to bx
		
RDCHAR:		;read another char
	mov ah, 1 	;preps for chat read
	int 21h		;reads char
	jmp TOPW1	;back to top


BCKSP:	
		;process backspace by dividing by 10
		;remainder does not matter

	mov ax, 10	;prep for div
	xchg ax, bx	;prep to div bx by 10 (ax must contain top #)
	div bx		;ax/dx -> ax = quotient dx = remainder
	xchg ax, bx	;moves quotient back into bx

		;erase char from scrn
	
	mov ah, 2	;prep for char display
	mov dl, 00h	;prep to output null char
	int 21h		;char output with fcn 2
	mov dl, 08	;prep for output backspace
	int 21h		;char output with fcn 2

	cmp bx, 0
	je SNCHK
	jmp RDCHAR	;reads next char and proceeds with processing input

ENDW1: 

		;process as signed number
	mov dx, [sign]	;restores sign indication 
	cmp dx, 1	;dx = 1 (- #)
	jne POSN	;No, # is postive skip ahead
	neg bx		;Yes, negate bx
POSN:		;proceed as normal

	pop dx		;restore register
	pop ax		;restore register
	ret 



Is This A Good Question/Topic? 0
  • +

Replies To: Using Backspace to Correct signed integer input

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Using Backspace to Correct signed integer input

Posted 08 November 2012 - 06:55 AM

In the old days "DEL"-character was echoed on the screen as
backspace, blank, backspace.

Does that help?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1