# Program Control Instructions

So far, we've learned that processors use instructions to transfer data, perform arithmetic, perform logical operations, and shift bits.

But there's another important question:

How does a CPU decide which instruction to execute next?

Normally, instructions execute sequentially:

Instruction 1 ↓ Instruction 2 ↓ Instruction 3 ↓ Instruction 4

But real programs don't always execute in a simple sequence.

They need:

Decisions

Loops

Function calls

Returns

Jumps

Interrupt handling

This is where program control instructions come in.

What Are Program Control Instructions?

Program control instructions are instructions that change or control the normal sequence in which instructions are executed.

Instead of simply executing the next instruction in memory, the CPU may be instructed to:

Jump to another address

Branch depending on a condition

Call a function or procedure

Return from a function

Respond to an interrupt

Halt execution

These instructions control the flow of a program.

The Program Counter

To understand program control instructions, we first need to understand the Program Counter (PC).

The Program Counter is a special-purpose register that holds the address of the instruction that the CPU will fetch next.

Conceptually:

Program Counter │ ▼ Instruction Memory │ ▼ Instruction

Normally, after an instruction is fetched, the PC advances so that execution continues with the next instruction.

For example:

1000 → 1004 → 1008 → 1012

The exact amount by which the PC changes depends on the processor's instruction format and architecture.

Program control instructions can change this normal sequence.

Sequential Execution

Consider a simple sequence:

Address Instruction

1000 LOAD R1 1004 ADD R2 1008 STORE R1 1012 ...

Normally:

1000 ↓ 1004 ↓ 1008 ↓ 1012

The CPU proceeds through the instructions in order.

But suppose the instruction at 1008 says:

Jump to 2000

The execution flow becomes:

1000 ↓ 1004 ↓ 1008 │ │ JUMP ▼ 2000 ↓ 2004 ↓ 2008

The program has changed its normal flow.

1.  Unconditional Jump
    

An unconditional jump always transfers execution to a specified target address.

For example:

JUMP 2000

Conceptually:

PC ← 2000

The next instruction is fetched from address 2000.

There is no condition that needs to be checked.

2.  Conditional Branch
    

A conditional branch changes the execution flow only when a specified condition is satisfied.

For example:

BEQ 2000

could conceptually mean:

Branch if Equal

The CPU checks the appropriate condition, often using status flags.

If the condition is true:

PC ← 2000

If the condition is false, execution continues normally.

Conceptually:

```plaintext
         Condition?
          /     \
        Yes      No
         ↓        ↓
     Target     Next
```

Conditional branches are fundamental to implementing decisions and loops.

Conditions and Status Flags

Conditional branches often rely on status or condition flags.

These flags can be affected by previous operations.

For example, suppose:

R1 = 10 R2 = 10

A comparison may establish that the values are equal.

The processor can then use a conditional branch:

CMP R1, R2 BEQ target

Conceptually:

Compare ↓ Update Flags ↓ Check Condition ↓ Branch or Continue

This allows programs to make decisions.

3.  Function or Procedure Call
    

Programs are often divided into smaller reusable sections called functions or procedures.

A program control instruction can transfer execution to such a section.

For example:

CALL function

Conceptually:

Main Program │ │ CALL ▼ Function │ │ RETURN ▼ Main Program

A call usually needs to preserve information about where execution should return.

Depending on the architecture, this return information may be stored using a register, stack, or another mechanism.

4.  Return
    

A return instruction transfers execution back to the location saved by a previous function or procedure call.

Conceptually:

CALL ↓ Function ↓ RETURN ↓ Instruction after CALL

This mechanism is fundamental to function calls.

5.  Interrupt Instructions
    

Processors also need mechanisms for handling interrupts.

An interrupt is a signal or event that causes the processor to temporarily change its normal execution flow so that it can handle an event.

Conceptually:

Normal Program │ │ Interrupt ▼ Interrupt Handler │ │ Return ▼ Normal Program

Interrupt mechanisms vary considerably between architectures.

They are commonly used for events such as:

Hardware devices requiring attention

Timers

Input events

Communication events

The CPU saves or otherwise preserves enough state to resume the interrupted program according to the architecture's rules.

6.  Halt
    

A halt instruction tells the processor to stop normal instruction execution.

For example:

HALT

The exact behavior depends on the architecture.

A halt operation may place the processor into a stopped or low-activity state until a suitable event, such as a reset or interrupt, occurs.

Program Control and Loops

Loops are one of the most important uses of program control instructions.

Consider:

while (condition) { operation; }

At the machine level, this can be implemented using a combination of:

Comparison

Conditional branch

Arithmetic

Other instructions

Conceptually:

```plaintext
    ┌─────────────┐
    │   Compare   │
    └──────┬──────┘
           │
      Condition?
      /       \
    Yes        No
     │          │
     ▼          ▼
 Operation    Continue
     │
     └──────────► Compare
```

The branch sends execution back to an earlier instruction.

That's what creates the loop.

Program Control and If/Else

Conditional branches can also implement decisions.

For example:

if (condition) A; else B;

Conceptually:

```plaintext
   Condition
    /     \
  True    False
   ↓        ↓
   A        B
```

The CPU uses comparison and program control instructions to choose the appropriate path.

Program Control and the Program Counter

The connection between program control instructions and the Program Counter is extremely important.

A normal instruction sequence might do:

PC → Next Instruction

A jump or branch can instead do something conceptually like:

PC ← Target Address

Therefore, program control instructions can directly affect the value used to determine where the CPU fetches its next instruction.

This is why they are called program control instructions.

Direct and Indirect Control Transfers

Program control instructions can specify their target in different ways.

For example, a branch target might be:

Explicitly encoded in the instruction

Calculated relative to the current PC

Stored in a register

Obtained through another addressing mechanism

This connects program control instructions to the addressing modes we'll study later.

Common Program Control Instructions

Type

Purpose

JUMP

Unconditionally transfer execution

BRANCH

Conditionally transfer execution

CALL

Transfer execution to a function/procedure

RETURN

Return from a function/procedure

INTERRUPT

Handle an interrupt/event

HALT

Stop normal processor execution

The exact instruction names vary between processor architectures.

Why Program Control Instructions Matter

Without program control instructions, programs would mostly execute from beginning to end without being able to make meaningful decisions or repeat sections of code.

They make possible:

if statements

else statements

Loops

Functions

Recursion

Error handling

Interrupt handling

Branching

Jumps

In other words, they give programs the ability to change their execution path.

Final Thoughts

Program control instructions control the sequence in which instructions are executed.

The most important concepts are:

Jump → Always change the execution target

Branch → Change the target when a condition is satisfied

Call → Enter a function or procedure

Return → Come back from it

Interrupt → Temporarily handle an event

Halt → Stop normal execution

All of these mechanisms are closely connected to the Program Counter, because changing program flow generally means changing where the processor gets its next instruction.

We've now covered the major instruction categories.

But there is still one fundamental question:

How is an instruction actually arranged inside the machine?

That brings us to:

Instruction Format.
