;-------------------------sgnd16in routine begins--------------------------+
; from BLUEBOOK OF ASSEMBLY ROUTINES FOR IBM PC & XT.
;         page : 78
;
; NAME SGND16IN
; ROUTINE FOR Conversion From ASCII Signed Decimal To Binary
;
; FUNCTION: This routine accepts a signed decimal number from the std
; input device and converts it to internal signed two's complement 16-bit
; binary form.
;
; INPUT: Individual digits of the signed decimal number are received in
; ASCII through a call to a std input routine.  The sign (+/-) is optional
; and the valid digits are 0 through 9.  An ASCII code other than that for
; the sign and the valid digits terminates the routine.
;
; OUTPUT: A signed two's complement 16-bit binary number is returned in the
; DX register.
;
; REGISTERS USED:  AX and CX are modified.  DX is used for output.
; SEGMENTS REFERENCED:  None
; ROUTINES CALLED:  STDIN
; SPECIAL NOTES: None
;
; ROUTINE TO CONVERT FROM ASCII SIGNED DECIMAL TO INTERNAL TWO'S COMPLEMENT
; 16-BIT BINARY.
;
sgnd16in	proc	far
;
	mov	dx,0		; initialize dx
	mov	ch,0		; sign flag
;
	call	stdin		; look for sign
	cmp	al,'-'		; was it minus ?
	jz	sgnd16in1	; yes... store it
	cmp	al,'+'		; was it a plus ?
	jz	sgnd16in2	; ignore pluses
	jmp	sgnd16in3	; anything else is a number
;
sgnd16in1:
; set sign as a negative
	mov	ch,0FFh		; 0FFh is -1 in two's complement
;
sgnd16in2
; normal loop
	call	stdin		; receive a digit in AL
;
sgnd16in3:
; already have a digit
	sub	al,30h		; reduce from ASCII
	jl	sgnd16in4		; check if too low
	cmp	al,9
	jg	sgnd16in4	; check if too high
	cbw			; convert to word
;
	push	cx		; save sign
	push	ax		; save digit
	mov	ax,dx
	mov	cx,10		; multiplier of ten
	mul	cx		; multiply
	mov	dx,ax		; result in dx
	pop	ax		; restore digit
;
	add	dx,ax		; add in digit
	pop	cx		; restore count
	jmp	sgnd16in2
;
sgnd16in4:
; resolve the sign
	cmp	ch,0		; is it there ?
	je	sgnd16in5	; no... skip it
;
	neg	dx		; was negative
;
sgnd16in5:
;
	ret			; return
;
sgnd16in	endp
;-------------------------sgnd16in routine ends---------------------------+
