Recap of Lecture 8 and Homework Check
- Byte addressing; aligned when A mod s = 0.
- Little-endian: smallest byte at the lowest address.
- Effective address = the address the instruction really uses.
- Displacement + immediate + register indirect = 75-99% of uses.
Homework check
- 1. 0xCAFEBABE at 0x100: both byte orders? Half word at 0x102 (little)?
- 2. R1 = 0x400, R2 = 0x10, Mem[0x400] = 0x800: EA of 0x20(R1), (R1+R2), @(R1), -(R1)?
- 3. Add R1,(R2)+ in MIPS?
Today's Plan (50 minutes)
0-5
Recap and homework check Lecture 8
5-8
Plan and learning outcomes
8-18
Module 1: Kinds of operations categories, what programs really use
18-32
Module 2: Control flow branches, jumps, calls and returns
32-38
Module 3: Type and size of operands bytes to doubles, how the type is given
38-45
Class activity classify, branch targets, jump table
45-50
Mistakes, quiz and homework
Learning Outcomes (SLOs)
By the end of this lecture you will be able to:
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
CLO3: Explain instruction set principles and advanced instruction-level and thread-level parallelism techniques.
Module 1: Kinds of Operations
What an instruction set can do
Categories of Instruction Operators
ALU: cashier
Transfer: loader
Control: warden
System: manager
Simple Operations Dominate
96%
top 10 x86
SPECint92 (H&P Fig. A.13)
Common case
Load, branch, compare, store, add - make these fast.
Our Own Mix: the Practical 9 Loop
P09_A1_trace.asm (n = 5)
- loop:
- bgt $t0, $t2, done # slt + bne
- add $t1, $t1, $t0
- addi $t0, $t0, 1
- j loop
Lesson: per pass = 2 (bgt) + add + addi + j = 5 instructions; ALU and control are almost all of it.
Data transfer appears as soon as the loop walks an array (Practical 10: lw every pass).
Module 2: Control Flow
Branches, jumps, calls and returns
Four Kinds of Control Flow
Most common: conditional branches - so they must be fast (a big topic in Unit 3).
Where Does the Branch Go?
- MIPS branch target = (PC + 4) + offset x 4
- beq at 0x00400008, offset field 2:
- 0x0040000C + 8 = 0x00400014
Why PC-relative?
Targets are close; code works wherever it is loaded.
Offsets count words from PC + 4 - not bytes from the branch itself.
Register Indirect Jump: a Switch
L09_switch.asm (tested in MARS 4.5)
- jt: .word case0, case1, case2
- li $t0, 2 # choice
- la $t1, jt
- sll $t2, $t0, 2 # choice x 4
- add $t1, $t1, $t2
- lw $t3, 0($t1) # address of case
- jr $t3 # indirect jump
OUTPUT
Like: the hospital reception desk - one lookup, then straight to the right room.
Three Ways to Test a Condition
MIPS: beq/bne compare and branch; less-than is slt (condition register) + bne. That is why blt is a pseudo-instruction.
Procedure Call and Return
L09_call.asm (tested in MARS 4.5)
- main:
- li $a0, 7
- jal square # $ra = 0x00400008
- move $s0, $v0 # return lands here
- li $v0, 10
- syscall
- square:
- mul $v0, $a0, $a0
- jr $ra # go back
OUTPUT
- $ra = 0x00400008
- $s0 = 49
Rule: jal saves PC + 4; a procedure that calls another must save $ra first.
Module 3: Type and Size of Operands
What the data looks like
Operand Types and Sizes
Money
Rs 265.43 = 26543 paisa (integer) - exact. As float it is only close.
SPEC: words and double words make up most data accesses (H&P A.5).
How Does the Hardware Know the Type?
Phone or money?
Same digits, different meaning. The form field (opcode) decides.
Rule: the opcode gives the type; the data itself carries no type.
Practical 6: lb gave -1 and lbu gave 255 for the same byte 0xFF.
What a Modern ISA Should Offer
Next lecture: how all of this is packed into 32 bits - the R, I and J formats.
Class Activity: Operations and Control Flow
7 min · pairs
In pairs. Use the Lecture 9 slides; no MARS needed.
Name the category of: sw, slt, j, syscall, mul, lbu.
A beq is at 0x00400040. Find the target if the offset field is 6, and if it is -4.
In L09_switch.asm, set choice = 1. Which address does jr go to, and what is printed?
Which test style is: x86 cmp + jl; MIPS slt + bne; MIPS beq?
Common Mistakes
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: slt (condition register) + bne.
✗ Money can safely be stored as float.
✓ Use integers (paisa) or decimal; 0.10 is not exact in binary.
Recap Quiz
Answer on paper in 3 minutes, then we check together.
Q1
Name the two most frequent 80x86 instructions in SPECint92 and their shares.
Q2
Which kind of control flow is most common, and roughly how common?
Q3
A jal is at 0x00400004. What value does $ra get?
Q4
Why are procedure returns done with a register-indirect jump?
Q5
How does MIPS know whether to add two integers or two single floats?
Homework and Next Lecture
- Read Hennessy and Patterson, Appendix A, sections A.4-A.6.
- Classify: lw, add, beq, jal, syscall, sll, sw, jr, add.s, lbu.
- Branch targets: beq at 0x00400020 with offset field -3; bne at 0x00400100 with offset field 5.
- Calls: a jal is at 0x00400010. What is in $ra? What goes wrong if the called procedure also uses jal?
NEXT LECTURE
Lecture 10: Instruction Formats, Semantics and Encoding
Fixed, variable and hybrid encodings; MIPS R, I and J formats; encoding and decoding by hand.