Learning Outcomes
SLO | After this lecture you can… | CLO | Bloom |
|---|---|---|---|
SLO 9.1 | Classify instructions into operator categories and explain why a few simple operations dominate real programs. | CLO3 | C2 Understand |
SLO 9.2 | Explain conditional branches, jumps, calls and returns, and compute PC-relative branch targets. | CLO3 | C3 Apply |
SLO 9.3 | Describe the types and sizes of operands and how an ISA tells the hardware the type. | CLO3 | C2 Understand |
§1 Kinds of Operations
§1.1 Categories of instruction operators
Slide L09-S06
Hennessy and Patterson group the operators of an instruction set into eight categories. Every computer has the first four; floating point is in almost every general-purpose processor; decimal, string and graphics operations are optional.
Category | What it does | MIPS examples | Real-world |
|---|---|---|---|
Arithmetic and logical | Integer add, subtract, and, or, compare, shift | add, addi, sub, and, ori, slt, sll | Cashier adding the bill at a utility store |
Data transfer | Move data between registers and memory | lw, sw, lb, lbu, sb, lui | Loader bringing goods from the godown (warehouse) |
Control | Change the order: branch, jump, call, return | beq, bne, j, jal, jr | Traffic warden sending cars another way |
System | Operating-system calls, memory management | syscall, eret | Bank manager - the only one who opens the vault |
Floating point | Add, multiply, divide real numbers | add.s, mul.d, lwc1 | Engineering calculations |
Decimal | Packed decimal (BCD) arithmetic | none | Exact money amounts in banking |
String | Move, compare, search strings | none (loops of lb/sb) | Copying a CNIC number character by character |
Graphics | Pixel and vertex operations | none (SIMD/GPU, Unit 5) | Photo filters on a phone |
§1.2 Simple operations dominate
Slide L09-S07
Hennessy and Patterson (Figure A.13) list the ten most frequent 80x86 instructions in SPECint92 programs. Even on x86, which has hundreds of instructions, these ten simple ones make up about 96% of all instructions executed:
Rank | Instruction | Share | Rank | Instruction | Share |
|---|---|---|---|---|---|
1 | load | 22% | 6 | and | 6% |
2 | conditional branch | 20% | 7 | sub | 5% |
3 | compare | 16% | 8 | move register-register | 4% |
4 | store | 12% | 9 | call | 1% |
5 | add | 8% | 10 | return | 1% |
(The rounded values add to 95%; Hennessy and Patterson report the total as 96%.) Lesson: 'focus on the common case' (Lecture 6) - make loads, stores, branches, compares and adds fast. This is also why RISC designs keep only simple operations.
§1.3 Our own mix: the Practical 9 loop
Slide L09-S08
Practical 9 Part A sums 1..5 with a while loop. MARS counts 35 executed instructions. Counting from the Basic column: each pass runs slt + bne (the pseudo bgt), add, addi and j = 5 instructions.
loop: |
Category | Where they come from | Executed |
|---|---|---|
ALU | 3 li before the loop; slt, add, addi in 5 passes (15); final slt (1); 3 ALU in printing/exit | 22 |
Branch | bne in 5 passes + the final bne that leaves | 6 |
Jump | j in 5 passes | 5 |
Other | 2 syscall | 2 |
Total | equals the MARS instruction count | 35 |
§2 Control Flow
§2.1 Four kinds of control-flow instruction
Slide L09-S10
Kind | Meaning | MIPS | Real-world | Share (int / FP) |
|---|---|---|---|---|
Conditional branch | Jump only if a condition is true | beq, bne | Motorway exit taken only if you need petrol | about 75% / 82% |
Jump | Always go to another place | j | Road sign: 'Lahore this way' | about 6% / 10% |
Procedure call | Jump and remember where to come back | jal | Going to the NADRA office | about 19% / 8% |
Procedure return | Go back to the remembered place | jr $ra | Coming back home | (with call) |
Shares are Hennessy and Patterson's measurements of control-flow instructions on a load-store machine running SPEC. Conditional branches are by far the most frequent, so they must be fast - a major topic of Unit 3 (control hazards and branch prediction).
§2.2 Naming the target
Slide L09-S11
PC-relative: target = PC + displacement. Targets of branches are usually close (inside the same loop or if), so a short field is enough, and the code still works wherever it is loaded (position independence). Hennessy and Patterson recommend a PC-relative field of at least 8 bits. Register indirect: the target is in a register, used when the target is not known at compile time - procedure returns, case/switch statements, virtual functions and function pointers, and dynamically linked libraries (DLLs).
Way to name the target | Used for | MIPS |
|---|---|---|
PC-relative | if statements and loops | beq, bne: 16-bit offset in words from PC + 4 |
Pseudo-direct | jumps anywhere in the same 256 MB region | j, jal: 26-bit field x 4 |
Register indirect | returns, switch tables, virtual functions, DLLs | jr |
MIPS branch target: target = (PC + 4) + offset x 4. Example: beq at 0x00400008 with offset field 2 → 0x0040000C + 8 = 0x00400014. |
§2.3 A switch with a jump table
Slide L09-S12
A switch can be compiled into a table of addresses. The program loads the address for the choice and jumps to it with jr. Tested in MARS: choice 2 prints 300; choice 0 prints 100 and choice 1 prints 200. Real-world: a hospital reception desk looks up your problem in a list and sends you straight to the right room - one lookup instead of a long chain of if-else tests.
L09_switch.asm (tested in MARS 4.5) |
Label | Address (MARS Basic column) |
|---|---|
jr $t3 | 0x00400018 |
case0 | 0x0040001c |
case1 | 0x00400024 |
case2 | 0x0040002c |
§2.4 Three ways to test a condition
Slide L09-S13
Style | How it works | Examples | Advantage | Disadvantage |
|---|---|---|---|---|
Condition code | ALU operations set flag bits; the branch tests the flags | 80x86, ARM, PowerPC, SPARC | The compare may be free | Almost every instruction changes the flags, which ties instruction order together (hard for pipelines) |
Condition register | A compare writes 0 or 1 into an ordinary register; the branch tests it | MIPS slt + bne, Alpha | Simple | Uses a register |
Compare and branch | The compare is part of the branch | MIPS beq/bne, PA-RISC, VAX | One instruction | More work in one instruction |
MIPS mixes the last two: beq and bne compare two registers directly, but 'less than' needs slt first. That is why blt, bge, bgt and ble are pseudo-instructions (Practical 7).
§2.5 Procedure call and return
Slide L09-S14
A call must save the return address. MIPS jal puts PC + 4 into $ra and jumps; jr $ra returns. Tested in MARS: jal is at 0x00400004, so $ra = 0x00400008; square returns 7 x 7 = 49.
L09_call.asm (tested in MARS 4.5) |
Registers also need saving. Caller saving: the calling procedure saves the registers it still needs. Callee saving: the called procedure saves the registers it will change. MIPS uses both ($t registers caller-saved, $s registers callee-saved). If a procedure calls another procedure, the second jal overwrites $ra, so the first $ra must be saved on the stack first (Practicals 13-14).
§3 Type and Size of Operands
§3.1 Common types
Slide L09-S16
Type | Bits | Typical use | MIPS |
|---|---|---|---|
Character | 8 | ASCII text | lb, lbu, sb |
Half word | 16 | Unicode text, short integers | lh, lhu, sh |
Word | 32 | int, 32-bit addresses | lw, sw |
Double word | 64 | long integers, 64-bit addresses | ld, sd (MIPS64) |
Single precision | 32 | float (IEEE 754) | lwc1, add.s |
Double precision | 64 | double (IEEE 754) | ldc1, add.d |
Packed decimal (BCD) | 4 per digit | exact money amounts in business programs | not in MIPS |
Measurements on SPEC programs show that words and double words make up most data accesses. Money should not be stored as binary floating point: 0.10 has no exact binary form. Store Rs 265.43 as 26543 paisa (an integer) or use decimal arithmetic.
§3.2 How the hardware knows the type
Slide L09-S17
Register bits carry no label. The same 32 bits 0x40490FDB are 1078530011 as an integer and about 3.14159 as a single-precision float. The type is given by the opcode: add (integer, traps on overflow), addu (no trap), add.s (single float), add.d (double float); lb (signed byte) versus lbu (unsigned byte) - in Practical 6 the byte 0xFF gave -1 with lb and 255 with lbu. Some old machines used tagged data (type bits stored with each value), but the idea did not survive.
§3.3 Recommendations and MIPS
Slide L09-S18
Hennessy and Patterson recommend | MIPS provides |
|---|---|
Simple, frequent operations, made fast | add, sub, and, or, slt, sll, lw, sw |
Compare equal, not equal, less than (many compares are with zero) | beq, bne; slt + bne; $zero register |
PC-relative branches with at least 8 bits of displacement | 16-bit word offset from PC + 4 |
Register-indirect jumps | jr |
Calls that save the return address | jal: $ra = PC + 4 |
8-, 16-, 32-, 64-bit integers; 32- and 64-bit IEEE floating point | lb/lh/lw (ld in MIPS64); .s and .d instructions |
§4 Common Mistakes
Slide L09-S20
Wrong idea | Correct idea |
|---|---|
A branch offset counts bytes from the branch. | MIPS counts words from PC + 4: target = (PC + 4) + offset x 4. |
jal saves the address of the jal itself. | It saves PC + 4, the next instruction (MARS: $ra = 0x00400008). |
blt is a real MIPS instruction. | It is a pseudo-instruction: slt (condition register) + bne. |
Money can safely be stored as float. | Use integers (paisa) or decimal; 0.10 is not exact in binary. |
Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Operator category | A group of similar operations | Departments in a shop |
Control flow | The order in which instructions run | The route a bus follows |
Conditional branch | Jump only if a test is true | Take the exit only if you need petrol |
Jump | Always go to another place | Road sign 'Lahore this way' |
Return address | Where to continue after a procedure | Note saying 'back at the shop' |
PC-relative | Target counted from the current instruction | 'Two shops ahead of you' |
Register indirect jump | Jump to the address held in a register | Go to the address on this slip |
Jump table | List of addresses, one per case | Hospital reception list of rooms |
Condition code | Flag bits set by the last ALU operation | Scoreboard lights after each ball |
Packed decimal (BCD) | Two decimal digits stored in one byte | Exact rupee and paisa amounts |
Self-Check Questions
- Name the four categories every ISA has, with one MIPS example each. (§1.1)
- Which two instructions are most frequent on x86 in SPECint92? (§1.2)
- How many instructions does one pass of the Practical 9 Part A loop execute? Why? (§1.3)
- Which kind of control flow is the most common? (§2.1)
- A beq is at 0x00400040 with offset field -4. Where does it go? (§2.2)
- Give three uses of register-indirect jumps. (§2.2, §2.3)
- Compare condition codes, condition registers and compare-and-branch. (§2.4)
- What does jal put in $ra, and why must a non-leaf procedure save it? (§2.5)
- Why should money not be stored as float? (§3.1)
- How does MIPS know whether add works on integers or floats? (§3.2)
Answers: beq at 0x00400040, offset -4 → 0x00400044 - 16 = 0x00400034; one pass = 5 instructions (bgt is 2).
Further Reading
- Hennessy and Patterson (2012): Appendix A, sections A.4 (operations), A.5 (type and size of operands), A.6 (control flow).
- Patterson and Hennessy, Computer Organization and Design: Chapter 2, sections 2.7-2.8 (decisions, procedures).