I'm in the process of writing an emulator for the Intel 8080.
The description of the CMP instruction from Intel's 8080 programming manual (see page 20) says the following:
The specified byte is compared to the contents of the accumulator. The comparison is performed by internally subtracting the contents of REG from the accumulator (leaving both unchanged) and setting the condition bits according to the result. In particular, the Zero bit is set if the quantities are equal, and reset if they are unequal. Since a subtract operation is performed, the Carry bit will be set if there is no carry out of bit 7, indicating that the contents of REG are greater than the contents of the accumulator, and reset otherwise.
The following example is given:
Example 1: Assume that the accumulator contains the number 0AH and the E register contains the number 05H. Then the instruction CMP E performs the following internal subtractions:
Accumulator = 0AH = 0 0 0 0 1 0 1 0
+ (-E Register) = -5H = 1 1 1 1 1 0 1 1
---------------
1 0 0 0 0 0 1 0 1
^
|
+---- carry = 1, causing the carry bit to be reset
The accumulator still contains 0AH and the E register still contains 05H; however, the Carry bit is reset and the zero bit reset, indicating E less than A.
This makes sense to me. If there is a carry out as a result of adding the 2's complement of a number to the accumulator, the number is less than the accumulator.
This seems to break however if you compare with zero. For example, suppose the accumulator contains 10 (0AH) and the E register contains 0. Then the instruction CMP E performs the following subtraction:
Accumulator = 0AH = 0 0 0 0 1 0 1 0
+ (-E Register) = -0H = 0 0 0 0 0 0 0 0
---------------
0 0 0 0 0 1 0 1 0
^
|
+---- carry = 0, causing the carry bit to be set
Here it seems that the carry bit would be set due to there being no carry out of the high order bit position (as is the case with subtraction instructions on the 8080). This however would indicate that 0 is greater than 10.
I know that the carry bit should be reset in my example comparison above, but what's causing it to be reset? Is there some sort of internal circuitry in the 8080 that special cases 0 for subtraction operations?
00001010 - 00000000
doesn't require borrowing out of the highest bit, the carry bit will be set. Or to put it another way, negating one of the operands, adding, and complementing the carry doesn't perform the same operation as subtraction.