When we first looked at registers, I mentioned that there was a Flag Register. This differs from the other registers in that each of the Bits of the register has a specific meaning that some of the operations rely on.
- Bit 7: Sign Flag
- Bit 6: Zero Flag
- Bit 5: Not Used
- Bit 4: Half Carry Flag
- Bit 3: Not Used
- Bit 2: Parity / Overflow Flag
- Bit 1: Add / Subtract Flag
- Bit 0: Carry Flag
The Sign Flag is used to store the state of the most-significant bit of the Accumulator (which represents the sign of the value using twos-complement notation).
The Zero Flag is used by certain operations to specify if its result was Zero and for comparisons.
The Half Carry flag is used when using decimal mathematics.
The Parity / Overflow Flag is used to determine if various registers have overflowed based on the instruction being performed and is also used to indicate data parity when inputting a byte from an I/O device.
The Add/Subtract Flag is used for decimal mathematics to distinguish between the
The Carry Flag is mainly used to indicate a carry for an addition or subtraction instruction.
At the time of writing, I have implemented a few operations that use the flag registers (8 bit
Add) and some of them have been very easy.
Flags such as the sign flag and the zero flag are very straight forward and the
Subtract flag has been easy so far.
The flag that has caused me the most trouble has been the half carry flag, which is to be set only if a carry from bit 3 occurs.
Originally, I interpreted this as if any addition occurs that results in a value of 32 or above, then the flag should be set. In which case, 64 + 64 would cause the flag to be set. This is incorrect.
The correct way of calculating whether a carry from bit 3 has occurred is to sum the lower nibbles of the values and then see if the value is 32 or above. (So I wasn't that far off).
The difference between the two approaches above is that by only summing the lower nibbles, I am explicitly dealing with a carry from the lower nibble to the higher nibble (which is the point of the flag) instead of just basing it on the value of the result.