MACHINE CODE TUTORIAL

Welcome to the first part of the beginner's guide to 68000   assembly
programming  for the  Amiga.   We will be  attempting to be  slightly
different  to  normal  tutorials,  in  that we will be  comparing our
code with Basic programming.   Most people who want to try their hand
at the  68000  language usually have a little  knowledge of some sort
of Basic (be it  AmigaBASIC  or the BBC version),  so in comparing it
with 68000,  we hope to make it  easier for the  beginner.  Also,  we
will  be  showing  the  code  in  the form of "building blocks".  One 
instruction  on  it's  own  won't  do  much;   however,  a  block  of
instructions will  allow some  small task  to be done.  One could tag
a number of these "blocks" together to build a program.

It  would  be  a  good idea  to have a  book explaining the different 
68000  commands  to start with;  I  started with  Abacus's  excellent
Amiga Machine Code for the  Absolute Beginner,  and I would recommend
this to anyone who hasn't got a book yet.  Also,  a good assembler is
needed to try any of these programs out.  Again, I recommend HiSoft's
DevPac for the Amiga.


1. The FOR...NEXT loop

Right, we'll start straight off with the FOR...NEXT loop.  Here's the
basic program we want to emulate:

FOR x = 1 TO 20 : < Do Something > : NEXT x

Well, here's one way of doing it in 68000 (I have added Comments):

Start:
	move.w	#1,d0	; Start with 1 ("FOR x = 1...")
Loop:
	< Do Something >; DON'T TYPE THIS LINE IN!
	add.w	#1,d0	; Increment x by 1
	cmp.w	#21,d0	; Is it above 20?
	bne	Loop	; No, so loop back again
	RTS		; Otherwise, we've finished.
	
In the above example, D0 is used for the loop.  D0 is a data register
held in the Amiga (much like a Basic variable).  There are eight data
registers, numbered  D0 to D7.  The numbers stored inside them can be
one, two, or four bytes long.  The ".W" extension I have suffixed the
instructions stands for  "Word",  i.e.  two bytes.  This allows for a
number between -32768 and 32767. The other two extensions allowed are
".B" for byte (allowing -128 to 127),  and  ".L" for long-word (which
allows between -2,147,483,648 to 2,147,483,648!).

D0  to  incremented  with  the  "Add.w"  instruction,  and then it is
compared with 21.  I  have  compared with 21  because  <Do Something>
would then be executed with D0 ranging from 1 to 20.  When D0 gets to
21, the program is terminated.  I use "BNE.S"  which means "Branch if
Not Equal". Not equal to what?  I hear you ask.  Well, it is relating
to the "CMP.W" instruction  on the line above.  So together,  the two
lines mean  "Branch to the line labelled with LOOP if D0 is not equal
to 21". There are other branch  "Conditions"  which will be described 
at a later stage.

However,  there is a quicker (and in my view,  a neater) way of doing
a  FOR...NEXT  loop.  It  involves  D0  (or any data register)  being
decremented.  Here's  the  basic  example,  as above,  but now with a
decrementing loop:

FOR x = 20 TO 0 STEP - 1 : < Do Something > : NEXT x

...and here's how to do it in 68000:

START:
	move.w	#20,d0	; Start with 20
Loop:
	<Do Something>
	dbra	d0,Loop	; Decrement and Branch
	RTS
	
What's  happening  here  then?  Well,  D0 starts at 20,  as you would
expect.  The <Do Something> code would then be executed. SO what does
"DBRA" do?   Quite simply,   it  decrements  the  value inside D0 and 
branches to the label  "Loop" if D0 is  ABOVE -1. When D0 reaches -1,
the program would fall through to the RTS command.

In the next issue we will look at another Basic command,  and discuss
what could be put inside that <Do Something> block.  However, I won't
leave  you with  nothing to do.  What would the  following section of
program do? Answer in the next Issue of Missionary.

START:
	move.w	#5,d0
Loop:
	add.w	#1,d0
	cmp.w	#16,d0
	bne.s	Loop
Loop2:
	dbra	d0,Loop2
	RTS
	

	
