# Sega CD Development

Home Sega CD SLO 32X Transfer ConvSCD

#### Part III: Addition, Subtraction, and Two's Complement

You may have wondered how on earth a computer stores negative numbers if all it understands is 1s and 0s (okay you probably weren't wondering this, but I'm covering it anyway). One solution might be to use the highest bit to represent the sign of the number. If this bit is set then the number is negative, if it's not set the number is positive. While that statement is correct, it doesn't represent the whole truth, but before I explain the rest of it you need to understand something else first.

What happens when you add a 1 to 9 (decimal)? You run out of room in the current digit, so you do a carry into the next digit making the result 10. The same thing happens when you add 1 to \$FF (hex). All the bits are filled in the first byte, so a carry is done into the first bit of the next byte making the result \$100, but what happens if the variable you are storing the result in is only a byte wide? The upper digit is lost and you are left with \$0.

So in byte arithematic, \$FF + 1 = 0. What other value when added to 1 gives us 0? x + 1 = 0 With some simple algebra we find that -1 is the answer. It turns out that the nature of fixed width arithematic gives us an easy way to make negative numbers. Let me give you another example to make it clearer. What is the result when you subtract \$2 from \$100? You get \$FE. Now if this was byte wide math, then that 1 wouldn't be there and you would borrow from a digit that doesn't exist so \$0 - \$2 = \$FE or -2. Hopefully you are beginning to see a patern here. When you count backwards from 0 on a computer, the numbers loop around so -1 = \$FF, -2 = \$FE, -3 = \$FD, etc.. Keep in mind that with a larger datawidth these values change. For a word, -1 = \$FFFF, -2 = \$FFFE and so on.

That is why offsets were sign-extended in the last tutorial section. If they weren't negative offsets would become positive when they were stretched to fit a longword wide address. \$FFFF is a positive number if it's a longword, but if you copy the highest bit into the extra bits in the longword, it becomes negative again, \$FFFFFFFF.

Okay, enough of the theory behind how the computer works. It's time for some actual processor instructions.

You'll notice that the syntax for add looks just like the syntax for move. A lot of instructions on the 68K work this way which is convenient because that way you can have some vague idea what is going on even if you don't know the exact function of the instruction. You'll always know what the source and destination are. It also means you can use all those fancy addressing modes for most instructions.

Examples:

Equivalents:
d0 = d0 + 2
d0 = d0 + d1

Those of you familiar with C can think of this as the += operator. If you don't know C, don't worry about that comparison. Just look at the Equivalents.

sub.[size] [source], [destination]

Pretty much the same idea here, but be careful. With subtraction it matters what order the operands come in so you have to remember which operand this instruction subtracts from which. It works like this: = - .

Examples:
sub.b #3, d4
sub.w d0, d1

Equivalents: d4 = d4 - 3
d1 = d1 - d0

That's it for part III, have fun.

Part II Sega CD Home Part IV