Ok, you need some stuff first. You have to download <b>SNASM68k</b> from our downloads section and a <b>Sonic 1 disassembly</b>
(I suggest you download my disasm because I will refer to its labels).
Now open the Sonic 1 disassembly in notepad. Many lines isn't there? =P<br><br>

Comments are marked by <b>;</b> or <b>*</b>. The game code starts with label <b>'Entrypoint:'</b>.<br><br>

Ok, let's start with the most interesting part of programming, programming XD. Ok, first command, <b>MOVE</b>.
This command moves some data (constants, registers, RAM) into either
another register or RAM location. JohnnyUK nicely called it 'copying'.<br><br>

<pre class="68k">Syntax: <b>move source,destination</b>

Exapmles:
	move.w	#2,($FFFFFE10).w
	move	(a6),d3
	move.b	($FFFFFE12).w,(a5)+</pre><br><br>

Looks complicated? Don't worry, I'll explain everything =) First, why <b>.w, .b</b>? If you use it, you will specify the data length being copied. If you don't the assembler will probably choose <b>.l</b>. <b>.b</b> is <b>byte</b>, <b>.w</b> is <b>word</b> and <b>.l</b> - <b>longword</b>.<br><br>

For example:<br><br>

<pre class="68k">move.b	#1,($FFFFFE10).w

before - FFFFFE10: 05 06 07 08
after  - FFFFFE10: 01 06 07 08

move.w	#1,($FFFFFE10).w

before - FFFFFE10: 05 06 07 08
after  - FFFFFE10: 00 01 07 08

move.l	#1,($FFFFFE10).w

before - FFFFFE10: 05 06 07 08
after  - FFFFFE10: 00 00 00 01
</pre>

I hope you get it =P Now, <b>#x</b> is a immediate value (a number, constant etc =P). Examples:<br><br>

<b>#5</b> - decimal 5<br>

<b>#$20</b> (hex) - decimal 32<br>
<b>#%1001</b> (binary) - decimal 9<br>
<b>#@777</b> (octal) - decimal 511<br>
<b>#'SEGA'</b> - ascii text (remember, this can be tricky. 1 letter - use <b>.b</b>, 2 letters - use <b>.w</b>, 4 letters - use <b>.l</b>)<br><br>

<b>($FFFFFE10).w</b> is an absolute address. Locations in ram <u>HAVE TO</u> be <b>.w</b>. ROM addresses usually are <b>.l</b>. Some programmers (forgot the name of the game...) use <b>.w</b>
for short ROM addresses, but that can cause serious bugs. Remember,
never write anything to a ROM address ;) (real Genesis will crash)<br><br>

Examples:<br><br>

<pre class="68k">move.b	#1,($FFFFFE12).w</pre>

moves 1 (byte) to $FFFFFE12 in RAM (lives in Sonic 1)

<pre class="68k">move.l	($12346).l,d0</pre>

moves a longword from address $12346 in ROM to data register d0.
<pre class="68k">move.w	(a0)+,d0</pre>

moves a word from the location at A0 to d0, increments a0 by 2 (length of word) after that.<br><br>
I'll explain the registers now (already told about it before, but let's
revise XD). There are 16 registers - 8 data registers and 8 address
registers. All registers are longwords. Data registers are called <b>d0-d7</b> and can store any data. Address registers usually store <u>pointers</u> to some location in ROM or RAM, but they can also be used for data. Address registers are <b>a0-a7</b>, but don't use <b>a7</b> because it's the stack pointer (I'll explain later). Registers are a nice way of storing temporary data. Eg.<br><br>

<pre class="68k">move.b	($FFFFFE12).w,d0 ;copy value at $FFFFFE12 to d0 (this is a comment =P)
move.b	d0,($FFFFFE13).w ;move d0 to $FFFFFE13
</pre>

Of course this could be done with:

<pre class="68k">move.b	($FFFFFE12).w,($FFFFFE13).w</pre>

but this is an example =P The biggest reason why use registers is that you can modify them without modifying the source, eg.<br><br>

<pre class="68k">move.b	($FFFFFE12),d0	;copy $FFFFFE12 to d0
add.b	#2,d0		;add 2 to d0
;do something with d0</pre>

We have a new command, ADD. It adds a number to a register or a place in ram.<br><br>

<pre class="68k">Syntax: <b>add source,destination</b>

Examples:	add.b	#2,($FFFFFE12).w	;increment $FFFFFE12 by 2
		add.l	(a0),a0			;increment a0 by value at a0
		add	d0,d0			;add d0 to d0 (d0 = d0 * 2)
</pre>

What is the difference between <b>a0</b> and <b>(a0)</b>?<br><br>

<b>a0</b> is the address, only a number. And <b>(a0)</b> is the value at this address. Have a look at this:


<pre class="68k">$FFFFFE12: 01	;some value in ram
a0: $FFFFFE12	;address
(a0): 01	;value at address $FFFFFE12</pre>

You can postincrement or predecreament address registers:<br><br>
<pre class="68k">;a0 is incremented by 1 (length of byte) after the instruction
		move.b	(a0)+,d0

;a0 is decremented by 2 (length of word) before the instruction
		move.w	-(a0),d0
</pre>

Exapmle:<br><br>
<pre class="68k">move.l	(a0)+,d0

before: a0: $00012345
        d0: $00000000
$000012345: $FFFFFFFF

after:  a0: $00012349
        d0: $FFFFFFFF
$000012345: $FFFFFFFF</pre>

Hope you understand =)<br><br>

Now, the opposite of adding is substracting. You do it using SUB instruction.<br><br>

Exapmles:<br><br>
<pre class="68k">		sub	#5,a0
		sub.w	(a0),($FFFFFE12).w
		sub.b	d0,d1
</pre>

That would be the end of this chapter. In next lesson we'll learn about <b>LEA</b>,
labels, comparing and branching depending on the results (like
if...else). It's about 3 am and I'm a bit tired. If you don't
understand something or you have any questions, ask on the forums =)
