UMBC CMSC 211

UMBC | CSEE


Improving Decimal I/O

Previously we had a procedure to convert a binary number to the decimal equivalent for display. Since we got the results backwards, we pushed the answer on the stack and when everything was converted, we popped and printed. The simpliest way to to store the results in an array and print the results from the array.

OutArray DB 5 DUP ( ? )
Stopper DB '$'
Notice that after the array, we put the dollar sign that will be used to stop the string output interrupt. Now we can simply convert each digit until we are done.

mov si, OFFSET ; Start at the right end
mov bx, 10
DigLoop: ; Book in error!
sub dx, dx
div bx
add dl, '0' ; convert next digit to ASCII
dec si ; si points to current first character
mov [ si ], dl
cmp ax, 0
jne DigLoop
_PutStr si
So when we put everything together, we get:
;; BIN2DEC.ASM--Procedure to take a binary number in ax and convert
;;   it to decimal, storing the ASCII characters in memory starting
;;   at di.  On output, di contains the address of the first character
;;   after the last digit of the converted number.
;;
;;  Calling Sequence:
;;	EXTRN	Bin2Dec:NEAR
;;	mov	ax, theNumber
;;	mov	di, OFFSET thePlace
;;	call	Bin2Dec
;;
;;  All registers except di are preserved
;;
;;  Program text from "Assembly Language for the IBM PC Family" by
;;   William B. Jones, (c) Copyright 1992, 1997, Scott/Jones Inc.
;;
	.MODEL	SMALL

	.DATA
M32768	DB	'32768'
DigArray	DB	5 DUP (?)

	.CODE
	PUBLIC	Bin2Dec
Bin2Dec	PROC
	push	ax
	push	bx 			;	Holds divisor (10)
	push	cx 			;	Count of digits generated
	push	dx
	push	si
	test	ax, ax 		;	Is number negative?
	jge	Positive
	mov	BYTE PTR [di], '-' ;	yes, output - sign
	inc	di
	cmp	ax, -32768 		;	Does ax = -ax?
	jne	NotM32768
	mov	si, OFFSET M32768 ;	Address of digits
	mov	cx, 5 ;	Number of digits (sign already set)
	jmp	Transfer
NotM32768:
	neg	ax
Positive:
	mov	si, OFFSET DigArray + 5 ; One beyond end
	sub	cx, cx 			;	Count := 0
	mov	bx, 10
DigLoop:
	sub	dx, dx
	div	bx
	add	dl, '0'
	dec	si 				;	Point to next digit position
	inc	cx 				;	  and count digit
	mov	[si], dl 		;	  and save digit
	test	ax, ax 		;	is ax 0?
	jne	DigLoop 		;	if not, more digits to process
Transfer:
	mov	al, [si]
	mov	[di], al
	inc	si
	inc	di
	loop	Transfer

	pop	si
	pop	dx
	pop	cx
	pop	bx
	pop	ax
	ret
Bin2Dec	ENDP
	END
  
We need something to invoke this!

;; PUTDEC.ASM--New version using Bin2Dec.  Procedure to take a binary
;;   number in ax and display it in decimal on the CRT screen.
;;
;;  Calling Sequence:
;;	EXTRN	PutDec:NEAR
;;	mov	ax, theNumber
;;	call	PutDec
;;
;;  All registers are preserved
;;
;;  Program text from "Assembly Language for the IBM PC Family" by
;;   William B. Jones, (c) Copyright 1992, 1997, Scott/Jones Inc.
;;
INCLUDE	PCMAC.INC

	.MODEL	SMALL

	.DATA
Digits	DB	7 DUP (?) 	    ;	Leave room for longest number and '$'

	.CODE
	PUBLIC	PutDec
	EXTRN	Bin2Dec : NEAR
PutDec	PROC
	push	ax
	push	dx
	push	di
	mov	di, OFFSET Digits
	call	Bin2Dec  		;	number already in ax
	mov	BYTE PTR [di], '$'
	_PutStr	Digits
	pop	di
	pop	dx
	pop	ax
	ret
PutDec	ENDP
	END
  


UMBC | CSEE |