The CMP (Compare) instruction in the 8085 Microprocessor is a crucial instruction used for comparing the content of the Accumulator (A) with another 8-bit value.
How it works (The Mechanism):
Conceptual Subtraction:CMP effectively performs a subtraction of the operand from the Accumulator's content (i.e., A - operand).
Non-Destructive: Crucially, the result of this subtraction is NOT stored anywhere. The Accumulator's content remains unchanged.
Flag Affected: The only purpose of the CMP instruction is to update the CPU's flags (specifically, the Zero, Carry, Sign, Parity, and Auxiliary Carry flags) based on the outcome of the conceptual subtraction.
Syntax and Operands:
The CMP instruction has two forms:
CMP R (Compare Register):
Syntax:CMP R (where R can be any general-purpose register: B, C, D, E, H, L).
Operation: Compares the content of the Accumulator (A) with the content of register R.
Example:CMP B (Compares A with B)
CMP M (Compare Memory):
Syntax:CMP M
Operation: Compares the content of the Accumulator (A) with the content of the memory location pointed to by the HL register pair.
Example:CMP M (Compares A with [HL])
CPI data (Compare Immediate Data):
Syntax:CPI data (where data is an 8-bit immediate value).
Operation: Compares the content of the Accumulator (A) with the 8-bit immediate data provided in the instruction itself.
Example:CPI 05H (Compares A with the hexadecimal value 05)
Flag Effects (Most Important Part):
The updated flags are then used by conditional jump instructions to make decisions in the program flow.
Let's say we are comparing A with Operand. The flags are affected as follows by the A - Operand operation:
Zero Flag (Z):
Set (Z=1) if A = Operand (because A - Operand would be 0).
Cleared (Z=0) if A ≠ Operand.
Carry Flag (C):
Set (C=1) if A < Operand (meaning a borrow was needed for the subtraction).
Cleared (C=0) if A ≥ Operand (no borrow needed).
Sign Flag (S):
Set (S=1) if the result of A - Operand is negative (MSB of the result is 1).
Cleared (S=0) if the result is positive or zero (MSB of the result is 0).
Parity Flag (P):
Set (P=1) if the result of A - Operand has an even number of set bits.
Cleared (P=0) if the result has an odd number of set bits.
Auxiliary Carry Flag (AC):
Set (AC=1) if a borrow was needed from bit 4 to bit 3 (during the conceptual subtraction).
Cleared (AC=0) otherwise.
How CMP is Used in Programs (With Conditional Jumps):
The real power of CMP comes when it's immediately followed by a conditional jump instruction, allowing your program to branch based on the comparison result.
Let's assume A contains a value, and we compare it with a value B (from a register, memory, or immediate data):
To check if A is equal to B (A == B):
assembly
MOV A, 25H ; A = 25H
CPI 25H ; Compare A with 25H
JZ EQUAL_LABEL ; Jump if Zero flag is set (A == 25H)
JMP NOT_EQUAL ; Otherwise, jump to NOT_EQUAL
EQUAL_LABEL: ; Code for when A is equal to 25H
...
To check if A is not equal to B (A ≠ B):
assembly
MOV A, 10H
CMP B ; Compare A with B
JNZ NOT_EQUAL_LABEL ; Jump if Zero flag is cleared (A ≠ B)
JMP EQUAL ; Otherwise, jump to EQUAL
NOT_EQUAL_LABEL: ; Code for when A is not equal to B
...
To check if A is less than B (A < B):
assembly
MOV A, 05H
CPI 0AH ; Compare A with 0AH
JC LESS_LABEL ; Jump if Carry flag is set (A < 0AH)
JMP NOT_LESS ; Otherwise, jump to NOT_LESS
LESS_LABEL: ; Code for when A is less than 0AH
...
To check if A is greater than or equal to B (A ≥ B):
assembly
MOV A, 0FH
CMP C ; Compare A with C
JNC GREATER_EQUAL_LABEL ; Jump if Carry flag is cleared (A ≥ C)
JMP LESS ; Otherwise, jump to LESS
GREATER_EQUAL_LABEL: ; Code for when A is greater than or equal to C
...
To check if A is greater than B (A > B):
(This requires checking both the Zero and Carry flags)
assembly
MOV A, 20H
CPI 10H ; Compare A with 10H
JZ NOT_GREATER ; If A == 10H, it's not greater, so jump
JNC GREATER_LABEL ; If Z is 0 (A!=10H) AND C is 0 (A >= 10H), then A > 10H
NOT_GREATER: ; Code if A is not greater than 10H (i.e., A <= 10H)
...
GREATER_LABEL: ; Code if A is greater than 10H
...
In Summary:
The CMP instruction is fundamental for implementing decision-making logic in 8085 programs. It allows the CPU to compare values without altering the data being compared, and then to use the resulting flag states to control the program's flow through conditional jump instructions.
Alternative logic for A > B:
assembly
CMP B
JNC GREATER_OR_EQUAL ; A >= B. Now check if A is strictly greater
JMP LESS ; A < B
GREATER_OR_EQUAL:
JNZ GREATER_LABEL ; A != B. Since we know A >= B and A != B, then A > B
JMP EQUAL_CASE ; A == B
GREATER_LABEL:
...
To check if A is less than or equal to B (A ≤ B):
(This requires checking both the Zero and Carry flags)
assembly
MOV A, 07H
CPI 0AH ; Compare A with 0AH
JC LESS_EQUAL_LABEL ; If A < 0AH (C=1), then jump
JZ LESS_EQUAL_LABEL ; If A == 0AH (Z=1), then jump
JMP GREATER ; Otherwise, A > 0AH
LESS_EQUAL_LABEL: ; Code for when A is less than or equal to 0AH
...