| Branches and Jumps | ||||||||||||
| Up to now: sequential instructions (strictly in order) | ||||||||||||
| What other kinds of instructions do we need (think of kinds of C statements)? | ||||||||||||
| selection (if/else, switch) | ||||||||||||
| looping (for, while) | ||||||||||||
| function calls | ||||||||||||
| There is no such thing as selection or loop statement in machine language | ||||||||||||
| only the evil G-word (goto) | ||||||||||||
| In assembly language, change the order of execution with a branch or jump | ||||||||||||
| branch: conditional (not always) | ||||||||||||
| jump: unconditional (always) | ||||||||||||
| What does it mean to branch? | ||||||||||||
| Normally, PC <--- PC + 4 changes PC to point to next sequential instruction | ||||||||||||
| Why add 4? | ||||||||||||
| Because each instruction is 32 bits (4 bytes) | ||||||||||||
| Jump: PC <--- target_location | ||||||||||||
| Conditional branch ("branch"): | ||||||||||||
| if (condition) | ||||||||||||
| PC <--- target_location | ||||||||||||
| else | ||||||||||||
| PC <--- PC + 4 | ||||||||||||
| Unconditional branch ("jump"): | ||||||||||||
| goto target_location | ||||||||||||