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. For example: move.b #1,($FFFFFE10).w 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) Examples: move.b #1,($FFFFFE12).w moves 1 (byte) to $FFFFFE12 in RAM (lives in Sonic 1) move.l ($12346).l,d0 moves a longword from address $12346 in ROM to data register d0. move.w (a0)+,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: move.b ($FFFFFE12).w,($FFFFFE13).w 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 Exapmle: move.l (a0)+,d0 Hope you understand =) Now, the opposite of adding is substracting. You do it using SUB instruction. Exapmles: sub #5,a0 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... >> |