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: 2367+5FD62367 + 5FD6

  • Example 5.2: AC221EE8AC22 - 1EE8

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: 2357+D62357 + D6

  • Signed Extension:

    • Example 5.4:

      • a) 2357+D62357 + D6

      • b) 2357+6D2357 + 6D

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) 00F500F5

      • b) 10EC10EC

  • Signed Truncation:

    • Example 5.6: Truncate each 16-bit value to an 8-bit value or state that it is not possible.

      • a) 00450045

      • b) 00F500F5

      • c) FFD1FFD1

      • d) FF66FF66

      • e) F280F280

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 3030

    • b) BRA 10-10

  • 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: FinalValueInitialValueFinal Value – Initial Value.

  • 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 2000h2000h.

    • a) Initial Address: 20002000, Destination: 20602060

    • b) Initial Address: 20002000, Destination: 20852085

    • c) Initial Address: 20002000, Destination: 1FF01FF0

    • d) Initial Address: 20002000, Destination: 1F801F80

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 30003000

    • b) BRA 1000-1000

  • 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 45004500, regardless of the instruction's location in memory.