IA-32 16 bit Integer to ASCII conversion

ShiftLeft 0 Tallied Votes 537 Views Share

This code will not work on EMU8086 or any processor prior to 80286. Line 40 would need to be changed.

; ============================================================================================
; Converts unsigned integer to NULL terminated ASCII string

;	ENTRY:	AX = 16 bit value to be converted
;		DX = Pointer to destination buffer (must be at least 5 bytes in length)

;	LEAVE:	AX = Pointer to first character of conversion
;		DX = Unchanged and points to ASCII string padded with blanks.
; --------------------------------------------------------------------------------------------

	I2A_16	push	di			; Preserve base pointer
		push	dx
		mov	dx, ax
	
	; This creates a buffer with leading spaces.  Could be changed to '00' if leading
	; zero's are prefered.
	
		mov	ax, '  '		; Pad buffer with spaces
		stosw
		stosw
		mov	byte [di], 0		; Teminate with NULL
		dec	di			; Bump back to last byte of string
		
	; It might be a better idea to check flag before changing, but in this case I'm
	; going to assume all routines change to auto increment before completing.
	
		std				; Auto decrement
	
	; Cycle until DX is null, but should it be zero at least one '0' will be written.
		
	 .Next	mov	 al, dl			; Get next byte 
	 	and	 al, 15			; Strip high nibble
	 	or	 al, '0'		; Make ASCII
	 	cmp	 al, 58			; and determine if greater than '9'
	 	jb	$ + 4			; Branch if between '0' & '9'
	 	
	 	add	 al, 7			; Makes character 'A' - 'F'
	 	
	 	stosb				; Write ASCII character to buffer
		shr	dx, 4			; Shift next nibble into AL
		jnz	.Next			; and continue if not zero
		
		cld				; Set default direction	
	 	pop	dx			; Restore original contents
		mov	ax, di			; Copy pointer
		inc	ax			; and bump so there are no leading spaces
		pop	di			; Pointer with leading spaces
		ret
mathematician 0 Junior Poster

Line 40 would need to be replaced by:

mov cl, 4
shr dx,cl

In the first x86 processors, only shr dx,1 or shr dx,cl were allowed (for dx or any other register).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.