cs2106 tutorial 01 - jiayushe.github.io
TRANSCRIPT
Self-Intro● DDP in Comp Science and Applied Mathematics
● Year 3
● Specialization in Algorithm & Theory, AI and Networking
3
Communication● Telegram Group
○ https://tinyurl.com/cs2106t3t4
● LumiNUS Forum
● Email Profs/TAs
4
Tutorial CA● 5% (max 6%)
○ 1% attendance○ 5% weekly LumiNUS quiz
■ Due at Friday 2359○ Can overflow into other CA components (aka Lab CA)
5
Process● Memory context
○ Text, Data, Stack, Heap
● Hardware context○ GP Registers, PC, SP, FP
● OS context○ PID, Process State
8
Q1
● Memory Layout
● For each variable○ Memory location○ Scope○ Lifetime ○ Component responsible for its allocation
10
int x;
int *p;
int f = 0;
int y = 0;
int main()
{
int g = 7;
p = malloc(100);
f = 6;
sub(f, g);
f = 8;
return 0;
}
void sub(int a, int b)
{
int c;
c = a;
a = x;
b = y;
x = y;
*p = b;
}
Q1
11
int x;
int *p;
int f = 0;
int y = 0;
int main()
{
int g = 7;
p = malloc(100);
f = 6;
sub(f, g);
f = 8;
return 0;
}
void sub(int a, int b)
{
int c;
c = a;
a = x;
b = y;
x = y;
*p = b;
}
Text
Data
Stack
Heap
Q1
● x, p, f, y
○ Data, global, alive during execution of entire programme, compiler
● g, c, a, b
○ Stack, local within function, alive during execution of function, compiler
● int[25] (100 bytes)
○ Heap, global, alive until explicit deallocation, OS
12
Q2a
14
C
int iFact( int N )
{
int result = 1, i;
for (i = 2; i <= N; i++){
result = result * i;
}
}
MIP-like Assembly Code
iFact:
.........
#Part (a)
addi $11, $0, 1
sw $11, ??($fp)
addi $12, $0, 2
sw $12, ??($fp)
lw $13, ??($fp)
loop: bgt $12, $13, end
mul $11, $11, $12
sw $11, ??($fp)
addi $12, $12, 1
sw $12, ??($fp)
j loop
end: .........
Q2a
15
C
int iFact( int N )
{
int result = 1, i;
for (i = 2; i <= N; i++){
result = result * i;
}
}
MIP-like Assembly Code
iFact:
.........
#Part (a)
addi $11, $0, 1
sw $11, -24($fp)
addi $12, $0, 2
sw $12, -20($fp)
lw $13, -16($fp)
loop: bgt $12, $13, end
mul $11, $11, $12
sw $11, -24($fp)
addi $12, $12, 1
sw $12, -20($fp)
j loop
end: .........
Q2b
16
MIP-like Assembly Code
### Main Function
main:
......
#irrelevant code omitted
#Part (b) – Caller prepare
to call function
addi $13, $0, 10
sw ????
call iFact, ??
C
int main( )
{
... = iFact( 10 );
}
Q2b
17
MIP-like Assembly Code
### Main Function
main:
......
#irrelevant code omitted
#Part (b) – Caller prepare
to call function
addi $13, $0, 10
sw $13, -28($sp)
call iFact, 0($sp)
C
int main( )
{
... = iFact( 10 );
}
Q2c
18
MIP-like Assembly Code
iFact:
#Part (c) –
# Callee enter function
save GPRs
save SP, FP
move SP, FP
#Part (a)
addi $11, $0, 1
sw $11, -24($fp)
addi $12, $0, 2
sw $12, -20($fp)
lw $13, -16($fp)
......
Q2c
19
MIP-like Assembly Code
iFact:
#Part (c) –
# Callee enter function
sw $11, -20($sp)
sw $12, -16($sp)
sw $13, -12($sp)
sw $sp, -8($sp)
sw $fp, -4($sp)
addi $sp, $sp, -40
addi $fp, $sp, 28
#Part (a)
addi $11, $0, 1
sw $11, -24($fp)
addi $12, $0, 2
sw $12, -20($fp)
lw $13, -16($fp)
......
Q2d
20
MIP-like Assembly Code
#Part (d)
# Callee exit function
save Return Results
restore GPRs
restore SP, FP
return saved PC
Q2d
21
MIP-like Assembly Code
#Part (d)
# Callee exit function
sw $11, -12($fp)
lw $11, -8($fp)
lw $12, -4($fp)
lw $13, 0($fp)
lw $sp, 4($fp)
lw $fp, 8($fp)
return 0($sp)
Q2
22
MIP-like Assembly Code
iFact:
#Part (c) – Callee enter function
sw $11, -20($sp)
sw $12, -16($sp)
sw $13, -12($sp)
sw $sp, -8($sp)
sw $fp, -4($sp)
addi $sp, $sp, -40
addi $fp, $sp, 28
#Part (c) – Callee enter function ends
addi $11, $0, 1
sw $11, -24($fp) ##Part (a)
addi $12, $0, 2
sw $12, -20($fp)
lw $13, -16($fp)
loop: bgt $12, $13, end
mul $11, $11, $12 #assume no overflow
sw $11, -24($fp) ##Part (a)
addi $12, $12, 1
sw $12, -20($fp) ##Part (a)
j loop
end:
MIP-like Assembly Code
#Part (d) – Callee exit function
sw $11, -12($fp)
lw $11, -8($fp)
lw $12, -4($fp)
lw $13, 0($fp)
lw $sp, 4($fp)
lw $fp, 8($fp)
return 0($sp)
#Part (d) – Callee exit function
### Main Function
main:
......
#irrelevant code omitted
#Part (b) – Caller prepare to call
function
addi $13, $0, 10
sw $13, -28($sp)
call iFact, 0($sp)
Q3
23
void change( int<Ref> i ) { //i is a pass-by-reference parameter
i = 1234; //this changes main's variable myInt in this case
}
int main() {
int myInt = 0;
change( myInt ); //myInt become 1234 after the function call
...... //other variable declarations and code
}
Q3a
● Suppose the main()'s and change()'s stack frame has been properly setup, and change() is now executing, show how to store the value 1234 into the right location.
● Register_D <- Load Offset(Register_S)
● Offset(Register_D) <- Store Value
24
Q3a
● Suppose the main()'s and change()'s stack frame has been properly setup, and change() is now executing, show how to store the value 1234 into the right location.
● $R1 <- Load 4($FP) //get saved FP, i.e. main's FP
● ‐4($R1) <- Store 1234 //don’t forget the offset
25
Q3b
● Briefly describe another usage scenario for pass-by-reference parameter that will not work with this approach.
26
Q3b
● Briefly describe another usage scenario for pass-by-reference parameter that will not work with this approach.
● A possible scenario is when change() is recursively called/called in a chain.
27
Q3c
● How to fix?
● Place the actual address of the value. Change the instruction to load/store from that address.
29