Branch instructions differ from jump instructions in that they do not supply the absolute address of the next instruction. When a branch instruction is executed, the destination address must be calculated relative to the current program counter. Thus, this set of instructions uses the relative addressing mode.
Calculating the Destination Address
In the relative mode, a relative address follows the opcode in memory. It is added to the current program counter to determine the destination address. The relative
address is a signed 8-bit number that indicates the number of bytes to branch forward 95 0100 86 4b LDAA #$4B ;load a number into AccA
0102 8b 23 ADDA #$23 ;Add another to AccA 0104 7e 01 80 JMP $0180 ;go store the result
• ;additional instructions •
•
0180 b7 00 00 STAA $0000 ;store the sum 0183 3f SWI
0100 86 4b LDAA #$4B ;load a number into AccA 0102 8b 23 ADDA #$23 ;Add another to AccA 0104 7e 01 80 JMP STORE ;go store the result
• ;additional instructions •
•
0180 b7 00 00 STORE STAA $0000 ;store the sum 0183 3f SWI
a) Jump using actual absolute address
b) Jump using label for absolute address
Destination Address
Destination Address
Figure 4.2 Jump Instructions
1. What is a label? How are labels used with jump instructions? 2. What kind of address is used by all jump instructions?
or backward in memory to the destination address. It is a displacement from the current position in memory. Since it is a signed 8-bit number, it must be sign extended to 16 bits before being added to the contents of the 16-bit program counter. The result of this operation produces the destination address, as shown in Equation 4.1.
DA = PC + rr Equation 4.1
DA = Destination Address PC = Address in the Program Counter
rr = Sign-Extended Relative Address
Figure 4.3 contains the same example used in Figure 4.1, except the jump instructions have been replaced with branch instructions. Instead of the absolute 16-bit address following a jump instruction, a relative address is provided following the branch instruction. This relative address must be added to the current program counter to calculate the destination address. In Figure 4.3, the branch instruction is assembled as two bytes in memory. The opcode for a BRA instruction is $20; it is located at location $0104. The operand field is located at location $0105 and is occupied by the relative address $7A. Figure 4.4 summarizes the sequence of events that occur to process this instruction.
Since the program counter contains $0180 at the end of this instruction, the instruction located at $0180 (STAA $0000) will be executed. The flow of the program was altered by this branch instruction, because the next sequential instruction was located at $0106. Each time a branch instruction is executed the processor must calculate the destination address in this manner. Additional examples of how this is done are shown in Example 4.1.
96
0100 86 4b LDAA #$4B ;load a number into AccA 0102 8b 23 ADDA #$23 ;Add another to AccA 0104 20 7a BRA $0180 ;go store the result
0106 • ;additional instructions
• •
0180 b7 00 00 STAA $0000 ;store the sum 0183 3f SWI
0100 86 4b LDAA #$4B ;load a number into AccA 0102 8b 23 ADDA #$23 ;Add another to AccA 0104 20 7a BRA STORE ;go store the result
0106 • ;additional instructions
• •
0180 b7 00 00 STORE STAA $0000 ;store the sum 0183 3f SWI
a) BRA using actual absolute address
b) BRA using label for absolute address
+007A Displacement PC Destination Address +007A Displacement PC Destination Address
Figure 4.3 BRA Instructions
Example 4.1
Problem: Calculate the destination address (DA) for each of the following branch instructions.
Address Machine Code Source Code a. 0109 20 6C BRA PAST b. 01E2 20 D3 BRA LOOP c. B723 20 9B BRA AGAIN
Solution: In each case, the contents of the PC must be determined. Then the sign-extended relative address must be added to the PC to calculate the destination address (Equation 4.1). The PC is the address of the next sequential instruction. Since each of these instructions occupies two bytes of memory, the address of the next instruction (PC) will be two greater than the address of the BRA instruction.
a. PC = $0109 + 2 → $010B rr = $6C, sign extend → +$006C DA = PC + rr → $0177 b. PC = $01E2 + 2 → $01E4 rr = $D3, sign extend → +$FFD3 DA = PC + rr → $01B7 c. PC = $B723 + 2 → $B725 rr = $9B, sign extend → +$FF9B DA = PC + rr → $B6C0
Calculating the Relative Address
All relative mode instructions calculate the destination address by adding the relative address to the current program counter. In Figure 4.3 it is shown that the source code
includes the actual destination address (i.e., BRA $0180), yet the machine code uses 97
Operation Description Functional Result Fetch BRA opcode
(1 machine cycle)
Contents of PC are copied to MAR.
PC is incremented.
Opcode is read from memory. Opcode is loaded into IR.
(PC) → MAR $0104 → MAR (PC) + 1 → PC $0105 → PC ($0105) → data bus $20 → data bus Opcode → IR $20 → IR Execute BRA
(2 machine cycles)
Contents of PC are copied to MAR.
PC is incremented. Displacement is read from memory. Calculate destination address. PC → MAR $0105 → MAR PC + 1 → PC $0106 → PC rr → MDR $7A → MDR PC + rr → PC $0106 + $007A = $0180 → PC
Figure 4.4 Process of Executing the BRA Instruction
the displacement. How does the assembler calculate the relative address from the destination address? The answer is found by subtracting PC from both sides of Equation 4.1, as shown in Equation 4.2.
DA = PC + rr Subtract PC from both sides DA – PC = PC – PC + rr Rearrange
rr = DA – PC Equation 4.2
Because the relative mode offset is a signed 8-bit value, the distance that a program can branch forward or backward is limited. The largest positive decimal number that can be represented in eight bits is 127, and the largest decimal negative number is –128. Thus, forward displacement is limited to 127 bytes, and backward displacement is limited to 128 bytes. Each time a branch instruction is assembled, the assembler must calculate the relative address. Examples of how this is done are shown in Example 4.2.
Example 4.2
Problem: Calculate the relative address (rr) for each of the following branch instructions.
Address Machine Code Source Code a. 0109 20 ?? BRA PAST 010B 8B 30 OVER ADDA #$30 010D 97 10 PAST STAA $10 b. 018E C6 0A BACK LDAB 0,X 0190 CB 30 ADDB #$30 0192 08 INX
0193 20 ?? BRA BACK c. 0173 20 ?? BRA MAIN
⋅ ⋅ ⋅
01E2 4A MAIN DECA d. E204 86 45 OUTA LDAA #$45
⋅ ⋅ ⋅
E27A 20 ?? BRA OUTA
Solution: In each case, the contents of the PC must be determined. Then the relative address can be calculated by subtracting the PC from the DA (Equation 4.2). The PC is the address of the next sequential instruction. Since each of these instructions occupies two bytes of memory, the address of the next instruction (PC) will be two greater than the address of the BRA instruction.
98
a. DA = 010D → $010D PC = $0109 + 2 → +$010B rr = DA – PC → $0002 rr = $02 b. DA = 018E → $018E PC = $0193 + 2 → +$0195 rr = DA – PC → $FFF9 rr = $F9 c. DA = 01E2 → $01E2 PC = $0173 + 2 → +$0175 rr = DA – PC → $006D rr = $6D d. DA = E204 → $E204 PC = $E25A + 2 → +$E25C rr = DA – PC → $FFA8 rr = $A8
Nature of the Relative Address
Further explanation is in order regarding the nature of the relative address. The relative address was defined earlier as the number of bytes to branch forward or backward in memory to the destination address. What does it mean to branch forward or backward? Figure 4.5 provides some insight into this process.
NOTE: When branching forward, the relative address is positive. It is the number of bytes of memory that will be skipped forward, starting with the first byte following the relative address in memory and going up to, but not including, the byte stored in the destination address.
In Example 4.2a, the relative address is calculated to be $02. This means that the program will skip forward past two bytes of memory. The two bytes of the ADDA #$30 instruction, $8B and $30, are skipped, as shown in Figure 4.5a. The instruction located two bytes forward in memory is executed.
rr - positive will skip next two bytes PC contains this address when rr is fetched from memory Destination address 1 2 0109 010B 010D 20 02 8B 30 97 10 BRA PAST OVER ADDA #$30 PAST STAA $10 a) Branching forward +2 rr - negative will
skip back seven bytes PC contains this address when rr is fetched from memory Destination address 7 6 5 4 3 2 1 018E 0190 0192 0193 0195 C6 0A CB 30 08 20 F9 BACK LDAB 0,X ADDB #$30 INX BRA BACK b) Branching backward
Figure 4.5 The Nature of the Relative Address 99
NOTE: When branching backward, the relative address is negative. It is the number of bytes of memory that will be skipped backward, starting with the relative address and going back to, and including, the byte stored in the destination address.
In Example 4.2b, the relative address was calculated to be $F9 (–710). This means that
the program will skip backward seven bytes of memory. The two bytes of the BRA instruction, the one byte of the INX instruction, the two bytes of the ADDA instruction and the two bytes of LDAB instruction are skipped, as shown in Figure 4.5b. The instruction located seven bytes backward in memory is executed.