Yay, finally got some time to write another lesson (it's almost 12pm).
Anyway, we'll learn about compares, branches and subroutines. But
first, here's some additional info to the previous lesson:<br><br>

There are few types of <b>MOVE</b>. There's <b>MOVEA</b>, <b>MOVEM</b>, <b>MOVEP</b> and <b>MOVEQ</b>. Usually the compiler chooses the right one, but you sometimes have to choose the correct one (assemblers aren't very smart ;).<br><br> 


<b>MOVEA</b> moves an address into an address register. The syntax:<br><br>

<pre class="68k">	movea	address,a0-a7</pre>

As I said before, don't use <b>a7</b>. Just wait =) The address is an effective address (can be a register, immediate value etc.) Here are some examples:
<pre class="68k">        movea    #$12346,a0     ;moves address $12346 to a0
        movea   -(a1),a5        ;moves address at value at pointer
                                ;in a1 to a5, decrements a1 before instruction
        movea   d0,a3           ;moves d0 to a3</pre>

I'll introduce the labels now :) Remember BASIC and goto function? Labels are similiar:<br><br>

<pre class="68k">	somecode
	
	bra	Label

	somecode

Label:
	somecode</pre>


<b>BRA</b> goes (jumps) to a label. <b>Bcc</b> instructions can branch on certain conditions, but they can be made with the <b>CMP</b> instruction. This means a bunch of new instructions ;) Here's the example code:
<pre class="68k">	game code
	game code

	bsr	LifesSub

	game code
	game code


LifesSub:
	cmp.w	#$0102,($FFFFFE10).w
	bne	TheLevelIsNot0102
	add.b	#1,($FFFFFE12).w
TheLevelIsNot0102:
	rts</pre>


Oh my, so many new things. We created <b>label 'LifesSub'</b>. It's called by <b>BSR</b>, what's that? <b>BSR</b> means <b>Branch to SubRoutine</b>.
Subroutines are like functions in C (or other programming languages).
You can call them whenever you want. There is one important thing
though, after the end of subroutine you <u>have to</u> put <b>rts</b>, which returns to the state before subroutine calling. Example:

<pre class="68k">	bsr	Foo
	bsr	Foo
	bsr	Foo

	...


Foo:

	code

	rts</pre>

Now, when Genny sees <b>BSR</b> (8 or 16 bit instruction, relative address) or <b>JSR</b> (32 bit absolute address, I'll tell about it later), it copies the next insruction <b>PC</b>
(Program Counter - register which stores info about the current
instruction location in ROM, like where the code is now, it's hard to
explain ;) state to <b>stack</b>.<br>I promised before that I'll tell you what the hell <b>stack</b> is. <b>Stack</b> is a reserved section in <b>RAM</b> (CAN be in SRAM, but it isn't tested yet ;) for temporary things, like our <b>PC</b> state. Now you see why <b>RTS</b> is important. <b>RTS</b> copies the <b>PC</b> state back from <b>stack</b>.<br><br> 


So, what's the <b>CMP</b> thingy? It COMPARES two values. It's not exactly if...else statement like in C. <b>BNE</b> means <b>'Branch if Not Equal'</b>. So if <b>$FFFFFE10</b> (level) isn't <b>$0102</b> (these two values are the same, equal), it branches. If it isn't, it doesn't. Simple, huh? There are many <b>Bcc</b> instructions, one of them being <b>BEQ</b> (<b>Branch if EQual</b>) which works opposite to <b>BNE</b>. I mentioned <b>JSR</b> some lines before. It is like <b>BSR</b>,
but it doesn't branch, but jumps to a subroutine. What's the
difference? Branch instruction is small (1 byte if branch is between
-127...127 and 2 bytes if it's between -32 768...32 768) but it's
limited. Some games like Sonic 3 &amp; Knuckles use RAM for... code.
Yeah, small parts of code that can be edited at any time. You can't
branch to RAM, that's first. Second, the limitation. $8000 back or
forth is not very big. There's the JSR. The instruction is longer (4 or
6 bytes, depending on the type), but it's an absolute address (you can
make a relative <b>jsr</b> insruction, but I've already told enough
new things today...), so you can jump to any address in 68000 memory.
For 68k memory map, see additional info. Examples:

<pre class="68k">	jsr	$FFFFFFF0
	jsr	Label
	jsr	a0</pre>

The absolute equivalent of <b>BRA</b> is <b>JMP</b>, which jumps to an absolute address without puting the PC on the stack.<br><br> 

The last two things we're gonna learn today are <b>MOVEQ</b> and <b>MOVEM.</b> 


<b>MOVEQ</b> is similar to <b>MOVE</b>, but it means <b>MOVE Quick</b>. It's a faster (and smaller - 1 byte) instruction. You can only move 1 byte. Example:
<pre class="68k">	moveq	#0,d0</pre>
Clears <u>ENTIRE</u> <b>d0</b>, because when you <b>moveq</b> into a register, it sets the <u>ENTIRE</u> register.<br><br>

It clears the register! <b>Move.l</b> would take 6 bytes (hey, in old times space was worth more than gold), when <b>moveq </b> only 1.<br>
You can also use <b>ADDQ</b> and <b>SUBQ</b> (they can also change values in ram)<br><br>

Now, the last thing. <b>MOVEM</b> (<b>MOVE Multiple</b>).
It can copy more than one register to a location in RAM or in reverse
(RAM to more than one register). As I said before A7 is the stack
pointer. So you can use stack, too! The reason why <b>MOVEM</b> was
created are subroutines. Subroutines are called in many different
places in ROM. And subroutines often use registers. Now if you don't
want to change some important registers inside a subroutine, you should
backup these registers before the subroutine and copy them back after.
Don't get it? Look at the example:<br><br>
<pre class="68k">	move.b	#$10,d0
	bsr	Subroutine
	move.b	d0,d1

	...

Subroutine:

	...

	moveq	#0,d0

	...

	rts
</pre>

Take a look at that code. We set <b>d0</b> to <b>$10</b>, then the subroutine clears <b>d0</b>. After the subroutine, we move <b>d0</b> to <b>d1</b>. That's not exactly what we wanted (it'll set d1 to 0, we wanted it to be <b>$10</b>). To fix that we can use movem:<pre class="68k">

	move.b	#$10,d0
	bsr	MovemSub
	move.b	d0,d1

	...

MovemSub:

	movem	d0-d2,-(sp)

	...

	moveq	#0,d0

	...

	movem	(sp)+,d0-d2
	rts</pre>

Now it'll work! the <b>SP</b> is the <b>stack pointer</b>. You could use <b>A7</b> (it's the same), but I use <b>sp</b>. We decremented <b>sp</b>, because if we didn't, we would change some important data in <b>SP</b>. and after that, we increment <b>SP</b> back to its state. The register list can be different. Eg.<br><br>

<pre class="68k">d0-d5 - d0, d1, d2, d3, d4, d5
a5-a6 - a5, a6
d1-d3/d6-d7 - d1, d2, d3, d6, d7
d6-a1/a5-a6 - d6, d7, a0, a1, a5, a6</pre>

There are many different possibilites... If you don't understand <b>MOVEM</b>,
just skip it, and come back to it when you will know more about ASM. It
took me 4 tries to understand that thing XD (and I didn't have any
nifty guides *shot*)<br><br>

That's all folks! In next lesson I'll introduce some more <b>Bcc</b> instructions and maybe some bit operations? G'night.