Research / Megadrive Programming / Let's start
Ok, you need some stuff first. You have to download SNASM68k from our downloads section and a Sonic 1 disassembly
(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
Comments are marked by ; or *. The game code starts with label 'Entrypoint:'.
Ok, let's start with the most interesting part of programming, programming XD. Ok, first command, MOVE.
This command moves some data (constants, registers, RAM) into either
another register or RAM location. JohnnyUK nicely called it 'copying'.
Syntax: move source,destination
Looks complicated? Don't worry, I'll explain everything =) First, why .w, .b? If you use it, you will specify the data length being copied. If you don't the assembler will probably choose .l. .b is byte, .w is word and .l - longword.
I hope you get it =P Now, #x is a immediate value (a number, constant etc =P). Examples:
#5 - decimal 5
#$20 (hex) - decimal 32
#%1001 (binary) - decimal 9
#@777 (octal) - decimal 511
#'SEGA' - ascii text (remember, this can be tricky. 1 letter - use .b, 2 letters - use .w, 4 letters - use .l)
($FFFFFE10).w is an absolute address. Locations in ram HAVE TO be .w. ROM addresses usually are .l. Some programmers (forgot the name of the game...) use .w
for short ROM addresses, but that can cause serious bugs. Remember,
never write anything to a ROM address ;) (real Genesis will crash)
moves 1 (byte) to $FFFFFE12 in RAM (lives in Sonic 1)
moves a longword from address $12346 in ROM to data register d0.
moves a word from the location at A0 to d0, increments a0 by 2 (length of word) after that.
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 d0-d7 and can store any data. Address registers usually store pointers to some location in ROM or RAM, but they can also be used for data. Address registers are a0-a7, but don't use a7 because it's the stack pointer (I'll explain later). Registers are a nice way of storing temporary data. Eg.
move.b ($FFFFFE12).w,d0 ;copy value at $FFFFFE12 to d0 (this is a comment =P)
Of course this could be done with:
but this is an example =P The biggest reason why use registers is that you can modify them without modifying the source, eg.
move.b ($FFFFFE12),d0 ;copy $FFFFFE12 to d0
We have a new command, ADD. It adds a number to a register or a place in ram.
Syntax: add source,destination
What is the difference between a0 and (a0)?
a0 is the address, only a number. And (a0) is the value at this address. Have a look at this:
$FFFFFE12: 01 ;some value in ram
You can postincrement or predecreament address registers:
;a0 is incremented by 1 (length of byte) after the instruction
Hope you understand =)
Now, the opposite of adding is substracting. You do it using SUB instruction.
That would be the end of this chapter. In next lesson we'll learn about LEA,
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 =)
Back | Printer friendly
<< 1. Basics | 3. Compares, branches... >>