	opt	L+		;make linkable
******************************************************************************
*
*		Example of using amiga.lib with assembly code
*
*			Compiles with Devpac V2.14
*
*		By Steve Marshall. Use and abuse as you will.
*
******************************************************************************

		incdir		"include:"
		include		"exec/exec.i"
		include		"libraries/dos.i"

;--------------	Let the linker know about these variables with xdef
		xdef		_DOSBase
		xdef		_stdout
		xdef		_stdin
		xdef		_SysBase
	
;--------------	Import the values from amiga.lib
		xref		_LVOCloseLibrary
		xref		_LVODelay
		xref		_LVOOpen
		xref		_LVOOpenLibrary
		xref		_LVOOutput
		
		xref		_afp
		xref		_CreateExtIO
		xref		_CreatePort
		xref		_DeleteExtIO
		xref		_DeletePort
		xref		_fclose
		xref		_fgetc
		xref		_fpa
		xref		_fpbcd
		xref		_fprintf
		xref		_fputc
		xref		_fputs
		xref		_getchar
		xref		_NewList
		xref		_printf
		xref		_putchar
		xref		_puts
		xref		_RangeRand
		xref		_sprintf

******************************************************************************

CALLSYS    MACRO		;added CALLSYS macro - using CALLARP
	IFGT	NARG-1       	;CALLINT etc can slow code down and  
	FAIL	!!!         	;waste a lot of memory  S.M. 
	ENDC
	JSR	_LVO\1(A6)
	ENDM
		
*****************************************************************************

start
	moveq		#0,d0			;any version
	lea		dosname,a1		;dos lib name
	move.l		4.w,a6			;execbase
	move.l		a6,_SysBase		;store for amiga.lib
	CALLSYS		OpenLibrary		;open dos lib
	move.l		d0,_DOSBase		;save lib base
	beq		NoDOS			;branch on error
	
	move.l		d0,a6			;lib base in a6
	CALLSYS		Output			;get std output handle
	move.l		d0,_stdout		;store as _stdout
	move.l		d0,_stdin		;and stdin
	beq		NoOut			;branch if no handle
		
afp
	pea		afpString		;get afp string
	jsr		_puts			;and print
	pea		FFPString		;push string onto stack
	jsr		_afp			;and convert to FFP
	addq.l		#8,sp			;correct stack
	move.l		d0,FFPNum		;save number

fpa
	pea		fpaString		;get fpa string
	jsr		_puts			;and print it
	pea		Buffer			;push workspace add on stack
	move.l		FFPNum,-(sp)		;get FFP number
	jsr		_fpa			;convert to ascii
	lea		12(sp),sp		;correct stack
	move.l		d0,Exp			;save exponent part

RangeRand
	move.l		#100,-(sp)		;number range
	jsr		_RangeRand		;first call always returns 0
	jsr		_RangeRand		;this usually returns $34
	addq.l		#4,sp			;correct stack
	move.l		d0,rnd			;save random number

sprintf
	move.l		d0,-(sp)		;push random number on stack
	move.l		d0,-(sp)		;push random number on stack
	pea		FmtString		;push format string on stack
	pea		DestString		;push destination string on stack
	jsr		_sprintf		;format string

	jsr		_puts			;print destination string
	lea		16(sp),sp		;correct stack

	move.l		#CONName,d1		;get console name
	move.l		#MODE_OLDFILE,d2	;get mode
	move.l		_DOSBase,a6		;get dos lib base
	CALLSYS		Open			;open console
	move.l		d0,FileHandle		;store handle
	beq		getchar			;branch on error

fgetc
	move.l		FileHandle,-(sp) 	;handle onto stack
	pea		fgetcString		;fgetc string onto stack
	jsr		_fputs			;print on console
	addq.l		#4,sp			;correct stack
	jsr		_fgetc			;get a character from console
	move.b		d0,Char+3		;and store it 
	addq.l		#4,sp			;correct stack

fputs
	move.l		FileHandle,-(sp)	;handle onto stack 
	pea		fputsString		;fputs string onto stack
	jsr		_fputs			;and print on console
	addq.l		#8,sp			;correct stack
	
fputc
	move.l		FileHandle,-(sp)	;handle onto stack 
	pea		fputcString		;fputc string onto stack
	jsr		_fputs			;and print on console
	addq.l		#4,sp			;leave handle on stack
	move.l		Char,-(sp)		;push char on stack
	jsr		_fputc			;print char
	addq.l		#4,sp			;leave handle on stack

fclose
	pea		fcloseString		;fclose string on stack	
	jsr		_fputs			;and print to console
	addq.l		#8,sp			;correct stack
	
	move.l		#250,d1			;set delay at 5 secs
	move.l		_DOSBase,a6		;get dos lib base
	CALLSYS		Delay			;and wait
	
	move.l		FileHandle,-(sp)	;CON handle on stack 
	jsr		_fclose			;and close console
	addq.l		#4,sp			;correct stack

getchar
	pea		getcharString		;getchar string on stack
	jsr		_puts			;print to stdout
	addq.l		#4,sp			;correct stack
	jsr		_getchar		;get char from stdin
	move.b		d0,Char+3		;and store it

putchar
	pea		putcharString		;putchar string on stack
	jsr		_puts			;print to stdout
	move.l		Char,-(sp)		;char on stack
	jsr		_putchar		;and print char to stdout
	pea		NullString		;get null string
	jsr		_puts			;print (to add linefeed)
	lea		12(sp),sp		;correct stack

printf
	move.l		#$0f0ff084,d0		;number in d0
	move.l		d0,-(sp)		;push onto stack
	move.l		d0,-(sp)		;push onto stack
	pea		printfString		;get format string
	jsr		_printf			;print result
	lea		12(sp),sp		;correct stack

puts
	pea		PutsString		;puts string onto stack
	jsr		_puts			;and print to stdout
	addq.l		#4,sp			;correct stack
	
CreatePort
	pea		CreatePortString	;get createport string
	jsr		_puts			;and print it to stdout
	
	move.l		#0,-(sp)		;pri = 0
	pea		PortName		;push portname on stack
	jsr		_CreatePort		;create msg port
	lea		12(sp),sp		;correct stack
	
	move.l		d0,Port			;store port
	bne.s		PortOK			;branch if no error
	
	pea		NoPortString		;get port error string
	jsr		_puts			;print to stdout
	addq.l		#4,sp			;correct stack
	bra		NewList			;branch always
	
PortOK
	move.l		Port,-(sp)		;port add on stack
	pea		PortfString		;get port format string
	jsr		_printf			;print result to stdout
	addq.l		#8,sp			;correct stack
	
CreateExtIO
	pea		CreateExtIOString	;get createextio string
	jsr		_puts			;print to stdout
	
	move.l		#$44,-(sp)		;size of IO_Audio
	move.l		Port,-(sp)		;rely port on stack
	jsr		_CreateExtIO		;create io block
	lea		12(sp),sp		;correct stack
	
	move.l		d0,IOBlock		;save io block
	bne.s		IOBOK			;branch if no errors
	pea		NoIOBString		;get io error string
	jsr		_puts			;print to stdout
	addq.l		#4,sp			;correct stack
	bra.s		DeletePort		;branch always

IOBOK
	move.l		IOBlock,-(sp)		;iob on stack
	pea		IOBfString		;get io format string
	jsr		_printf			;print result to stdout
	addq.l		#8,sp			;correct stack
	
DeleteExtIO
	pea		DeleteIOBString		;get delete iob string
	jsr		_puts			;print to stdout
	
	move.l		IOBlock,-(sp)		;io block on stack
	jsr		_DeleteExtIO		;and delete it
	addq.l		#8,sp			;correct stack
	
DeletePort
	pea		DeletePortString	;get delete port string
	jsr		_puts			;print to stdout
	
	move.l		Port,-(sp)		;port on stack
	jsr		_DeletePort		;and delete it
	addq.l		#8,sp			;correct stack
	
NewList
	pea		ListString		;get list string
	jsr		_puts			;print to stdout
	pea		List			;push list on stack
	jsr		_NewList		;initialize list
	addq.l		#8,sp			;correct stack
	
	move.l		#250,d1			;set 5 secs delay
	move.l		_DOSBase,a6		;get dos lib base
	CALLSYS		Delay			;wait

NoOut
	move.l		_DOSBase,a1		;get dos lib base
	move.l		4.w,a6			;execbase in a6
	CALLSYS		CloseLibrary		;close dos lib
 
NoDOS
	rts					;end of program

******************************************************************************
;			Variables and Strings
******************************************************************************

rnd
	dc.l	0
Port
	dc.l	0
IOBlock
	dc.l	0
Char
	dc.l	0
FFPNum
	dc.l	0
Exp
	dc.l	0
FileHandle
	dc.l	0
_DOSBase
	dc.l	0
_stdout
	dc.l	0
_stdin
	dc.l	0
	
_SysBase
	dc.l	0
	
List
	dcb.b	14,0
Buffer
	dcb.b	20,0
DestString:
	dcb.b	64,0
 
dosname
	DOSNAME
	
CONName
	dc.b	'CON:0/156/640/100/Test Console',0
	EVEN
	
PortName
	dc.b	'Amiga lib port',0
	EVEN
				
afpString
	dc.b	'Testing the afp (ASCII to Floating point) routine',0
	EVEN
	
CreatePortString
	dc.b	'Trying to create a message port',0
	EVEN
	
CreateExtIOString
	dc.b	'Trying to create an Extended IO Block',0
	EVEN
	
DeleteIOBString
	dc.b	'Deleting the Extended IO Block',0
	EVEN
	
DeletePortString
	dc.b	'Deleting the Reply Port',$0a,0

fcloseString
	dc.b	$0a,'Testing the fclose routine - Closing Console',0
	EVEN

FFPString:
	dc.b	'4.5938744E30',0
	EVEN

fgetcString
	dc.b	'Testing the fgetc routine - Press any key + Return...',$0a,0
	EVEN

FmtString:
	dc.b	'Random Number in Hex is $%lx and in Decimal is %ld',0
	EVEN

fpaString
	dc.b	'Testing the fpa (Floating point to ASCII) routine',0
	EVEN

fputcString:
	dc.b	$0a,'Testing the fputc routine - you pressed key ...',0
	EVEN

fputsString
	dc.b	'Testing the fputs routine',0
	EVEN

getcharString
	dc.b	'Testing the getchar routine - Press any key + Return',0
	EVEN
	
IOBfString
	dc.b	'Created Extended IO Block at address $%lx',$0a,0
	EVEN

ListString
	dc.b	'Initializing a new list',0
	EVEN
	
NoIOBString
	dc.b	"Can't create Extended IO Block",0

NoPortString
	dc.b	"Can't create port",0
	
NullString
	dc.w	$0000

PortfString
	dc.b	'Created Port at address $%lx',$0a,0
	EVEN

printfString
	dc.b	$0a,'Testing the printf routine - Number is $%lx or Decimal %ld',$0a,0
	EVEN

putcharString
	dc.b	'Testing the putchar routine - key pressed was...',0
	EVEN
	
PutsString:
	dc.b	'Testing the puts routine',$0a,0
	EVEN

