Program Control Instructions
In normal execution, instructions are fetched sequentially from memory. Program control instructions are used to change this sequential flow by modifying the Program Counter (PC). This allows the CPU to implement loops, decision-making (if-else), and function calls.
Common Program Control Instructions
- **Branch (BR):** An instruction that causes the CPU to jump to a specific address. It can be conditional or unconditional.
- **Jump (JMP):** Similar to a branch, but usually implies an unconditional transfer to a specific memory location.
- **Call and Return:** Used for subroutines. 'Call' saves the current PC address to the stack and jumps to the subroutine; 'Return' pops the address from the stack to resume the main program.
- **Skip:** An instruction that skips the very next instruction if a specific condition is met.
Status Bits (Condition Codes)
The Arithmetic Logic Unit (ALU) contains a **Status Register** (or Flag Register) that stores specific bits based on the result of the last arithmetic or logic operation. These bits are checked by conditional branch instructions.
| Flag | Symbol | Condition for Set (1) |
|---|---|---|
| Carry | C | Set if there is a carry-out from the most significant bit. |
| Zero | Z | Set if the result of the operation is zero. |
| Sign | S | Set if the result is negative (MSB is 1). |
| Overflow | V | Set if the result exceeds the register capacity (signed). |
Conditional Branch Instructions
These instructions only change the PC if a specific status bit condition is met. They are the foundation of 'if' statements in high-level languages.
JZ (Jump if Zero) ; Jumps if Z = 1
JNC (Jump if No Carry) ; Jumps if C = 0
JG (Jump if Greater) ; Jumps if (S XOR V) OR Z = 0
Common Mistakes to Avoid
- Confusing 'Carry' with 'Overflow' (Carry is for unsigned, Overflow is for signed math).
- Assuming every instruction updates the flags (some instructions, like MOV, often leave flags unchanged).
- Forgetting to save the Status Register during an Interrupt (can lead to logical errors after the interrupt ends).
- Thinking a 'Jump' and a 'Call' are identical (Call must always be paired with a Return).
Advanced Concepts
- Vector Interrupts vs. Non-Vector Interrupts
- Software Interrupts (Traps)
- Program Status Word (PSW)
- Delayed Branching in RISC
- Condition code evaluation for signed vs unsigned comparisons
Practice Exercises
- If R1 = 0xFF and we add 0x01, what will be the values of the C and Z flags?
- Explain the difference between an Unconditional Jump and a Conditional Branch.
- Write the logic condition (using S and V flags) to detect if one signed number is less than another.
- What happens to the stack when a 'Call' instruction is executed?
Conclusion
Program control instructions provide the 'intelligence' of a processor, allowing it to respond to data and make decisions. By utilizing status bits, the CPU can efficiently manage complex logical flows and maintain the modularity of software through subroutines.
Codecrown