; ---------------------------------------------------------------------

; Paul Overaa's Amiga Computing Assembler Series - code for instalment 1

; ---------------------------------------------------------------------

NULL			EQU	   0

_AbsExecBase		EQU	   4

_LVOOpenLibrary		EQU	-552

_LVOCloseLibrary	EQU	-414

_LVODisplayBeep		EQU	 -96

; ---------------------------------------------------------------------

LINKLIB		MACRO
		move.l	a6,-(a7)
		move.l	\2,a6
		jsr	\1(a6)
		move.l	(a7)+,a6
		ENDM

CALLSYS		MACRO
		LINKLIB	_LVO\1,\2
		ENDM

; ---------------------------------------------------------------------

_main		lea 	lib_names,a2
		lea 	_DOSBase,a3
		move.w	#(5-1),d3		loop counter
openloop	move.l	(a2)+,a1		library name pointer
		moveq	#0,d0			any version will do
		CALLSYS	OpenLibrary,_AbsExecBase
		move.l	d0,(a3)+		store returned base
		dbeq	d3,openloop

		beq	error_exit				
		
		; if we reach here all libraries are open 
		; and available for use. Next two instructions
		; just provide a visible test of this!

		move.l	#NULL,a0		flash all screens
		CALLSYS	DisplayBeep,_IntuitionBase
		
normal_exit	lea	lib_names,a3		convenient label - see mag text
		moveq	#5,d2			library count
		jsr	CloseLibs		close libraries
		moveq	#0,d0			clear d0 for O/S
		rts				and terminate program

error_exit	moveq	#(5-1),d2		see mag text
		sub	d3,d2
		jsr	CloseLibs		close libraries
		moveq	#0,d0			clear d0 for O/S
		rts				and terminate program
; ---------------------------------------------------------------------		

; CloseLibs() On entry...

; 	a3 should hold address of the longword location just past 
; 	   that of the first library to close (this is because the 
;	   routine uses a backward reading loop).

; 	d2 should hold count of the number of libraries to close	

CloseLibs	tst.b	d2			test counter
		beq	loop_end		
		move.l	-(a3),a1		get library base
		CALLSYS	CloseLibrary,_AbsExecBase
		subq.b	#1,d2
		bra	CloseLibs
loop_end	rts

; ---------------------------------------------------------------------					

_DOSBase	ds.l 1
_GfxBase	ds.l 1
_IntuitionBase	ds.l 1
_GadToolsBase 	ds.l 1
_AslBase	ds.l 1

lib_names	dc.l lib1,lib2,lib3,lib4,lib5

lib1		dc.b 'dos.library',NULL
lib2		dc.b 'graphics.library',NULL
lib3		dc.b 'intuition.library',NULL
lib4		dc.b 'gadtools.library',NULL
lib5		dc.b 'asl.library',NULL

		END

; ---------------------------------------------------------------------


		