//----------------------------------------------------------------
//	eax2hex.s
//
//	This file defines a procedure which can be called from a
//	separate assembly language program that wants to display
//	the value now in register EAX as a string of hexadecimal 
//	numerals.  The address of an 8-character array is passed
//	to the subroutine in the EDI register for use in storing
//	the ascii codes for the eight hexadecimal numerals.  All
//	registers except EFLAGS are preserved by the subroutine.
//
//	      assemble using:  $ as eax2hex.s -o eax2hex.o
//
//	programmer: ALLAN CRUSE
//	written on: 01 FEB 2006
//----------------------------------------------------------------

	.section	.data
radix:	.long		16		# hexadecimal number-base
hex: 	.ascii		"0123456789ABCDEF"	# set of numerals

	.section	.text
eax2hex:
	pushal				# preserve CPU registers

	# loop to generate the remainders for division by sixteen	
	movl		$8, %ecx	# perform eight divisions 
nxdiv:
	subl		%edx, %edx	# setup dividend in EDX:EAX
	divl		radix		# divide dividend by radix
	pushl		%edx		# save remainder on stack
	loop		nxdiv		# then do another division

	# loop to store the hexadecimal numerals in reverse order
	movl		$8, %ecx	# generate eight numerals
nxnum:	
	popl		%edx		# recover saved remainder
	movb		hex(%edx), %al	# lookup the numeral
	movb		%al, (%edi)	# and put it into buffer  
	incl		%edi		# advance buffer pointer
	loop		nxnum		# go get another remainder

	popal				# restore saved registers
	ret				# return control to caller

	.global		eax2hex		# make entry-point visible
	.end				# nothing more to assemble