Title: CS 2200 Lecture 03b Instruction Set Architectures ISAs
1CS 2200 Lecture 03bInstruction Set
Architectures (ISAs)
- (Lectures based on the work of Jay Brockman,
Sharon Hu, Randy Katz, Peter Kogge, Bill Leahy,
Ken MacKenzie, Richard Murphy, and Michael
Niemier)
2A REALLY long example
3?
2000 fact sub sp,sp,8 adjust stack for
2 2004 sw ra,4(sp) save the return
addr 2008 sw a0,0(sp) save arg
n 2012 slt t0,a0,1 test for n lt
1 2016 beq t0,zero,L1 if n gt 1, goto
L1 2020 add v0,zero,1 return 1 2024
add sp,sp,8 pop 2 off stack 2028
jr ra return to caller 2032 L1
sub a0,a0,1 n gt 1 arg gets n-1 2036
jal fact call fact w/ (n-1) 2040
lw a0,0(sp) ret frm jal restr n 2044
lw ra,4(sp) restore ret addr 2048
add sp,sp,8 adj stk ptr (pop 2) 2052
mul v0,a0,v0 ret n fact(n-1) 2056
jr ra return to caller
0
0
0
0
0
0
0
0
0
v0
0
Assume that 1.) fact(n) is called with a value of
n 4, 2.) a return address of 1000, 3.) and the
stack pointer is as shown
ret. val.
0
a0
4
func. arg.
0
t0
0
sp
...
0
ra
1000
0
4(No Transcript)
5(No Transcript)
6Why are we saving the n argument?
7if nlt1, were done n 4 therefore were not
done here, 4 lt 1 is not true so t0 0
8t0 zero, therefore goto L1 (do not pass go,
do not collect 200)
9We have to call fact() again here, we just
update the n value (i.e. n now 3)
10Recursively call fact(3) Now we want to return to
where we left off (or PC4) inside function
call note jal stands for jump link
11Again, adjust the SP to store n and RA (this time
within a previous call to fact())
12again, save RA
13again, save n
14again, evaluate test case (is nlt1? here, no.)
15again, test set register (t0 still ! 1,
therefore goto L1)
16Our terminating condition has still not been
met recall return(nfact(n-1)) Therefore, we
decrement n again
17and call fact() again
18(No Transcript)
19Note that we want to return to the exact same
place as before Like in a HLL, we can generate
less code by using functions
the same applies for assembly (we only NEED one
block of code) Therefore, return to same address
20(No Transcript)
21(No Transcript)
22(No Transcript)
23(No Transcript)
24(No Transcript)
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29(No Transcript)
30(No Transcript)
31(No Transcript)
32(No Transcript)
33(No Transcript)
34(No Transcript)
35Finally, n lt 1 Therefore, t0 1.
36This time, we do NOT branch to L1 1 ! 0,
therefore we can start returning values
37Recall that v0 is a return register. This is the
1 time where we reached a terminating
condition. Therefore, we return 1
38Now, we can pop parameters from this function
call off of the stack because we dont need them.
39Why dont we need to load something into ra here?
40There was no next function call (therefore ra
already had 2040 in it) Now, we can load the
value we called function with...
41And, we can restore the address that we need to
return to from this function call
We cant depend on ra b/c there was another
function call after this one Even though it is
the same value, it may not be
42Pop the next call off of the stack
43Were now in else part of the function else
return(nfact(n-1))
44return to caller
45Now, we need to do the exact same stuff a few
more times
46(No Transcript)
47(No Transcript)
48here we have the value of 2!
49(No Transcript)
50(No Transcript)
51(No Transcript)
52(No Transcript)
53here we have the value of 3!
54(No Transcript)
55(No Transcript)
56(No Transcript)
57(No Transcript)
58here we have the value of 4!
59(No Transcript)
60Recall caller/callee mechanics
who does what when?
foo() bar(int a)
int temp 3 bar(42)
... ...
return(temp a)
2. callee at entry
1. caller at call time
4. caller after return
3. callee at exit
61But first, strategy
do most work at callee entry/exit
- Caller at call time
- put arguments in a0..a4
- jalr ..., ra
- Callee at entry
- Callee at exit
- put return value in v0
- Caller after return
- retrieve return value from v0
62Good Strategy
do most work at callee entry/exit
- Caller at call time
- put arguments in a0..a4
- jalr ..., ra
- Callee at entry
- allocate all stack space
- save ra s0..s3 if necessary
- Callee at exit
- restore ra s0..s3 if used
- deallocate all stack space
- put return value in v0
- Caller after return
- retrieve return value from v0
most of the work
63Good Strategy
do most work at callee entry/exit
- Caller at call time
- put arguments in a0..a4
- save any caller-save temporaries
- jalr ..., ra
- Callee at entry
- allocate all stack space
- save ra s0..s3 if necessary
- Callee at exit
- restore ra s0..s3 if used
- deallocate all stack space
- put return value in v0
- Caller after return
- retrieve return value from v0
- restore any caller-save temporaries
most of the work
64Summary
- Summary
- Caller saves registers (outside the agreed upon
convention i.e. ax) at point of call - Callee saves registers (per convention i.e. sx)
at point of entry - Callee restores saved registers, and re-adjusts
stack before return - Caller restores saved registers, and re-adjusts
stack before resuming from the call - Big ?
- What conventions did we use in the long example?
65Special Cases
- What to do if there are more parameters than
registers available for parameters? - i.e. you have int foo(int a, int b, int c, int d,
int e, int f, int g, int j, int k) - but with register conventions, only have a0
a4 to use to pass in arguments - Push them onto stack before call
66Warning!
- You may have been slightly mislead about what can
and cannot be done with a stack! - Must maintain push/pop integrity
- Can we go into the middle of the stack and read
write something?
67Callee Conventions
- Local variables for procedures
- Allocate on stack
- Software convention
- Designate a register as a frame pointer (fp)
- Sets bounds on stack values
- Makes references to local variables easier
- Why? (youll see)
- Save/restore fp on call/return
- Push activation record on stack on call
- Pop activation record on return
- Additional parameters placed above fp
68Frame pointer
fp
Addl args Frame Ptr
Addl args Frame Ptr
sp
Arg regs
Ret addr
Other regs
Locals
Before Call
After Call
During Call
69Example
- Assume we are calling a function with 8
parameters p, q, r, s, t, u, v, w - The function will be calling additional functions
so we need to save all the arguments in a0-a4
on the stack
70Example (cont.)
- Caller
- Assume that we have put first five args in
a0-a4 - add sp, sp, -3 Move stack pointer to
make - room for 3 add'l args
- sw s0, 2(sp) Push extra args on stack
- sw s1, 1(sp)
- sw s2, 0(sp)
- jalr s3, ra Assume s3 has address
of sub - yada
- yada
- yada
71Example (cont.)
- Callee
- sub
- add sp, sp, -1 Make room on stack for
old fp - sw fp, 0(sp) Push old fp onto stack
- add fp, zero, sp Copy current sp into fp
- add sp, sp, -6
- sw ra, 5(sp)
- sw a0, 4(sp) p (also at fp-2)
- sw a1, 3(sp) q (also at fp-3)
- sw a2, 2(sp) r
- sw a3, 1(sp) s
- sw a4, 0(sp) t
72Example
- So
-
- lw REG, 6(sp) gets old frame pointer
- lw REG, 5(sp) gets return address
- lw REG, 4(sp) gets p etc.
-
- But what if we move the stack pointer?
- Where is p?
- Recall ao (p) is accessible
- lw REG, -2(fp)
- (Youre complier will be smart and figure this
out) -
-
73Field NamesI-Type Instruction
address
rB
rA
op
name
20
4
4
4
bits
op opcode rA first register source
operand rB second register source
operand address offset from base register
74Other Goodies
- Jump and Link Register
- LC-2200 uses JALR for function calls
- Put address of function in reg
- lw s0, function(zero)
- Jump to function saving ret addr
- jalr s0 ra
- function
75Other Goodies
Or
- Jump and Link Register
- LC-2200 uses JALR for function calls
- Put address of function in reg
- addi s0, zero, function
- Jump to function saving ret addr
- jalr s0 ra
- function
76Other Goodies
- Jump and Link Register
- LC-2200 can also use JALR for simple jumps.
- Put address of label in reg
- lw s0, jumphere(zero) or
- addi s0, zero, jumphere,
- Jump to label discarding ret addr
- jalr s0 zero
- jumphere
77Field NamesJ-Type Instruction
unused
rB
rA
op
name
20
4
4
4
bits
op opcode rA Jump to address in this
register rB Put pc1 into this
register Note if rA and rB are the same
register pc1 is stored first and the the jump
is made to pc1 (i.e. the next instruction)
78Other Goodies
- Branching
- Relative branches
- PC-relative
- PC PC 1 address
- How to simulate jumping far away?
- beq s1, s2, L1 L1 too far!!!
- beq s1, s2, L2
- beq zero, zero, L3
- L2 jalr s0 zero s0 contains L1
- L3