Flag register

The flag register in the Intel 8085 microprocessor is an 8-bit register that holds a set of status flags. These flags are used to indicate the results of arithmetic and logical operations performed by the arithmetic logic unit (ALU).

Register structure

The flag register in the 8085 is an 8-bit register, but only 5 of its bits are used as flags. The unused bits are normally set to 0 and have no particular meaning in the context of the flag state.

Individual flags

Sign flag (S)

Position: bit 7 of the flag register.

Function: the sign flag reflects the sign of the result of an arithmetic or logical operation. If the most significant bit (MSB) of the result is 1, this indicates a negative number in two's complement representation, and the sign flag is set to 1. If the MSB is 0, the result is positive and the sign flag is set to 0.

Example: consider an 8-bit subtraction operation whose result is the number 101010102. Since the most significant bit is 1, the sign flag will be 1, indicating a negative result.

Zero flag (Z)

Position: bit 6 of the flag register.

Function: the zero flag is set to 1 if the result of an arithmetic or logical operation is zero. Otherwise it is set to 0.

Example: if you perform the operation 5-5 and get the result 000000002, the zero flag will be set to 1.

Auxiliary carry flag (AC)

Position: bit 4 of the flag register.

Function: the auxiliary carry flag is used in BCD (binary-coded decimal) arithmetic. It is set to 1 if a carry or borrow out of the lower nibble (bits 0-3) into the upper nibble (bits 4-7) occurs during an arithmetic operation.

Example: when 07h and 09h are added in BCD arithmetic (000001112 + 000010012) a carry occurs from bit 3 into bit 4. The auxiliary carry flag will therefore be set to 1.

Parity flag (P)

Position: bit 2 of the flag register.

Function: the parity flag indicates the parity of the operation result. If the number of ones in the result is even, the parity flag is set to 1 (even parity). If the number of ones is odd, the parity flag is set to 0 (odd parity).

Example: if the result of the operation is 010101012, that is 4 ones (an even number), the parity flag will be set to 1.

Carry flag (CY)

Position: bit 0 of the flag register.

Function: the carry flag is set to 1 if a carry out of the most significant bit (MSB) occurs during an addition operation, or if a borrow occurs during a subtraction operation. It is also used in multi-byte arithmetic to handle carry or borrow between bytes.

Example: when FFh and 01h are added (111111112 + 000000012) a carry out of the most significant bit occurs, so the carry flag will be set to 1.

Importance of the flags

Conditional jumps: the flags are crucial for conditional jump instructions in 8085 assembly language. Instructions such as JC (jump on carry), JZ (jump on zero) and JP (jump on parity) use the flag state to determine whether to jump to another part of the program or continue sequential execution.

Arithmetic and logical operations: they provide information about the results of arithmetic and logical operations that can be used to check for errors, verify results and support further processing in the program.