Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jump and Branch Instructions

Similar presentations


Presentation on theme: "Jump and Branch Instructions"— Presentation transcript:

1 Jump and Branch Instructions
Jump instruction. Symbolic address. Conditional Branches. Textbook P&H: Chapter 3.5.Instructions for making decisions. Central Connecticut State University, MIPS Tutorial. Chapters 17,18.

2 Repeat and Alter actions
The power of computers is: their ability to repeat actions and their ability to alter their operation depending on data. Modern programming languages express these abilities using control structures. Repeated action (iteration) is done with a while structure. Altered operation (alteration) is done with an if-then-else structure.

3 Assembly builds the structures
The machine instructions of the processor mainly do not have Repeat and Alter structures (Intel has loops). Consequentially Assembly language does not have these structures. When you program in assembly language you must build these structures out of basic assembly instructions. These basic instructions are the jump instructions and the conditional branch instructions.

4 What does Jump The effect of a jump instruction is to put a new address into the PC. Now the next machine cycle fetches the instruction at the new address. Instead of executing the instruction that follows the jump instruction in memory, the processor "jumps" to an instruction somewhere else in memory.

5 Jump syntax $8=$8+1 j target # after a delay of one Branch delay slot
# machine cycle, # processor jumps to target main: sll $0,$0,0 sll $0,$0,0 j main addiu $8,$8,1 Branch delay slot $8=$8+1 This is endless loop. Is there anything changed in this loop ?

6 Jump Machine Instruction
[0x ] 0x nop ; 8: sll $0,$0,0 [0x ] 0x nop ; 9: sll $0,$0,0 [0x ] 0x nop ;10: sll $0,$0,0 [0x c] 0x nop ;11: sll $0,$0,0 [0x ] 0x j 0x [main] ;12: j main [0x ] 0x addiu $8, $8, ;13: addiu $8,$8,1 The intent of the jump instruction is to put the address 0x into the PC. However, PC changes only after the instruction in the branch delay slot has executed. How does a 32-bit instruction specify a 32-bit address? What does mean this machine code 0x ? How the assembler forms it ? How the computer translates that instruction back into the address ?

7 Addressing Modes 5. Jump uses the Pseudodirect addressing mode, where the jump address is the 26 bits of the instruction concatenated with the upper bits of the PC Addressing mode – One of several addressing regimes delimited by their varied use of operands and or addresses.

8 Jump instruction format
Some of the instruction's bits must be used for the op-code. There is room in the instruction for a 26-bit address. The 32-bit target address is transformed by compiler into a 26-bit address and is written into the instruction. During execution the 26-bit target address field is transformed back into a 32-bit jumping address by computer. How this is done ? opcode target

9 26 bits keep instruction number (not byte’s address)
[0x ] 0x nop [0x ] 0x nop [0x ] 0x nop [0x c] 0x nop [0x ] 0x j 0x [main] [0x ] 0x addiu $8, $8, 1 Instructions always start on an address that is a multiple of four (they are word-aligned). So the low order two bits of a 32-bit instruction address are always "00". 0-0000 4-0100 8-1000 C-1100 No need to keep the last 2 zeros in 26 bits of instruction. They could be restored later during jump time ,28

10 We have 28 bits. What with the 4 remaining bits to have a full absolute address ?
We cannot do “jump to absolute address”. Because we cannot keep more than 28 bits of address in the jump instruction But if we have some base address then we can construct the full absolute address and jump by that address. That base is the PC’s high order 4 bits. ,28 PC

11 How is formed the absolute address
Shifting the 26-bit target left two places results in a 28-bit word-aligned address. Now the high-order four bits in the PC are concatenated to the high-order end of the 28-bit address to form a 32-bit address.

12 Relative addressing causes the jumps to be local
A jump instruction can't jump to any arbitrary location in the full 32-bit address space It must jump to an address within the following range: wxyz . wxyz The program is compact and mainly the most jumps and branches are to nearby addresses. If no other memory allocation technique (paging, relocation, segmentations) is being used then OS should place the program in the WXYZ segment in the way that the program’s start and the end remain in that segment

13 Jump Example 1. Full 32-bit jump address: 0x00400000
Machine Instruction Assembly Instruction sll $0,$0,0 C j firstInstruction 1. Full 32-bit jump address: 0x 2. The 26-bit field of the jump instruction: 3. Shift it left two positions: 4. What are the high-order four bits of the PC? 0000 5. Copy (4) to the left of (3): 6. Is (5) the same as (1)? Yes

14 Jump to absolute address
Unconditionally jump to the instruction whose address is in register rs. .text .globl __start __start: sll $0,$0,0 here: sll $0,$0,0 lui $5,0x0040 ori $5,0x0004 jr $5 # Jump to 0x addiu $8,$8,1

15 Unpredictable result Mips states that placing branch instruction in to a branch delay slot leads to undefined results. __start: sll $0,$0,0 lbl1: sll $0,$0,0 lbl2: sll $0,$0,0 j __start # OK j lbl1 # Incorrect j lbl # Incorrect addiu $8,$8,1


Download ppt "Jump and Branch Instructions"

Similar presentations


Ads by Google