Lecture 4
Instructions: Language
of the Computer
Compiling If Statements
n High-level language code:
if (i==j) f = g+h;
else f = g-h;
n f, g, … in $s0, $s1, …
n Compiled MIPS code:
bne $s3, $s4, Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit: …
Assembler calculates addresses
Lecture 4 — Instructions: Language of the Computer — 2
Compiling Loop Statements
n High-level language code:
while (save[i] == k) i += 1;
n i in $s3, k in $s5, address of save in $s6
n Compiled MIPS code:
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit: …
Lecture 4 — Instructions: Language of the Computer — 3
Basic Blocks
n A basic block is a sequence of instructions
with
n No embedded branches (except at end)
n No branch targets (except at beginning)
n A compiler identifies basic
blocks for optimization
n An advanced processor
can accelerate execution
of basic blocks
Lecture 4 — Instructions: Language of the Computer — 4
More Conditional Operations
n Set result to 1 if a condition is true
n Otherwise, set to 0
n slt rd, rs, rt
n if (rs < rt) rd = 1; else rd = 0;
n slti rt, rs, constant
n if (rs < constant) rt = 1; else rt = 0;
n Use in combination with beq, bne
slt $t0, $s1, $s2 # if ($s1 < $s2)
bne $t0, $zero, L # branch to L
Lecture 4 — Instructions: Language of the Computer — 5
Branch Instruction Design
n Why not blt, bge, etc?
n Hardware for <, ≥, … slower than =, ≠
n Combining with branch involves more work
per instruction, requiring a slower clock
n All instructions penalized!
n beq and bne are the common case
n This is a good design compromise
Lecture 4 — Instructions: Language of the Computer — 6
Signed vs. Unsigned
n Signed comparison: slt, slti
n Unsigned comparison: sltu, sltui
n Example
n $s0 = 1111 1111 1111 1111 1111 1111 1111 1111
n $s1 = 0000 0000 0000 0000 0000 0000 0000 0001
n slt $t0, $s0, $s1 # signed
n –1 < +1 Þ $t0 = 1
n sltu $t0, $s0, $s1 # unsigned
n +4,294,967,295 > +1 Þ $t0 = 0
Lecture 4 — Instructions: Language of the Computer — 7
Procedure Calling
n Steps required
1. Place parameters in registers
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call
Lecture 4 — Instructions: Language of the Computer — 8
Register Usage
n $a0 – $a3: arguments (reg’s 4 – 7)
n $v0, $v1: result values (reg’s 2 and 3)
n $t0 – $t9: temporaries
n Can be overwritten by callee
n $s0 – $s7: saved
n Must be saved/restored by callee
n $gp: global pointer for static data (reg 28)
n $sp: stack pointer (reg 29)
n $fp: frame pointer (reg 30)
n $ra: return address (reg 31)
Lecture 4 — Instructions: Language of the Computer — 9
Procedure Call Instructions
n Procedure call: jump and link
jal ProcedureLabel
n Address of following instruction put in $ra
n Jumps to target address
n Procedure return: jump register
jr $ra
n Copies $ra to program counter
n Can also be used for computed jumps
n e.g., for case/switch statements
Lecture 4 — Instructions: Language of the Computer — 10
Leaf Procedure Example
n High-level language code:
int leaf_example (int g, h, i, j)
{ int f;
f = (g + h) - (i + j);
return f;
}
n Arguments g, …, j in $a0, …, $a3
n f in $s0 (hence, need to save $s0 on stack)
n Result in $v0
Lecture 4 — Instructions: Language of the Computer — 11
Leaf Procedure Example
n MIPS code:
leaf_example:
addi $sp, $sp, -4
Save $s0 on stack
sw $s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a3 Procedure body
sub $s0, $t0, $t1
add $v0, $s0, $zero Result
lw $s0, 0($sp)
Restore $s0
addi $sp, $sp, 4
jr $ra Return
Lecture 4 — Instructions: Language of the Computer — 12
Non-Leaf Procedures
n Procedures that call other procedures
n For nested call, caller needs to save on the
stack:
n Its return address
n Any arguments and temporaries needed after
the call
n Restore from the stack after the call
Lecture 4 — Instructions: Language of the Computer — 13
Non-Leaf Procedure Example
n High-level language code:
int fact (int n)
{
if (n < 1) return 1;
else return n * fact(n - 1);
}
n Argument n in $a0
n Result in $v0
Lecture 4 — Instructions: Language of the Computer — 14
Non-Leaf Procedure Example
n MIPS code:
fact:
addi $sp, $sp, -8 # adjust stack for 2 items
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # save argument
slti $t0, $a0, 1 # test for n < 1
beq $t0, $zero, L1
addi $v0, $zero, 1 # if so, result is 1
addi $sp, $sp, 8 # pop 2 items from stack
jr $ra # and return
L1: addi $a0, $a0, -1 # else decrement n
jal fact # recursive call
lw $a0, 0($sp) # restore original n
lw $ra, 4($sp) # and return address
addi $sp, $sp, 8 # pop 2 items from stack
mul $v0, $a0, $v0 # multiply to get result
jr $ra # and return
Lecture 4 — Instructions: Language of the Computer — 15
Local Data on the Stack
n Local data allocated by callee
n e.g., C automatic variables
n Procedure frame (activation record)
n Used by some compilers to manage stack storage
Lecture 4 — Instructions: Language of the Computer — 16
Memory Layout
n Text: program code
n Static data: global
variables
n e.g., static variables in C,
constant arrays and strings
n $gp initialized to address
allowing ±offsets into this
segment
n Dynamic data: heap
n E.g., malloc in C, new in
Java
n Stack: automatic storage
Lecture 4 — Instructions: Language of the Computer — 17
Character Data
n Byte-encoded character sets
n ASCII: 128 characters
n 95 graphic, 33 control
n Latin-1: 256 characters
n ASCII, +96 more graphic characters
n Unicode: 32-bit character set
n Used in Java, C++ wide characters, …
n Most of the world’s alphabets, plus symbols
n UTF-8, UTF-16: variable-length encodings
Lecture 4 — Instructions: Language of the Computer — 18
Byte/Halfword Operations
n Could use bitwise operations
n MIPS byte/halfword load/store
n String processing is a common case
lb rt, offset(rs) lh rt, offset(rs)
n Sign extend to 32 bits in rt
lbu rt, offset(rs) lhu rt, offset(rs)
n Zero extend to 32 bits in rt
sb rt, offset(rs) sh rt, offset(rs)
n Store just rightmost byte/halfword
Lecture 4 — Instructions: Language of the Computer — 19
32-bit Constants
n Most constants are small
n 16-bit immediate is sufficient
n For the occasional 32-bit constant
lui rt, constant
n Copies 16-bit constant to left 16 bits of rt
n Clears right 16 bits of rt to 0
lui $s0, 61 0000 0000 0011 1101 0000 0000 0000 0000
ori $s0, $s0, 2304 0000 0000 0011 1101 0000 1001 0000 0000
Lecture 4— Instructions: Language of the Computer — 20
Branch Addressing
n Branch instructions specify
n Opcode, two registers, target address
n Most branch targets are near branch
n Forward or backward
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
n PC-relative addressing
n Target address = PC + offset × 4
n PC already incremented by 4 by this time
Chapter 2 — Instructions: Language of the Computer — 21
Jump Addressing
n Jump (j and jal) targets could be
anywhere in text segment
n Encode full address in instruction
op address
6 bits 26 bits
n (Pseudo)Direct jump addressing
n Target address = PC31…28 : (address × 4)
Lecture 4 — Instructions: Language of the Computer — 22
Target Addressing Example
n Loop code from earlier example
n Assume Loop at location 80000
Loop: sll $t1, $s3, 2 80000 0 0 19 9 4 0
add $t1, $t1, $s6 80004 0 9 22 9 0 32
lw $t0, 0($t1) 80008 35 9 8 0
bne $t0, $s5, Exit 80012 5 8 21 2
addi $s3, $s3, 1 80016 8 19 19 1
j Loop 80020 2 20000
Exit: … 80024
Lecture 4 — Instructions: Language of the Computer — 23
Branching Far Away
n If branch target is too far to encode with
16-bit offset, assembler rewrites the code
n Example
beq $s0,$s1, L1
↓
bne $s0,$s1, L2
j L1
L2: …
Lecture 4 — Instructions: Language of the Computer — 24
Addressing Mode Summary
Lecture 4 — Instructions: Language of the Computer — 25
Translation and Startup
Many compilers produce
object modules directly
Static linking
Lecture 4 — Instructions: Language of the Computer — 26
Assembler Pseudoinstructions
n Most assembler instructions represent
machine instructions one-to-one
n Pseudoinstructions: figments of the
assembler’s imagination
move $t0, $t1 → add $t0, $zero, $t1
blt $t0, $t1, L → slt $at, $t0, $t1
bne $at, $zero, L
n $at (register 1): assembler temporary
Lecture 4 — Instructions: Language of the Computer — 27