In this chapter we'll learn how to search for wanted code and edit it.
But before that, I'll show some arithmetic and data functions.<br><br>

First, the <b>LEA</b> (<b>Load Effective Address</b>) instruction. It loads an effective address into an address register. You should use this instead of <b>MOVEA</b>. Examples:
<pre class="68k">	lea	($FFFFFE10).w,a1
	lea	($12345).l,a2
	lea	(Label).l,a3
	lea	4(a0),a4
	lea	4(pc),a5</pre>

<b>lea 4(a0),a4</b> loads the value at <b>a0 + 4</b> into <b>a4</b>. <b>lea 4(pc),a5</b> loads <b>PC + 4</b> into <b>a5</b>.<br><br> 


You can multiply and divide in 68k (which I didn't know for years, really XD). There are 4 instructions for it: <b>DIVS</b> (<b>DIVide Signed</b>), <b>DIVU</b> (<b>DIVide Unsigned</b>), <b>MULS</b> (<b>MULtiply Signed</b>), <b>MULU</b> (<b>MULtiply unsigned</b>). Examples:<br><br>

<pre class="68k">	divu	#5,d0	;divide d0 value by 5 (unsigned)
	divs	d1,d2	;divide d2 value by d1 value (signed)
	mulu	#3,d3	;multiply d3 value by 3 (unsigned)
	muls	d3,d0	;multiply d0 value by d3 value (signed)</pre><br><br>

As you know or not, unsigned <b>$FE</b> is <b>$FE</b> and signed <b>$FE</b> is <b>-$2</b>. There's a difference between multiplying by <b>$FE</b> and <b>-$2</b>:

<pre class="68k">	moveq	#4,d0
	move.l	d0,d1
	mulu	#-$2,d0	; d0 = $4 * $FE, d0 = $3F8
	muls	#-$2,d1	; d1 = $4 * -$2, d1 = -$8 ($F8)</pre><br><br>

As you can see there is a difference. The last three instructions are different from the ones you have seen already. <b>NOP</b> does nothing. It really doesn't XD. It is used to make <b><u>VERY</u></b> small delays, to let hardware parts to act. Example (it doesn't do anything):<br><br>
<pre class="68k">	move.w	#4,d0
	nop		;it really does nothing =P
	moveq	#0,d0</pre>

The second instruction is <b>ILLEGAL</b>.
It's not a real instruction, it's just a random word (depending on the
assembler, normally TAS.B something) that will cause an illegal
instruction interrupt (interrupts will be covered later).<br><br>

And the last one, <b>DC</b>. This instruction is to store data in ROM. Can be used as <b>DC.B</b>, <b>DC.W</b> and <b>DC.L</b>. I'll better show an example:<br><br>
<pre class="68k">	dc.b	$12,$24,$31,$21,$21,$31,$31,$12
;this will store $1224312121313112 in ROM.

	dc.w	$0EEE, $0CCC, $0AAA, $0888, $0666, $0444, $0222, $0000
;a random palette ;)

	dc.l	$1,$123141,$14144144

	dc.w	%1110,1000</pre>

The possibilities are endless. Here's our first real example:<br><br>

<pre class="68k">	lea	(Palette_Sega).l,a0
	lea	($FFFFFA80).w,a1
	move.w	(a0)+,(a1)+
	rts

Palette_Sega:	dc.w  $EEE, $EEA, $EE4,	$EC0
		dc.w  $EE4, $EEA, $EEC,	$EEA
		dc.w  $EEA, $EEA, $EEA,	$EEA
		dc.w  $EEC, $EEA, $EE4,	$EC0
		dc.w  $EC0, $EC0, $EEC,	$EEA
		dc.w  $EE4, $EC0, $EA0,	$E60
		dc.w  $EEA, $EE4, $EC0,	$EA0
		dc.w  $E80, $E00</pre>

It loads the palette address to <b>a0</b> and the palette address in ram to <b>a1</b>. Copies first color/colour <b>(a0)</b> to the first color in ram, then increments <b>a0</b> and <b>a1</b>. This really works =)<br><br>

Now what I promised - editing the code in the game. Open the <b>Sonic 1 disassembly</b>. Search it for <b>label 'PlayLevel:'</b>. There's a bunch of variable settings (a list of RAM variables is in <b>Guides&gt;Sonic 1&gt;RAM</b>).
I commented some stuff there. The thing we are interested in is editing
the lives count. The RAM location that stores lifes is <b>$FFFFFE12</b>. Change it to <b>4</b>.
Now, how do we assemble our edited code? I made a batch program which
assembles a code using SNASM68K. Call it 'asm.bat' or something and put
this in there:<br><br>
<pre class="68k">@echo off
echo asm.bat by drx

if "%1"=="" goto noinput

snasm68k.exe -emax 0 -p -o ae- %1.asm, %1.bin
goto end

:noinput

set /p input=  Input asm file   
set /p output= Output bin file   

snasm68k.exe -emax 0 -p -o ae- %input%, %output%

:end</pre>

There are two options of using it:<br><br>
1) Open it normally, type the input asm file and the output bin file
and it will assemble it
2) Go to the command line, type 'asm FILE' (without the quotes) and
it'll assemble FILE.asm to FILE.bin (it'll show the errors and the
window won't disappear in Win XP...)<br><br>

Now open the assembled file in an emulator. If you made it correctly, you should have 4 lives.<br><br>

Few things that are different in disasm from what I said before:<br><br>

1) <b>Bcc.s</b> or <b>Bcc.w</b> - this is wrong. You shouldn't use this, assembler will choose the correct one. <b>.s</b> is a one byte branch (-127..127) and <b>.w</b> is a two bytes branch (-32767..32767).<br><br>

2) There are instructions like <b>CMPI</b>, <b>CMPA</b>, <b>ADDA</b>, <b>ADDI</b> etc. That are just different types of the same instruction, you should just use <b>CMP</b> or <b>ADD</b> and the assembler will choose the correct one. The exception is <b>MOVEM</b>, <b>MOVEQ</b>, <b>ADDQ</b>, <b>xQ</b> etc.<br><br>

If you understand 90% of what has been written here, then it's good.
Hell, if you understand 20% of it, it's good XD. Assembler is not easy,
and a smallest error can ruin all your work.<br><br>

<i>"50% of the time spent programming an assembly language is correcting bugs."</i> - an assembly programmer XD<br><br>

The more time you spend on it, the better you are at fixing those bugs XD<br><br>

The most common bugs:<br><br>

1) You MUST put a <b>#</b> before an immediate value (<b>move.b #1,d0</b>) If you won't, this will be treated as an address.<br>

2) When you move a longword into <b>d0</b>, then a byte, the upper bytes still are there! To clear <b>d0</b> use <b>moveq #0,d0</b> or <b>clr.l d0</b><br>
3) This is the most annoying bug ever. <b>PC</b> must be <u>ALWAYS</u>

even. if you insert one byte somewhere, the code after it will be all
in odd addresses and the Genesis will crash. Also, try to load even ROM
addresses into address registers, because that can be nasty, too. The
best method to make sure an address will be always even is to put this
before it:<br><br>
<pre class="68k">	align $2</pre>
	
or this:<br><br>
<pre class="68k">	even</pre>
This will make it always even. But remember, don't put it between the
code, or it'll give you an illegal operation (it SHOULDN'T, because the
<b>PC</b> is always even but...)<br><br>

You can align as many bytes as you want, eg putting this at the end will make the output rom size be a multiplication of <b>$80000</b>:<br><br>
<pre class="68k">	align $80000</pre>
With the knowledge you have (I hope you do) you should be able to find
and edit the basic things like lives. Remember, the best method to
learn is to discover everything on your own. Don't rely only on my
guides, be creative =)<br><br>

Next chapter will cover loops and arrays (not C-like arrays...). Have fun.