//-----------------------------------------------------------------
//	showreal.s
//
//	This program displays the single-precision floating-point
//	number whose value is supplied in the data-section below. 
//
//	      to assemble:  $ as showreal.s -o showreal.o
//	      and to link:  $ ld showreal.o -o showreal
//	      and execute:  $ ./showreal
//
//	programmer: ALLAN CRUSE
//	written on: 23 APR 2007
//-----------------------------------------------------------------

	.section	.data
real:	.float	1.2345			# the built-in real number	
temp:	.int	0			# storage for its multiple
tento4:	.int	10000			# decimal-point will shift
msg:	.ascii	" The real-number is "	# legend for output string
buf:	.ascii	"                  \n"	# buffer for output string
len:	.int	. - msg			# length for output string

	.section	.text
_start:	# multiply the real-number by 10**4
	finit				# initializes coprocessor
	fld	real			# load the real number
	fild	tento4			# load multiplier-factor
	fmulp				# multiply and pop st(0) 
	fistp	temp			# store st(0) as integer
	fwait				# synchronize processors

	# convert the 'temp' integer to a decimal digit-string 
	mov	temp, %eax		# temp into accumulator
	mov	$10, %ebx		# and radix into EDX
	mov	$5, %ecx		# setup digit counter
nxdiv:	xor	%edx, %edx		# extend EAX to quadword
	div	%ebx			# perform division by 10
	add	$'0', %dl		# remainder intto ascii
	mov	%dl, buf(%ecx)		# store numeral in buffer
	loop	nxdiv			# again for other digits

	# insert decimal-point
	movb	$'.', buf		# overwrite leading zero
	rolw	$8, buf			# swap character-positions

	# display message
	mov	$4, %eax		# 'write' system-call ID
	mov	$1, %ebx		# standard-output device
	lea	msg, %ecx		# address of message
	mov	len, %edx		# length of message
	int	$0x80			# invoke kernel service

	# terminate program
	mov	$1, %eax		# 'exit' system-call ID
	mov	$0, %ebx		# use zero as exit-code
	int	$0x80			# invoke kernel service

	.global	_start
	.end