Download Texas A&M University Computer Science Department CPSC …

Document related concepts
no text concepts found
Transcript
Chapter 2. Instruction:
Language of the Computer
1
Five Basic Components
Processor
Input
Control
Memory
Datapath
Output
Since 1946 all computers have had 5 components!!!
2
A Translation Hierarchy
C pro gram
•
C om piler
Assem bly langua ge p rogram
High Level Language (HLL)
programs first compiled
(possibly into assembly),
then linked and finally
loaded into main memory.
Assem bler
O bject: M achine lang u ag e m odule
O bject: L ibrary ro utine (m ach ine langua ge)
L in ker
Executab le: M achin e language program
Loader
M em ory
3
Machine Language v.s. Assembly
Language
• In C, we can have
a = b+c+d;
• The MIPS assembly code will look like this:
add a, b, c
add a, a, d
where a, b, c and d are registers
• The MIPS machine language will look like:
000000 00010 00011 00001 00000 100000
000000 00001 00100 00001 00000 100000
• Comparison
– The high level language is most efficient and readable
– The assembly language is less efficient yet readable
– The machine language directly corresponds to the assembly
language, but not readable
4
High Level Language Operators/Operands
•
Consider C or C++
•
Operators: +, -, *, /, % (mod); (7/4 equals 1, 7%4 equals 3)
•
Operands:
– Variables: fahr, celsius
– Constants: 0, 1000, -17, 15.4
•
In most High Level Languages, variables declared and given a
type
– int fahr, celsius;
– int a, b, c, d, e;
5
Assembly Operators/Instructions
° MIPS Assembly Syntax is rigid:
for arithmetic operations, 3 variables
Why? Directly corresponds to the machine language and makes
hardware simple via regularity
° Consider the following C statement:
a = b + c + d - e;
° It will be executed as multiple instructions
add a, b, c # a = sum of b & c
add a, a, d # a = sum of b,c,d
sub a, a, e # a = b+c+d-e
° The right of # is a comment terminated by end of the line.
Applies only to current line.
6
Compilation
° How to turn the notation that programmers prefer into notation
computer understands?
° Program to translate C statements into Assembly Language
instructions; called a compiler
° Example: compile by hand this C code:
a = b + c;
d = a - e;
° Easy:
add a, b, c
sub d, a, e
° Compile an entire program of 10,000 lines of C code by hand?
° Big Idea: compiler translates notation from one level of
computing abstraction to lower level
7
Compilation 2
° Example: compile by hand this C code:
f = (g + h) - (i + j);
° First sum of g and h. Where to put result?
add f, g, h
# f contains g+h
° Now sum of i and j. Where to put result?
Compiler creates temporary variable to hold sum: t1
add t1, i, j
# t1 contains i+j
° Finally produce difference
sub f, f, t1
# f = (g+h)-(i+j)
8
Compilation -- Summary
° C statement (5 operands, 3 operators):
f = (g + h) - (i + j);
° Becomes 3 assembly instructions
(6 unique operands, 3 operators):
add f,g,h
# f contains g+h
add t1,i,j
# t1 contains i+j
sub f,f,t1
# f=(g+h)-(i+j)
° In general, each line of C produces many assembly instructions
Why people program in C vs. Assembly?
9
Assembly Design: Key Concepts
• Assembly language is essentially directly supported in
hardware, therefore ...
• It is kept very simple!
– Limit on the type of operands
– Limit on the set operations that can be done to absolute
minimum.
• if an operation can be decomposed into a simpler
operation, don’t include it.
10
MIPS R3000 Instruction Set Architecture (Summary)
° Instruction Categories
 Load/Store
 Computational
 Jump and Branch
 Floating Point (coprocessor)
Registers
R0 - R31
PC
HI
LO
3 Instruction Formats: all 32 bits wide
R:
I:
J:
OP
Rs
Rt
OP
Rs
Rt
OP
Rd
sa
funct
Immediate
jump target
11
Different Instruction Sets
Compute C = A + B under different instruction sets:
Register
(register-memory)
Register
(load-store)
Stack
Accumulator
Push A
Load A
Load R1,A
lw R1,A
Push B
Add B
Add R1,B
lw R2,B
Add
Store C
Store C, R1
add R3,R1,R2
Pop C
sw C,R3
MIPS
Assembly Variables: Registers (1/4)
• Unlike HLL, assembly cannot use variables
– Why not? Keep Hardware Simple
• Assembly Operands are registers
– limited number of special locations built directly into the hardware
– operations can only be performed on these!
• Benefit: Since registers are directly in hardware, they are very
fast
• Registers are not what we normally call “memory”
13
Assembly Variables: Registers (2/4)
• 32 registers in MIPS
– Why 32? Smaller is faster
– Each register can be referred to by number or name
– Number references: $0, $1, $2, … $30, $31
• Each MIPS register is 32 bits wide
– Groups of 32 bits called a word in MIPS
• Drawback: Since registers are in hardware, there are a
predetermined number of them
– Solution: MIPS code must be very carefully put together to
efficiently use registers
14
Assembly Variables: Registers (3/4)
• By convention, each register also has a name to make it easier
to code
• For now:
$16 - $23
$s0 - $s7 (correspond to C variables)
$8 - $15
$t0 - $t7 (correspond to temporary variables)
• In general, use register names to make your code more
readable
15
Comments in Assembly
• Another way to make your code more readable: comments!
• Hash (#) is used for MIPS comments
– anything from hash mark to end of line is a comment and will be
ignored
• Note: Different from C.
– C comments have format /* comment */ , so they can span many
lines
16
Addition and Subtraction (1/4)
• Syntax of Instructions:
add
a, b, c
where:
add: operation by name
a: operand getting result (destination)
b: 1st operand for operation (source1)
c: 2nd operand for operation (source2)
• Syntax is rigid:
– 1 operator, 3 operands
– Why? Keep Hardware simple via regularity
17
Addition and Subtraction (2/4)
• Addition in Assembly
– Example (in MIPS):
add $s0, $s1, $s2
– Equivalent to (in C): a = b + c
where registers $s0, $s1, $s2 are associated with variables a, b, c
• Subtraction in Assembly
– Example (in MIPS):
sub $s3, $s4, $s5
– Equivalent to (in C): d = e - f
where registers $s3, $s4, $s5 are associated with variables d, e, f
18
Addition and Subtraction (3/4)
• How to do the following C statement?
a = b + c + d - e;
• Break it into multiple instructions:
add
$s0, $s1, $s2
#a=b+c
add
$s0, $s0, $s3
#a=a+d
sub
$s0, $s0, $s4
#a=a-e
19
Immediates
• Immediates are numerical constants.
• They appear often in code, so there are special instructions for
them.
• ''Add Immediate'':
addi
$s0, $s1, 10
f = g + 10
(in MIPS)
(in C)
where registers $s0, $s1 are associated with variables f, g
• Syntax similar to add instruction, except that last argument is a
number instead of a register.
20
Register Zero
• One particular immediate, the number zero (0), appears very
often in code.
• So we define register zero ($0 or $zero) to always have the value
0.
• This is defined in hardware, so an instruction like
addi
$0, $0, 5
will not do anything.
• Use this register, it’s very handy!
21
Assembly Operands: Memory
• C variables map onto registers; what about large data
structures like arrays?
• 1 of 5 components of a computer: memory contains such data
structures
• But MIPS arithmetic instructions only operate on registers,
never directly on memory.
° Data transfer instructions transfer data between registers and
memory:
– Memory to register
– Register to memory
22
MIPS Addressing Formats (Summary)
• How memory can be addressed in MIPS
1 . Im m e d i a t e a d d r e s s i n g
op
rs
rt
Im m e d ia te
2 . R e g is te r a d d r e s s in g
op
rs
rt
rd
. . .
fu n c t
R e g is te r s
R e g is te r
3 . B a s e a d d r e s s in g
op
rs
rt
M emory
A d dress
+
R e g is te r
B y te
H a lfw o r d
W o rd
4 . P C - r e la ti v e a d d r e s s in g
op
rs
rt
M emory
A d dress
PC
+
W o rd
5 . P s e u d o d ir e c t a d d r e s s in g
op
A d d re s s
PC
M emory
W o rd
23
Data Transfer: Memory to Reg (1/4)
• To transfer a word of data, we need to specify two things:
– Register: specify this by number (0 - 31)
– Memory address: more difficult
-
Think of memory as a single one-dimensional array, so we
can address it simply by supplying a pointer to a memory
address.
-
Other times, we want to be able to offset from this pointer.
24
Data Transfer: Memory to Reg (2/4)
• To specify a memory address to copy from, specify two things:
– A register which contains a pointer to memory
– A numerical offset (in bytes)
• The desired memory address is the sum of these two values.
• Example:
8($t0)
– specifies the memory address pointed to by the value in $t0, plus
8 bytes
25
Data Transfer: Memory to Reg (3/4)
• Load Instruction Syntax:
lw
a, 100(b)
– where
lw operation (instruction) name
a register that will receive value
100 numerical offset in bytes
b register containing pointer to memory
• Instruction Name:
– lw (meaning Load Word, so 32 bits or one word are loaded at a
time)
26
Data Transfer: Memory to Reg (4/4)
• Example:
lw
$t0, 12($s0)
This instruction will take the pointer in $s0, add 12 bytes to it, and
then load the value from the memory pointed to by this calculated
sum into register $t0
• Notes:
– $s0 is called the base register
– 12 is called the offset
– offset is generally used in accessing elements of array or structure:
base register points to beginning of array or structure
27
Data Transfer: Reg to Memory
•
Also want to store value from a register into memory
•
Store instruction syntax is identical to Load instruction syntax
•
Instruction Name:
sw
(meaning Store Word, so 32 bits or one word are loaded at a
time)
• Example:
sw
$t0, 12($s0)
This instruction will take the pointer in $s0, add 12 bytes to it, and
then store the value from register $t0 into the memory address
pointed to by the calculated sum
28
Pointers vs. Values
° Key Concept: A register can hold any 32-bit value. That value can
be a (signed) int, an unsigned int, a pointer (memory address),
etc.
• If you write
lw
$t2, 0($t0)
then, $t0 better contain a pointer
• What is this for:
add $t2,$t1,$t0
29
Addressing: Byte vs. word
•
Every word in memory has an address, similar to an index in an array
•
Early computers numbered words like C numbers elements of an
array:
– Memory[0], Memory[1], Memory[2], …
Called the “address” of a word
 Computers needed to access 8-bit bytes as well as words
(4 bytes/word)
 Today machines address memory as bytes, hence word
addresses differ by 4
Memory[0], Memory[4], Memory[8],
30
Compilation with Memory
• What offset in lw to select A[8] in C?
•
4x8=32 to select A[8]: byte vs. word
• Compile by hand using registers:
g = h + A[8];
– g: $s1, h: $s2, $s3: base address of A
• 1st transfer from memory to register:
lw
$t0, 32($s3)
# $t0 gets A[8]
– Add 32 to $s3 to select A[8], put into $t0
• Next add it to h and place in g
add
$s1, $s2, $t0 # $s1 = h + A[8]
31
Notes about Memory
• Pitfall: Forgetting that sequential word addresses in machines
with byte addressing do not differ by 1.
– Many an assembly language programmer has toiled over errors
made by assuming that the address of the next word can be
found by incrementing the address in a register by 1 instead of
by the word size in bytes.
– So remember that for both lw and sw, the sum of the base
address and the offset must be a multiple of 4 (to be word
aligned)
32
More Notes about Memory: Alignment
• MIPS requires that all words start at addresses that are multiples
of 4 bytes
Bytes in Word
0
1
2
3
Aligned
Not
Aligned
Word Location
 Called Alignment: objects must fall on address that is multiple of
their size.
33
Role of Registers vs. Memory
• What if more variables than registers?
– Compiler tries to keep most frequently used variable in
registers
– Writing less frequently used to memory: spilling
• Why not keep all variables in memory?
– Smaller is faster:
registers are faster than memory
– Registers more versatile:
• MIPS arithmetic instructions can read 2, operate on them,
and write 1 per instruction
• MIPS data transfer only read or write 1 operand per
instruction, and no operation
34
So Far...
• All instructions have allowed us to manipulate data.
• So we’ve built a calculator.
• To build a computer, we need ability to make decisions
35
C Decisions: if Statements
• 2 kinds of if statements in C
– if (condition) clause
– if (condition) clause1 else clause2
• Rearrange 2nd if into following:
if
(condition) goto L1;
clause2;
go to L2;
L1: clause1;
L2:
– Not as elegant as if - else, but same meaning
36
MIPS Decision Instructions
• Decision instruction in MIPS:
– beq
a, b, L
– beq is ‘Branch if (registers are) equal’
Same meaning as (using C):
if
(a == b) goto L
• Complementary MIPS decision instruction
– bne
a, b, L
– bne is ‘Branch if (registers are) not equal’
Same meaning as (using C):
if
(a!=b) goto L1
• Called conditional branches
37
MIPS Goto Instruction
• In addition to conditional branches, MIPS has an unconditional
branch:
j label
• Called a Jump Instruction: jump (or branch) directly to the
given label without needing to satisfy any condition
• Same meaning as (using C):
goto label
• Technically, it’s the same as:
beq
$0, $0, label
since it always satisfies the condition. But j has a longer address
field (26 bits) while beg has a shorter address field (16 bits)
38
Compiling C if into MIPS (1/2)
• Compile by hand
if (i == j)
f = g+h;
else f = g-h;
• Use this mapping:
– f: $s0,
–
–
–
–
(true)
i == j
f=g+h
(false)
i == j?
i != j
f=g-h
Exit
g: $s1,
h: $s2,
i: $s3,
j: $s4
39
Compiling C if into MIPS (2/2)
(true)
i == j
f=g+h
° Final compiled MIPS code:
beq $s3, $s4, True
# branch i==j
sub $s0, $s1, $s2 # f=g-h(false)
j
True:
add
Fin
# go to Fin
$s0,$s1,$s2
# f=g+h (true)
(false)
i == j?
i != j
f=g-h
Exit
Fin:
° Note: Compilers automatically create labels to handle decisions (branches)
appropriately. Generally not found in HLL code.
40
Instruction Support for Functions
C
M
I
P
S
... sum(a,b);... /* a, b: $s0,$s1 */
}
int sum(int x, int y) {
return x+y;
}
address
1000
1004
1008
1012
1016
add
add
addi
j
...
$a0,$s0,$zero
$a1,$s1,$zero
$ra,$zero,1016
sum
# x = a
# y = b
#$ra=1016
#jump to sum
2000
sum: add $v0,$a0,$a1
2004
jr
$ra
# new instruction
41
Instruction Support for Functions
• Single instruction to jump and save return address: jump and link
(jal)
• Before:
1008 addi $ra,$zero,1016 #$ra=1016
1012 j
sum
#go to sum
• After:
1012 jal sum
# $ra=1016,go to sum
• Why have a jal? Make the common case fast: functions are very
common.
42
Instruction Support for Functions
• Syntax for jr (jump register):
jr
register
• Instead of providing a label to jump to, the jr instruction
provides a register which contains an address to jump to.
• Very useful for function calls:
– jal stores return address in register ($ra)
– jr jumps back to that address
43
Nested Procedures
int sumSquare(int x, int y) {
return mult(x,x)+ y;
}
• Routine called sumSquare; now sumSquare is calling mult.
• So there’s a value in $ra that sumSquare wants to jump back to,
but this will be overwritten by the call to mult.
• Need to save sumSquare return address before call to mult.
44
Nested Procedures
• In general, may need to save some other info in addition to
$ra.
• When a C program is run, there are 3 important memory
areas allocated:
– Static: Variables declared once per program, cease to exist
only after execution completes
– Heap: Variables declared dynamically
– Stack: Space to be used by procedure during execution; this
is where we can save register values
45
C memory Allocation
Address

$sp
stack
pointer
0
Stack
Space for saved
procedure information
Heap
Explicitly created space,
e.g., malloc(); C pointers
Static
Variables declared
once per program
Code
Program
46
Run-Time Memory Allocation in Executable Programs

$sp
7fff ffff
hex
Stack
$sp
stack
pointer
global
pointer $gp
$gp
Explicitly allocated
space,
(C malloc()library
proc)
Dynamic data
1000 8000
hex
1000 0000
hex
Static data
Text
pc
Local data in functions,
stack frames
0040 0000
hex
Variables allocated
once
per program
(global, C static)
Reserved
0
0
Program instructions
47
Using the Stack
• So we have a register $sp which always points to the last
used space in the stack.
• To use stack, we decrement this pointer by the amount of
space we need and then fill it with info.
• So, how do we compile this?
int sumSquare(int x, int y) {
return mult(x,x)+ y;
°
}
48
Using the Stack (2/2)
°Compile by hand
sumSquare:
addi
sw
sw
$sp, $sp, -8
$ra, 4($sp)
$a1, 0($sp)
#space on stack
#save ret addr
# save y
add
jal
$a1, $a0, $zero
mult
# mult(x,x)
# call mult
lw
$a1, 0($sp)
# restore y
add
$v0, $v0, $a1
# mult()+ y
lw
$ra, 4($sp)
# get ret addr
addi
$sp, $sp, 8
# restore stack
jr
$ra
49
Steps for Making a Procedure Call
1) Save necessary values onto stack.
2) Assign argument(s), if any.
3) jal call
4) Restore values from stack.
50
Rules for Procedures
• Called with a jal instruction, returns with a jr $ra
• Accepts up to 4 arguments in $a0, $a1, $a2 and $a3
• Return value is always in $v0 (and if necessary in $v1)
• Must follow register conventions (even in functions that only you
will call)! So what are they?
Return address
Arguments
Return value
$ra
$a0, $a1, $a2, $a3
$v0, $v1
Local variables
$s0, $s1, , $s7
51
Register Conventions (1/6)
• Caller: the calling function
• Callee: the function being called
• When callee returns from executing, the caller needs
to know which registers may have changed and
which are guaranteed to be unchanged.
° Register Conventions: A set of generally accepted
rules as to which registers will be unchanged after a
procedure call (jal) and which may be changed.
52
Register Conventions (2/6)
• $0: No Change. Always 0.
• $v0-$v1: Change. These are expected to contain new
values.
• $a0-$a3: Change. These are volatile argument
registers.
• $t0-$t9: Change. That’s why they’re called temporary:
any procedure may change them at any time.
53
Register Conventions (3/6)
•$s0-$s7: No Change. Very important, that’s why
they’re called saved registers. If the callee changes
these in any way, it must restore the original values
before returning.
•$sp: No Change. The stack pointer must point to
the same place before and after the jal call, or else
the caller won’t be able to restore values from the
stack.
•$ra: Change. The jal call itself will change this
register.
54
Register Conventions (4/6)
• What do these conventions mean?
– If function A calls function B, then function A must
save any temporary registers that it may be using
onto the stack before making a jal call.
– Function B must save any S (saved) registers it
intends to use before garbling up their values
– Remember: Caller/callee need to save only
temporary / saved registers they are using, not all
registers.
55
Register Conventions (5/6)
• Note that, if the callee is going to use some s
registers, it must:
– save those s registers on the stack
– use the registers
– restore s registers from the stack
– jr $ra
• With the temp registers, the callee doesn’t need to
save onto the stack.
• Therefore the caller must save those temp registers
that it would like to preserve though the call.
56
Other Registers (6/6)
• $at:
may be used by the assembler at any time;
always unsafe to use
• $k0-$k1: may be used by the kernel at any time;
unsafe to use
• $gp:
don’t worry about it
• $fp:
don’t worry about it
• Note: Read up on $gp and $fp in Appendix A.
57
Example: Compile This
main() {
int i,j,k,m; /* i-m:$s0-$s3 */
i = mult(j,k); ... ;
m = mult(i,i); ...
}
int mult (int mcand, int mlier){
int product;
product = 0;
while (mlier > 0) {
product += mcand;
mlier -= 1;
}
return product;
}
58
Example: Compile This
__start:
add $a0, $s1, $0
# arg0 = j
add $a1, $s2, $0
# arg1 = k
jal mult
# call mult
add $s0, $v0, $0
# i = mult()
..
add $a0, $s0, $0
# arg0 = i
add $a1, $s0, $0
# arg1 = i
jal mult
# call mult
add $s3, $v0, $0
...
# m = mult()
done:
59
Example: Compile This
• Notes:
– main function ends with done, not jr $ra, so there’s no
need to save $ra onto stack
– all variables used in main function are saved registers, so
there’s no need to save these onto stack
60
Example: Compile This
mult:
add
Loop:
slt
beq
add
addi
j
$t0,$0,$0
# prod = 0
$t1,$0,$a1
$t1,$0,Fin
$t0,$t0,$a0
$a1,$a1,-1
Loop
#
#
#
#
#
mlr > 0?
no => Fin
prod += mc
mlr -= 1
goto Loop
add
$v0,$t0,$0
# $v0 = prod
jr
$ra
# return
Fin:
61
Example: Compile This
• Notes:
– no jal calls are made from mult and we don’t use any saved
registers, so we don’t need to save anything onto stack
– temp registers are used for intermediate calculations (could have
used s registers, but would have to save the caller’s on the stack.)
– $a1 is modified directly (instead of copying into a temp register)
since we are free to change it
– result is put into $v0 before returning
62
Loops in C/Assembly
• There are three types of loops in C:
– while
– Do while
– for
° Key Concept: Though there are multiple ways of writing a loop
in MIPS, conditional branch is key to decision making
63
Inequalities in MIPS
• Until now, we’ve only tested equalities
(== and != in C). General programs need to test < and > as well.
• Create a MIPS Inequality Instruction:
– <Set on Less Than>
– Syntax: slt
reg1,reg2,reg3
– Meaning:
if (reg2 < reg3)
reg1 = 1;
else
reg1 = 0;
– In computereeze, “set” means “set to 1”,
“reset” or “clear” means “set to 0”.
64
Inequalities in MIPS
• How do we use this?
• Compile by hand:
if (g < h) goto Less;
• Use this mapping:
g: $s0, h: $s1
65
Inequalities in MIPS
• Final compiled MIPS code:
slt $t0,$s0,$s1
# $t0 = 1 if g<h
bne $t0,$0,Less
# goto Less
# if $t0!=0
# (if (g<h))
Less:
• Branch if $t0 != 0 or (g < h)
– Register $0 always contains the value 0, so bne and beq often use it
for comparison after an slt instruction.
66
Inequalities in MIPS (5/5)
 4 combinations of slt & beq/bneq:
slt $t0,$s0,$s1
# $t0 = 1 if g<h
bne $t0,$0,Less
# if(g<h) goto Less
slt $t0,$s1,$s0 # $t0 = 1 if g>h
bne $t0,$0,Grtr # if(g>h) goto Grtr
slt $t0,$s0,$s1
# $t0 = 1 if g<h
beq $t0,$0,Gteq
# if(g>=h) goto Gteq
slt $t0,$s1,$s0 # $t0 = 1 if g>h
beq $t0,$0,Lteq
# if(g<=h) goto Lteq
67
Immediates in Inequalities
• There is also an immediate version of slt to test against
constants: slti
– Helpful in for loops
C
M
I
P
S
if (g >= 1) goto Loop
Loop:
. . .
slti $t0,$s0,1
beq
$t0,$0,Loop
# $t0 = 1 if
# $s0<1 (g<1)
# goto Loop
# if $t0==0
# (if (g>=1))
68
Example: The C Switch Statement
•
Choose among four alternatives depending on whether k has the
value 0, 1, 2 or 3. Compile this C code:
switch (k) {
case 0: f=i+j; break; /* k=0*/
case 1: f=g+h; break; /* k=1*/
case 2: f=g-h; break; /* k=2*/
case 3: f=i-j; break; /* k=3*/
}
69
Example: The C Switch Statement
• This is complicated, so simplify.
• Rewrite it as a chain of if-else statements, which we already
know how to compile:
if(k==0) f= i + j;
else if(k==1) f= g + h;
else if(k==2) f= g - h;
else if(k==3) f= - j;
• Use this mapping:
f: $s0, g: $s1, h: $s2, i: $s3, j: $s4, k: $s5
70
Example: The C Switch Statement
• Final compiled MIPS code:
bne
add
j
L1:
addi
bne
add
j
L2:
addi
bne
sub
j
L3:
addi
bne
sub
Exit:
$s5, $0, L1
$s0, $s3, $s4
Exit
# branch k!=0
# k==0 so f=i+j
# end of case so Exit
$t0, $s5, -1
$t0, $0, L2
$s0, $s1, $s2
Exit
#
#
#
#
$t0 = k-1
branch k != 1
k==1 so f=g+h
end of case so Exit
$t0, $s5, -2
$t0, $0, L3
$s0, $s1, $s2
Exit
#
#
#
#
$t0=k-2
branch k != 2
k==2 so f=g-h
end of case so Exit
$t0, $s5, -3
$t0, $0, Exit
$s0, $s3, $s4
# $t0 = k-3
# branch k != 3
# k==3 so f=i-j
71
Case/Switch via Jump Address Table
• Notice that last case must wait for n-1 tests before executing,
making it slow
• Alternative tries to go to all cases equally fast: jump address
table for scalability
– Idea: encode alternatives as a table of addresses of the
cases
– Table is an array of words with addresses corresponding to
case labels
– Program indexes into table and jumps
• MIPS instruction "jump register" (jr) unconditionally
branches to address in register; use load to get address
72
Jump Address Table
Base register:
beginning of
jump table
Code for alt1
0
Baddr + 0
Code for alt2
4
Baddr + 4
Code for alt3
8
Baddr + 8
Code for altk
k 4
Baddr + k 4
Code blocks for each one of the cases in the switch statement
No testing for each case condition is necessary here.
73
Case/Switch via Jump Address Table
• Use k to index a jump address table, and then jump via the
value loaded
• 1st test that k matches 1 of cases (0  k  3); if not, the
code exits
slti
$t3, $s5, 0
bne $t3, $zero, LOW
slti
$t3, $s5, 4
beq $t3, $zero, LOW
# Test if k < 0
# if k<0,goto Exit
# Test if k < 4
# if k>=4,goto Exit
• Multiply k by 4 to index table of words:
add $t1, $s5, $s5 # Temp reg $t1 = 2*k
add $t1, $t1, $t1 # Temp reg $t1 = 4*k
74
Case/Switch via Jump Address Table
• Assume 4 sequential words in memory, base address
in $t2, have addresses corresponding to labels L0, L1,
L2, L3.
add
$t1, $t1, $t2
# $t1 = &JumpTable[k]
lw
$t1, 0($t1)
# $t1 = JumpTable[k]
• Now jump using address in register $t1:
jr
$t1
# jump based on reg $t1
75
Case/Switch via Jump Address Table
• Cases jumped to by jr:
L0:
add $s0, $s3, $s4 # k=0 so f = i + j
j Exit
# end case, goto
L1: add $s0, $s1, $s2 # k=1 so f = g +
j Exit
# end case, goto
L2: sub $s0, $s1, $s2 # k=2 so f = g +
j Exit
# end case, goto
L3: sub $s0, $s3, $s4 # k=3 so f = i –
j Exit
# end of switch
statement
LOW:
addi
$s0, $0, -1
# f = -1;
Exit
h
Exit
h
Exit
Exit:
76
Jump Address Table: Summary
slti
bne
slti
beq
add
add
add
lw
$t3,
$t3,
$t3,
$t3,
$t1,
$t1,
$t1,
$t1,
jr
$t1
L0:
j
L1:
j
L2:
j
L3:
add
Exit
add
Exit
sub
Exit
sub
LOW: addi
Exit:Exit:
$s5, 0
$zero, LOW
$s5, 4
$zero, LOW
$s5, $s5
$t1, $t1
$t1, $t2
0($t1)
#
#
#
#
#
#
#
#
Test if k < 0
if k < 0, goto otherwise
Test if k < 4
if k >= 4, goto otherwise
Temp reg $t1 = 2*k
Temp reg $t1 = 4*k
$t1 = & JumpTable[k]
$t1 = JumpTable[k]
# jump based on $t1
$s0, $s3, $s4
$s0, $s1, $s2
$s0, $s1, $s2
$s0, $s3, $s4
#
#
#
#
#
#
#
k=0
end
k=1
end
k=2
end
k=3
so f = i +
case, goto
so f = g +
case, goto
so f = g –
case, goto
so f = i –
j
Exit
h
Exit
h
Exit
j
$s0, $0, -1 # f = -1;
# end of switch statement
77
Things to Remember
• A Decision allows us to decide which pieces of code to execute
at run-time rather than at compile-time.
• C Decisions are made using conditional statements within an if,
while, do while or for.
• MIPS Decision making instructions are the conditional branches:
beq and bne.
• To help the conditional branches make decisions concerning
inequalities, we introduce a single instruction: <Set on Less
Than> called slt, slti, sltu, sltui
78
Announcements
•
•
•
•
Lab 2 will be released this week
Ensure that your CS accounts have been activated
Make sure your key card lets you get into the lab
(HRBB Rm 209)
Keep up-to-date with material taught in class
79