News
Documents
Source Code
Downloads
Message Board


Sega Megadrive Assembly Programming Basics

Copyright, Lewis Bassett, December 2005


Chapter 4 - Assembly Language

Section 6 - Other Useful Instructions

In this final section of Chapter 4, I'll explain a few more final assembly instructions, before we move on to producing working Megadrive ROMS. These instructions don't really fit into any of the other sections, are very useful none the less.


NOP - No OPeration

NOP is a blank instruction. It does absolutely nothing except waste processor time, so why is it useful? Some parts of the Megadrive's hardware are actually slower than the processor. This means that sometimes we have to wait, and the NOP instruction is perfect.

Here's an example:

	move.w	#$8900,	$ff0000;
	nop;
	nop;
	move.w	#$9000,	$ff0000;
First we move a number into memory. Next, we wait for two instructions, and then we write another value. This is just an example. In real life we wouldn't have to wait before writing to RAM, since RAM is still very fast. However, for other parts of the Megadrive's hardware, we would have to use NOP instructions to wait for the hardware to finish what it's doing, before we can use it again in our program. This will become more clear later on, when I explain some parts of the program.


CLR - Clear

The CLR instruction is used to clear the contents of a data register or a memory location to zero. This is much faster than using a MOVE instruction, although it is slighly slower than using a MOVEQ instruction.

Look at this example:

	clr.l	d0;
This clears the entire contents of the register d0, since I've specified that the CLR instruction works with a longword.

Here's the same example again:

	clr.w	d0;
This time, since I've specified that I'm only clearing a word, only half of the register is cleared.

Since CLR doesn't work with address registers, we can use a MOVE instruction to clear an address register:

	move.l	#$00000000,	a1;
Note that in order for this instruction to clear the whole register, we must specify that we're clearing a whole longword.


Summary

To summarise, assemblers are used to generate working machine code from an assembly source file.

Assembly source files always have an extension of '.asm', and contain assembly instructions and assembler directives.

Assembler directives are things like code labels, data definitions and variable names, and are converted during the first step of the conversion.

Code labels are ALWAYS on the left edge of the source file and contain NO spaces. One code label can only be used once. No duplicates.

Assembly instructions are always at least one space away from the left edge of the source file, with at least a space between each part of the instruction. Operands are always seperared with comas, with the source operand first, followed by the destination. Assembly instructions are converted during the second step of the conversion.

Assembled machine code files have an extension of '.bin' and are called ROMS. Megadrive ROMs have a specific format, which is explained in the next chapter.

Okay, this chapter was very very long, but if you've made it this far you'll be okay. Assembly language is very simple and anyone who has some common sense can use it. However, you may wish to have a re-read over everything that's been covered so far, before moving on to the next chapter. Note, that if at anytime you need a reference to the instructions described in this chapter, a summary of all the instructions is available here.



Chapter 4 - Section 5 Contents Chapter 5


Designed & maintained by Lewis AS Bassett
SEGA, Megadrive, Genesis, Sonic the Hedgehog, etc are all owned by Sega Enterprises Ltd