Before we take a closer look at the condition codes and how we can use them to alter the flow of a program - that is, how we can implement loops, if then else etc, we need to take a braek and discuss the differences between signed and unsigned numbers.
When we MOVE some data into a data register the same number can actually mean two different things. Confused ? You will be !
If we use an 8 bit number as an example, the data $FF can either mean 255 or minus one. In a 16 bit example, $FFFF can mean 65535 or -1 and in a 32 bit long word, $FFFFFFFF means 2^31 or -1. The important thing to remember is that it is you, the programmer, who decides which version is in use at any particular time.
Ok, how does it work ? The 68000 family of processors can use signed or unsigned numbers. If the signed version is in use then the number will be either negative (less than zero) or positive (zero or greater). If unsigned numbers are being used then the value will always be positive. How can the processor tell the difference ?
The answer to the question 'is this number signed or unsigned ?' is either 'yes' or 'no' equivalent to one or zero in binary terms. This implies that a single bit can be used to hold the sign of the number and this is exactly how it happens. By convention the most significant bit of the number holds the sign. A one indicates that the number is negative while a zero indicated that it is not.
Those of you who are thinking ahead of me now might well be saying 'but surely using a single bit of the register will reduce the amount of numbers that can be represented by a factor of two ?'. Not quite.
In binary, the numbers representing the hexadecimal values $00 to $0F will all fit into a half byte or nibble. A nibble is 4 bits and each bit represent a single power of two in the number.
Just as 1231 means (1 * 10^3) + (2 * 10^2) + (3 * 10^1) + (1 * 10^0), which is, (1 * 10 * 10 * 10) + (2 * 10 * 10) + (3 * 10) + (1 * 1) which is, 1000 + 200 + 30 + 1 which is the number we have at the start of all this, the same is true in binary.
The binary nibble 1010 is (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0), which is (1 * 2 * 2 * 2) + (0 * 2 * 2) + (1 * 2) + (0 * 1), which is 8 + 0 + 2 + 0, which is 10 in decimal with converts to $0A in hexadecimal.
All the possible values that can be held in an unsigned nibble are 0000 (zero) up to 1111 (15 or $0F) and conversion is a matter of adding up each power of two in the number. From the right we have 2^0 which is simply one. Then 2^1 or two and so on.
In an unsigned nibble the most significant bit (2^3) is used to hold the sign, so all numbers below unsigned 7 are positive while those 'above' 7 are actually negative and so are actually below 7.
If the highest bit was not the sign bit it would represent 2^3 or 8. To convert into a signed value simply negate the 8 to get minus 8, and add all the other bit values to it. Taking the same binary example of 1010 as above, this is now :
(-1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0). This eventually gives minus 8 plus 2 which is minus 6. This now implies that for a signed number the range is -8 to +7 which is still a possible 16 values as with the unsigned version, just shifted slightly down the number scale.
That is the only difference between signed and unsigned numbers. The ranges of values in a byte are minus 128 to plus 127, in a word it is minus 32768 to plus 32767 and for a long word it is minus 2147483648 to plus 2147483647.
When dealing with signed numbers any number which has a 8, 9, A, B, C, D, E or F in the most significant digit (hex that is) is negative. All the rest are positive. I find the quickest way to find the equivalent negative value is to subtract from 2^number of bits. For example -1 in a byte is 2^8 -1 which is 256 - 1 which is 255. 255 in hex is $FF which is the 8 bit representation of -1. Similarly, -10 is 256 - 10 = 146 which is $F6. Use 65536 for 16 bit words and 4294967296 for 32 bit long words.
Enough for now. Just remember when coding a program in assembelr that numbers can be two different values at the same time. You determine which one is appropriate at any one time. It is far easier to consider unsigned numbers all the time but this might not be applicable. Writing a program to record the number of sheep jumping over a fence need never use signed numbers, while the amout of money in your bank account oprobably will. Just remember to be consistant.