Module 5: Unconditional Branches - Detailed Notes
Module 5: Unconditional Branches
Objectives
Perform hexadecimal addition and subtraction.
Use unconditional branch and jump instructions.
Lecture Outline
Instructions
Hexadecimal Addition and Subtraction
Extending Hexadecimal Numbers
Truncating Hexadecimal Numbers
Relative Addressing (REL with 8-bit signed offset)
Calculating Branch Destinations
Relative Addressing (REL with 16-bit signed offset)
JUMP Instruction
Instructions
BRA, LBRA: Always branch to an instruction, uses relative addressing.
BRN, LBRN: Never branch to an instruction, uses relative addressing.
JMP: Jump to an instruction at a specified address, uses extended or indirect addressing.
Hexadecimal Addition and Subtraction
Adding and subtracting in hexadecimal is similar to decimal arithmetic.
Carries and borrows are worth 16 instead of 10.
Example 5.1:
Example 5.2:
Extending Hexadecimal Numbers
When adding or subtracting numbers with different lengths, the shorter number must be extended to match the longer number's number of digits.
Unsigned Extension:
Example 5.3:
Signed Extension:
Example 5.4:
a)
b)
Truncating Hexadecimal Numbers
Microcomputers often use a fewer number of bits to represent values to save time and space.
The process of extending numbers can be reversed with care.
Unsigned Truncation:
Example 5.5: Truncate each 16-bit value to an 8-bit value or state that it is not possible.
a)
b)
Signed Truncation:
Example 5.6: Truncate each 16-bit value to an 8-bit value or state that it is not possible.
a)
b)
c)
d)
e)
Relative Addressing (REL with 8-bit signed offset)
Effective Address Calculation: Add the operand (as a signed number) to the value in the Program Counter (PC).
The effective address is then loaded into the PC, and the program executes from that new address.
Format: One-byte hexadecimal value representing a signed value.
Example 5.7:
a) BRA
b) BRA
The value entered into machine code is the number of bytes forward or backward from the byte after the entire branch instruction, including the operand.
Calculating Branch Destinations
The change in the Program Counter is calculated as: .
Determine if the result can be represented as a one-byte signed value.
Example 5.8: Calculate the offset to the destination specified from the BRA instruction starting at address .
a) Initial Address: , Destination:
b) Initial Address: , Destination:
c) Initial Address: , Destination:
d) Initial Address: , Destination:
Relative Addressing (REL with 16-bit signed offset)
Effective Address Calculation: Add the operand (as a signed number) to the value in the PC.
The effective address is loaded into the PC, and the program executes from that new address.
Format: Two-byte hexadecimal value representing a signed value.
Example 5.9:
a) BRA
b) BRA
Machine code represents the number of bytes forward or backward from the byte after the branch instruction (including the operand).
With a 16-bit offset, all memory addresses are reachable, so the offset calculation is always valid.
JUMP Instruction
Key Difference Between JUMP and BRANCH:
Branch: Uses a relative offset based on the current value of the Program Counter (PC).
Jump: Executes the next instruction at a fixed, known address or at a relative offset from an index register; it does not depend on the value of the PC.
Branches are generally preferred over jumps.
Conditional branches enable the process to make decisions by continuing execution or branching to a different instruction (covered in the next module).
Code should be address agnostic, meaning it does not need to be loaded to a specific starting address to function correctly.
Example 5.10: Write an instruction to cause the processor to start executing at address , regardless of the instruction's location in memory.