The Program Counter and Branch Targets

A program counter identifies which instruction is fetched under a processor's execution model. Branches can change the next chosen address.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace two paths
  2. Clarify relative offsets
  3. State whether the offset is measured from the current or next address
  4. Test direction and offset units

Trace two paths

In an example with four-byte instructions at addresses 12, 16, and 20, sequential execution after 12 fetches 16. A taken branch to 20 skips 16. Fetch and execute locates when that address changes.

Clarify relative offsets

Some instruction sets express a target relative to a specified base and scale. A stored offset of two might count instructions instead of bytes. A unit-aware interface prevents mixing those interpretations.[1]

State whether the offset is measured from the current or next address

In a toy word-addressed machine, suppose PC is 20, a branch is one word long and the displacement is minus three relative to the next instruction. The sequential address is 21 and the taken target is 18. See signed integer fields.

Current PC = 20
Sequential PC = 21
Taken PC = 21 - 3 = 18
Fall-through when condition false: 21

Test direction and offset units

An offset measured in bytes can yield a different target in a word-addressed description. Include a negative displacement, zero displacement and one untaken branch. Decode the displacement with signedness before adding it.

References

  1. Noam Nisan and Shimon Schocken, From Nand to Tetris: Course Projects ↩