

			Object Files And Linking
			~~~~~~~~~~~~~~~~~~~~~~~~

 Devpacs assembler, Genim2, comes with a ' built in ' linker. This means it
is possible to assemble a source file straight into an executable file. As
this is by far easier than linking an object module using Blink or Alink
this is what most people tend to do. The art of linking is never
investigated.

 Object Files
 ~~~~~~~~~~~~
 An object file is a source file that has been assembled into a stage of
limbo bordering on executable. What?

 Well the source has been assembled into hex format, but all label
references are left in lookup tables. The reason for doing this is that a
particular label may not have been in the source file assembled, but in a
source file assembled at an earlier date.

 When the two source files are linked together using Blink, the lookup
tables are cross referenced and all labels are satisfied.

 It is possible to instruct GenIm that a label referenced in a source file
is not defined in that file, but is defined in another file you intend to
link with it. This is accomplished using the XREF directive.

 It is also possible to tell GenIm that a label defined in a source file is
going to be referenced in another object file. This is accomplished using
the XDEF directive.

 All other labels used in a module are private to that module, so
repetition in another module will not cause duplicate label errors.

 This process of defining labels in one module for use in another is known
as Importing/Exporting.

 Why Bother?
 ~~~~~~~~~~~
 The main advantage of this method is integrating assembler routines into
compiler generated programs. Compilers such as Lattice C, NorthC, PCQ
Pascal, DICE and Draco all support this approach.

 For the assembler programmer, the main benefit is that a module can be
written, tested and debugged in one hit. The final source being assembled
as an object file ready for linking with future programs. There is no need
to ever assemble the code again.

 An example is needed. Let's try something simple first, an object module
that will add two words together:

		XREF		_Answer		Where to store result

		XDEF		_Num1,_Num2	Where inputs are found

_AddNums	moveq.l		#0,d0		clear
		move.l		_Num1,d0
		add.l		_Num2,d0
		move.l		d0,_Answer

_Num1		dc.l		0
_Num2		dc.l		0


 Notice how _Answer is defined as being declared in another module, _Answer
is said to be Imported into this source. _Num1 and _Num2 are defined as
being made available to other modules and are said to be Exported.

 So how would we use this source. Well first we must assemble it as a
Linkable file to disc. Assume we called it 'AddLong.o'. Now a program such
as the following could use this routine:

		XREF		_Num1,_Num2

		XDEF		_Answer

Start		move.l		#5,_Num1
		move.l		#7,_Num2

		jsr		_AddNums	call routine in other
						object module

		move.l		_Answer,d0
		rts

_Answer		dc.l		0

 Assume this has been assembled as a Linkable file called ' Test.o '. Now
the two files would be joined together using Blink from the CLI as follows:

Blink FROM Test.o AddLong.o TO ram:Prog

 The final program generated by linking Test.o and AddLong.o together will
be created in ram: and be called Prog. Use Monam2 to load and single step
this just to convince yourself it works.

 Note: It is important to get the order of the files to be linked correct.
If the AddLong.o routine was placed first, then execution of the final
executable would start in the code generated by AddLong.o, not Test.o as it
should do.

 A Practicle Example
 ~~~~~~~~~~~~~~~~~~~
 One of the many object files I keep handy is a NoiseTracker replayer. This
has had a PowerPacker loader added to it to make loading any file from disc
a simple affair.

 The module Imports _GfxBase and _PPBase from any module linked with it.
For this reason all programs linked with this file must open the graphics
library and powerpacker library.

 There are two labels Exported, these are PlayFile and StopPlaying.
PlayFile requires the address of a NULL terminated filename in register a0,
this file is then loaded and played. If a tune is already playing it is
stopped first. StopPlaying does not require any parameters, it stops any
tunes from playing.

 Here is an example source that utalises this object module:

		Incdir		sys:include/
		Include		exec/exec_lib.i

		XDEF		_GfxBase,_PPBase	Export to Play.o

		XREF		PlayFile,StopPlaying	Import from Play.o

Start		move.b		#0,-1(a0,d0)		NULL CLI parameter

		move.l		a0,a5			save filename ptr

		lea		gfxname,a1		a1->lib name
		moveq.l		#0,d0			any version
		CALLEXEC	OpenLibrary		open
		move.l		d0,_GfxBase		save base pointer
		beq		ERROR

		lea		ppname,a1		a1->lib name
		moveq.l		#0,d0			any version
		CALLEXEC	OpenLibrary		open
		move.l		d0,_PPBase		save base pointer
		beq		ERROR1

		move.l		a5,a0			a0->filename
		jsr		PlayFile		play the music

WAIT		btst		#6,$bfe001		test LMB
		bne.s		WAIT			loop 'till pressed

		jsr		StopPlaying		stop music

		move.l		_PPBase,a1		a1->lib base
		CALLEXEC	CloseLibrary		close it

ERROR1		move.l		_GfxBase,a1		a1->lib base
		CALLEXEC	CloseLibrary		close it

ERROR		moveq.l		#0,d0			No DOS error
		rts					and finito

gfxname		dc.b		'graphics.library',0
		even
_GfxBase	dc.l		0

ppname		dc.b		'powerpacker.library',0
		even
_PPBase		dc.l		0


 As you can see, this is a simple program. With a little work it could be
changed into a useable utility, the Play.o file uses the CIA interrupts and
is not distorted by disc access. I leave this up to you.

 To check this example source out, load the file Test.s from the
Tutorials directory into GenAm. Assemble as a linkable file to ram: with
the name ' test.o '. Now invoke blink with the following:

df1:blink FROM ram:test.o df1:tutorials/play.o TO ram:Player

 When blink has finished, type the following at the CLI:

ram:Player df1:Modules/mod.music

 What easier way of adding a replayer to a program.

 By the way, this is the replayer used by the latest acc menu system!

 What Next
 ~~~~~~~~~
 You can now attempt Paul Overaa's Interrupt tutorial in the December issue
of Amiga User International.

				Mark.

 Okay, I could not resist it. I have made very minor alterations to my ever
faithful Int_Start.s routine and converted it into a better replayer. It now
starts a tune playing, opens an Intuition window and keeps playing the tune
until the window is closed.

 To create this file, you will need to follow these steps:

 1/ Load superplayer.s from the Tutorials of this disc into Genam2.

 2/ assemblr to ram: as a Linkable file called player.o

 3/ Invoke Blink as follows:

Blink FROM ram:player df1:tutorials/play.o TO ram:Player

 4/ To test the replayer, enter the following:

run Player df1:modules/mod.music

 5/ While the music is playing, do something else. Close the small window
   to stop the music.

 Hope this has convinced you that object modules have their uses.

 One last use is doing away with Incbin files. Use a utility I wrote ages ago 
to convert raw data files into object files. You can even specify the label
name to be Exported from the raw data!!!



