News
Documents
Source Code
Downloads
Message Board


Sega Megadrive Assembly Programming Basics

Copyright, Lewis Bassett, December 2005


Chapter 4 - Assembly Language

Section 4 - Comparing and Testing

In Section 3, I explained about logical instructions and how the affect the condition code register by setting flags according to the outcome of an operation. Logical instructions are used mostly to mask or merge data. Logical instructions can also be used to compare two data sources, and use the result to make a decision. This method however is very limited. For comparing data and making desicions, we use variations on two instruction types: comparision and test.


Cmp - Compare

A CMP instruction works by subracting the source from the destination, and setting the appropriate flags in the Condition Code Register, according to the result. The subtraction isn't actually stored anywhere, so both the source and the destination remain unchanged.

CMP operations between two different data sources use flags in the CCR to record the following outcomes:

Equal
Not Equal
Negative
Not Negative
Greater Than
Less Than


Let's compare two numbers, 1 and 8:

	move.b	#$01,	d0;
	move.b	#$08,	d1;
	cmp.b	d0,	d1;
First of all, notice that we have to specify if the instruction works with a byte, word or longword. The code above compares d1 to d0 by subtracting d0 from d1. The result is 7, which is less than 8, the number we compared it to, so the appropriate flags in the CCR are set to inform other instructions that the result of this operation was "Greater Than", "Not Equal" and "Not Negative".


Now, let's compare the numbers $fffe and $fffe:

	move.l	#$fffe,	d0;
	move.l	d0,	d1;
	cmp.l	d0,	d1;
The numbers are both the same! Which means the CCR will be set to store the outcome as "Equal" and "Not Negative".


Let's compare the first two numbers again, but this time, the other way around:

	move.b	#$01,	d0;
	move.b	#$08,	d1;
	cmp.b	d1,	d0;
In the above code, the CMP instruction subtracts 8 from 1! The result is -7, so the processor stores the outcomes "Less Than", "Not Equal" and "Negative" in the Condition Code Register.

CMP instructions are used to make decisions along with branch instructions, which I'll explain in the next section. For now, get used to the idea that CMP instructions compare two numbers, and store the result of the comparision temporarily into the CCR.


Cmpi - Compare Immediate

CMPI instructions work exactly the same as CMP instructions, except the sourch operand is an immediate value.

Here's an example:

	move.l	#$ff00ff11,	d0;
	cmpi.l	#$ff00ff22,	d0;
CMPI instructions require that the destination operand is a data register, so the value $ff00ff11 is copied into register d0. I then use the CMPI instruction to compare my immediate value ($ff00ff22) to the number stored in d0. The result of the operation is "Less Than", "Negative" and "Not Equal".


Tst - Test for Negative or Zero

The TST instruction is used to check if an address or register contains information. It works by checking the contents of the location provided, and setting either the Zero or the Negative flag, depending on whether the location is empty, or contains a negative number.

Check this example:

	move.b	#$00,		d0;
	tst.b	d0;
First I've copied a blank value into register d0. The TST instruction reads register d0, and because it's equal to zero, sets the zero flag in the CCR.

Here's the same example with a different number:

	move.b	#$30,		d0;
	tst.b	d0;
Here, because d0 is equal to $30, the TST instruction doesn't set any flags at all.

TST commands are useful for if you want to check if some information is present, before executing some code. In the next section, I'll explain about subroutines and labels, and how they work with the Condition Code Register.



Chapter 4 - Section 3 Contents Chapter 4 - Section 5


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