MC logo

Branch Coding

  Coding Examples

<<Load Coding Example Jump Coding>>
Branch instructions have a few peculiar twists. First, note that branch instructions are relative to the PC. That is, they contain not the address where the instruction is branch to, but the difference between the address of the branch instruction and the address of the target. Furthermore, the offset is not from the branch instruction itself, but from the following instruction. This is because, when the branch instruction is executed, the PC has already been incremented. Finally, since all instructions are four bytes, all of these differences are multiples of four. Therefore, the instruction need not store the low two bits of the offset (which are always zero). This effectively allows branches to go four times as far.

Consider the instruction beq $s4, $t4, fred. Assume that fred is located at address 0x00400410, and the beq instruction is located at 0x004003AC.

  1. Take the difference between the target and the instruction after the branch: 0x00400410 - (0x004003AC + 4) = 0x60.

  2. Shift the offset right two bits (divide it by four). This gives 0x18.

  3. Look up the values of the registers: $s4 is 20 = 101002, $t4 is 12 = 011002.

  4. Look up the instruction to find its type and format. (You'll find it on p. A-62.) Plug the numbers into the format. Pad the offset to 16 bits. Just to keep you off guard, the registers are coded in the same order as in the assembler instruction.
    00010010100011000000000000011000

  5. Regroup the bits.
    00010010100011000000000000011000

  6. Represent as a hex number: 128C0018
<<Load Coding Example Jump Coding>>