cmput229 - fall 2003
Post on 08-Jan-2016
27 Views
Preview:
DESCRIPTION
TRANSCRIPT
CMPUT 229 - Computer Organization and Architecture I
1
CMPUT229 - Fall 2003
TopicC: Pointers and Arrays
José Nelson Amaral
CMPUT 229 - Computer Organization and Architecture I
2
Reading Material
The slides for this topic were prepared based on chapters 17 of:
Patt, Yale N., and Patel, Sanjay J., Introduction to Computing Systems: from bits & gates to C & Beyond, McGrawHill Press, 2001.
An excellent reference book for the C Language is:
Harbison, Samuel P., and Steele Jr., Guy, C: A Reference Manual, Prentice Hall, 4th Edition, 1995.
CMPUT 229 - Computer Organization and Architecture I
3
Example: A swap function
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Patt and Patel, pp. 366
CMPUT 229 - Computer Organization and Architecture I
4
Assembly for Swap Generated with -O0 (part 1)
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
# 5 main() # 6 {
addiu $sp,$sp,-32 # .frame.len.mainsd $gp,16($sp) # .lcl_spill_b002sd $ra,8($sp) # .lcl_spill_b001lui $a3,%hi(%neg(%gp_rel(main +0)))addiu $a3,$a3,%lo(%neg(%gp_rel(main +0))) addu $gp,$t9,$a3
# 7 int valueA = 3;addiu $a2,$zero,3 sw $a2,0($sp) # valueA
# 8 int valueB = 4;addiu $a1,$zero,4 sw $a1,4($sp) # valueB
# 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);lw $a0,%got_page(.rodata)($gp) addiu $a0,$a0,%got_ofst(.rodata) lw $a1,0($sp) # valueAlw $a2,4($sp) # valueBlw $t9,%call16(printf)($gp) jalr $t9 # printfnop
$a1
Stack
$a2
$sp$a0
0
4
3
434bash-2.01$ ./swapO0
Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
5
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 2)
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lw $a0,0($sp) # valueAlw $a1,4($sp) # valueBlw $t9,%call16(Swap)($gp) jalr $t9 # Swapnop
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
lw $a0,%got_page(.rodata+48)($gp)addiu $a0,$a0,%got_ofst(.rodata+48)lw $a1,0($sp) # valueAlw $a2,4($sp) # valueBlw $t9,%call16(printf)($gp) jalr $t9 # printfnop
.BB4.main: # 0x74 # 13 }
or $v0,$zero,$zero ld $gp,16($sp) # .lcl_spill_b002ld $ra,8($sp) # .lcl_spill_b001addiu $sp,$sp,32 # .frame.len.mainjr $ra nop
4$a1
Stack
3$a2
43$sp
$a0 30
4
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
6
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 {
addiu $sp,$sp,-32 # .frame.len.Swapsw $a0,20($sp) # firstValsw $a1,28($sp) # secondVal
# 17 int tempVal; # 18 # 19 tempVal = firstVal;
lw $v1,20($sp) # firstValsw $v1,0($sp) # tempVal
# 20 firstVal = secondVal;lw $v0,28($sp) # secondValsw $v0,20($sp) # firstVal
# 21 secondVal = tempVal;lw $at,0($sp) # tempValsw $at,28($sp) # secondVal
# 22 }addiu $sp,$sp,32 # .frame.len.Swapjr $ra nop .end Swap
4$a1
3$a2
3$a0
Stack43$sp 0
4
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
7
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 {
addiu $sp,$sp,-32 # .frame.len.Swapsw $a0,20($sp) # firstValsw $a1,28($sp) # secondVal
# 17 int tempVal; # 18 # 19 tempVal = firstVal;
lw $v1,20($sp) # firstValsw $v1,0($sp) # tempVal
# 20 firstVal = secondVal;lw $v0,28($sp) # secondValsw $v0,20($sp) # firstVal
# 21 secondVal = tempVal;lw $at,0($sp) # tempValsw $at,28($sp) # secondVal
# 22 }addiu $sp,$sp,32 # .frame.len.Swapjr $ra nop .end Swap
4$a1
3$a2
3$a0
Stack43
$sp
3
4
$v1
$v0
3
3
4
4
$at 3
3
0
4
8
12
16
20
24
28
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
8
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 {
addiu $sp,$sp,-32 # .frame.len.Swapsw $a0,20($sp) # firstValsw $a1,28($sp) # secondVal
# 17 int tempVal; # 18 # 19 tempVal = firstVal;
lw $v1,20($sp) # firstValsw $v1,0($sp) # tempVal
# 20 firstVal = secondVal;lw $v0,28($sp) # secondValsw $v0,20($sp) # firstVal
# 21 secondVal = tempVal;lw $at,0($sp) # tempValsw $at,28($sp) # secondVal
# 22 }addiu $sp,$sp,32 # .frame.len.Swapjr $ra nop .end Swap
4$a1
3$a2
3$a0
4
3
3
Stack43$sp
3$v1
4$v0
$at 3
0
4
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
9
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lw $a0,0($sp) # valueAlw $a1,4($sp) # valueBlw $t9,%call16(Swap)($gp) jalr $t9 # Swapnop
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
lw $a0,%got_page(.rodata+48)($gp)addiu $a0,$a0,%got_ofst(.rodata+48)lw $a1,0($sp) # valueAlw $a2,4($sp) # valueBlw $t9,%call16(printf)($gp) jalr $t9 # printfnop
.BB4.main: # 0x74 # 13 }
or $v0,$zero,$zero ld $gp,16($sp) # .lcl_spill_b002ld $ra,8($sp) # .lcl_spill_b001addiu $sp,$sp,32 # .frame.len.mainjr $ra nop
Assembly for Swap Generated with -O0 (part 3)
4$a1
3$a2
3$a0
4
3
3
Stack43$sp
3$v1
4$v0
$at 3
0
4
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
10
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lw $a0,0($sp) # valueAlw $a1,4($sp) # valueBlw $t9,%call16(Swap)($gp) jalr $t9 # Swapnop
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
lw $a0,%got_page(.rodata+48)($gp)addiu $a0,$a0,%got_ofst(.rodata+48)lw $a1,0($sp) # valueAlw $a2,4($sp) # valueBlw $t9,%call16(printf)($gp) jalr $t9 # printfnop
.BB4.main: # 0x74 # 13 }
or $v0,$zero,$zero ld $gp,16($sp) # .lcl_spill_b002ld $ra,8($sp) # .lcl_spill_b001addiu $sp,$sp,32 # .frame.len.mainjr $ra nop
Assembly for Swap Generated with -O0 (part 3)
3$a1
4$a2
3$a0
4
3
3
Stack43$sp
3$v1
4$v0
$at 3
0
4
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4After Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
11
Addresses and Values
The problem with our swap program is that the main is passingthe values of variables valueA and valueB to the swap function.
Thus the swap function does all its work within its own frame inthe stack and never actually changes the state of the variablesin the main function.
Could a “smarter” compiler figure out that the swap function is doing nothing? Lets try the MIPSPro compiler with -O3.
CMPUT 229 - Computer Organization and Architecture I
12
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O3 (part 1)
# 5 main() # 6 {
lui $a1,%hi(%neg(%gp_rel(main +0))) # [0] addiu $sp,$sp,-16 # [0] .frame.len.mainsd $gp,8($sp) # [1] .gra_spill_b002addiu $a1,$a1,%lo(%neg(%gp_rel(main +0))) # [1] addu $gp,$t9,$a1 # [2] lw $t9,%got_disp(printf)($gp) # [3] .loc 1 10 3
# 7 int valueA = 3; # 8 int valueB = 4; # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);
lw $a0,%got_page(.rodata)($gp) # [4] addiu $a2,$zero,4 # [5] sd $ra,0($sp) # [5] .gra_spill_b001addiu $a1,$zero,3 # [5] jalr $t9 # [6] printfaddiu $a0,$a0,%got_ofst(.rodata) # [6]
.BB2.main: # 0x30 #<freq> BB:2 frequency = 1.00000 (heuristic)# 11 Swap(valueA, valueB);
lw $t9,%call16(Swap)($gp) # [0] addiu $a1,$zero,4 # [2] jalr $t9 # [3] Swapaddiu $a0,$zero,3 # [3]
$a1
Stack
$a2
$sp$a0
0
4
4
3
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
13
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O3 (part 1)
# 5 main() # 6 {
lui $a1,%hi(%neg(%gp_rel(main +0))) # [0] addiu $sp,$sp,-16 # [0] .frame.len.mainsd $gp,8($sp) # [1] .gra_spill_b002addiu $a1,$a1,%lo(%neg(%gp_rel(main +0))) # [1] addu $gp,$t9,$a1 # [2] lw $t9,%got_disp(printf)($gp) # [3] .loc 1 10 3
# 7 int valueA = 3; # 8 int valueB = 4; # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);
lw $a0,%got_page(.rodata)($gp) # [4] addiu $a2,$zero,4 # [5] sd $ra,0($sp) # [5] .gra_spill_b001addiu $a1,$zero,3 # [5] jalr $t9 # [6] printfaddiu $a0,$a0,%got_ofst(.rodata) # [6]
.BB2.main: # 0x30 #<freq> BB:2 frequency = 1.00000 (heuristic)# 11 Swap(valueA, valueB);
lw $t9,%call16(Swap)($gp) # [0] addiu $a1,$zero,4 # [2] jalr $t9 # [3] Swapaddiu $a0,$zero,3 # [3]
$a1
Stack
$a2
$sp$a0
0
4
4
3
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
14
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O3 (part 3)
Swap: # 0x70.frame $sp, 0, $ra
.BB1.Swap: # 0x70 #<freq> #<freq> BB:1 frequency = 1.00000 (heuristic) #<freq> # 18 # 19 tempVal = firstVal; # 20 firstVal = secondVal; # 21 secondVal = tempVal; # 22 }
jr $ra # [0] nop # [0]
$a1
Stack
$a2
$sp$a0
0
4
4
3
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
15
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O3 (part 2)
.BB3.main: # 0x40 #<freq> #<freq> BB:3 frequency = 1.00000 (heuristic) #<freq>
lw $t9,%got_disp(printf)($gp) # [0] # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
lw $a0,%got_page(.rodata+48)($gp) # [1] addiu $a2,$zero,4 # [2] addiu $a1,$zero,3 # [2] jalr $t9 # [3] printfaddiu $a0,$a0,%got_ofst(.rodata+48) # [3]
.BB4.main: # 0x58 #<freq> BB:4 frequency = 1.00000 (heuristic) # 13 }
ld $ra,0($sp) # [0] .gra_spill_b001or $v0,$zero,$zero # [2] ld $gp,8($sp) # [3] .gra_spill_b002jr $ra # [3] addiu $sp,$sp,16 # [3] .frame.len.main
$a1
Stack
$a2
$sp$a0
0
4
3
4
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4After Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
16
What happened at -O3?
While compiling the swap code, MIPSPro figured out that it wasdoing nothing of consequence, and thus replaced the body of thefunction with a simple return instruction.
However during the compilation of main, MIPSPro did not knowwhat swap was up to, and thus could not eliminate the call toswap.
This happens because, even at -O3, MIPSPro does not do inter-procedural analysis (IPA), and thus the compilation ofeach procedure is done in isolation.
In order to force MIPSPro do perform IPA, we would have toinclude the option -ipa in the command line. But then the compiler cannot produce the assembly files because IPA actuallytakes place during the linking phase of the compiler.
CMPUT 229 - Computer Organization and Architecture I
17
Example2: A new swap function
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Patt and Patel, pp. 371
CMPUT 229 - Computer Organization and Architecture I
18
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1) # 5 main()
# 6 {lui $a2,%hi(%neg(%gp_rel(main +0))) # [0] addiu $sp,$sp,-32 # [0] .frame.len.mainsd $gp,16($sp) # [1] .gra_spill_b002addiu $a2,$a2,%lo(%neg(%gp_rel(main +0))) # [1]
# 7 int valueA = 3;addiu $a1,$zero,3 # [2] sw $a1,0($sp) # [2] valueAaddu $gp,$t9,$a2 # [2] lw $t9,%got_disp(printf)($gp) # [3]
# 8 int valueB = 4;addiu $a3,$zero,4 # [4]
# 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);lw $a0,%got_page(.rodata)($gp) # [4] sd $ra,8($sp) # [5] .gra_spill_b001addiu $a2,$zero,4 # [5] addiu $a1,$zero,3 # [5] sw $a3,4($sp) # [6] valueBjalr $t9 # [6] printfaddiu $a0,$a0,%got_ofst(.rodata) # [6]
#<freq> BB:2 frequency = 1.00000 (heuristic) # 11 NewSwap(&valueA, &valueB);
lw $t9,%call16(NewSwap)($gp) # [0] addiu $a1,$sp,4 # [2] valueBjalr $t9 # [3] NewSwapaddiu $a0,$sp,0 # [3] valueA
Stack
0
4
$a2
$a3
$a0
$a1 33
4
0x7ffff0004
40x7ffff0000bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4 $sp
4
CMPUT 229 - Computer Organization and Architecture I
19
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1)
# 15 void NewSwap(int *firstVal, int *secondVal) # 16 { # 17 int tempVal; # 18 # 19 tempVal = *firstVal;
lw $at,0($a0) # 20 *firstVal = *secondVal;
lw $v0,0($a1) sw $v0,0($a0)
# 21 *secondVal = tempVal; # 22 }
jr $ra sw $at,0($a1)
Stack43 0
4
$a2
$a3 4
$a0 0x7ffff0000
$a1 0x7ffff0004
$at
$v0
$sp
3
34
4
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
CMPUT 229 - Computer Organization and Architecture I
20
Assembly for NewSwap Generated with -O3 (part 1)
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
lw $t9,%got_disp(printf)($gp)
# 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);lw $a0,%got_page(.rodata+48)($gp)lw $a2,4($sp) # [2] valueBlw $a1,0($sp) # [3] valueAjalr $t9 # [3] printfaddiu $a0,$a0,%got_ofst(.rodata+48)
Stack34 0
4
$a2
$a3 4
$a0 0x7ffff0000
$a1 0x7ffff0004
$at 3
$v0 4
$sp
3
4
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4After Swap: valueA = 4 and valueB = 3
CMPUT 229 - Computer Organization and Architecture I
21
The car.c program#include <stdio.h>#define STRINGLENGTH 20
typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node *next;} CarNode;
void ReadCar(CarNode *car);void PrintCar(CarNode car);
main(){ CarNode mycar; ReadCar(&mycar); PrintCar(mycar);}
void ReadCar(CarNode *car){ car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74;}
void PrintCar(CarNode car){ printf("vehicleID: %d\n",car.vehicleID); printf("make: %s\n",car.make); printf("model: %s\n",car.model); printf("year: %d\n",car.year); printf("mileage: %d\n",car.mileage); printf("cost: %f\n",car.cost);}
Patt and Patel, pp. 419
CMPUT 229 - Computer Organization and Architecture I
22
The car.c program
#define STRINGLENGTH 20
typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node next;} CarNode;
vehicleID
make[20]
model[20]
yearmileage
cost
next
048
1216202428323640444852566064
CMPUT 229 - Computer Organization and Architecture I
23
The car.c program#include <stdio.h>#define STRINGLENGTH 20
typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node next;} CarNode;
void ReadCar(CarNode car);void PrintCar(CarNode car);
main(){ CarNode mycar; ReadCar(&mycar); PrintCar(mycar);}
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
CMPUT 229 - Computer Organization and Architecture I
24
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t9
$t6
$t0
$t8
$t7
vehicleID
CMPUT 229 - Computer Organization and Architecture I
25
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t9
$t0
$t8
$t7
vehicleID
$t6
vehicleID
make[0-3]
CMPUT 229 - Computer Organization and Architecture I
26
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
$t6
vehicleID
make[0-3]
$t9
make[0-3]
make[4-7]
make[4-7]
CMPUT 229 - Computer Organization and Architecture I
27
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
$t6
vehicleID
make[0-3]
$t9
make[0-3]
make[4-7]
make[4-7]
make[8-11]
CMPUT 229 - Computer Organization and Architecture I
28
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
make[0-3]
$t9
make[0-3]
make[4-7]
make[8-11]
$t6
make[8-11]
make[12-15]
CMPUT 229 - Computer Organization and Architecture I
29
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
make[8-11]
vehicleID
make[12-15]
$t9
make[12-15]
make[16-19]
make[16-19]$t6
CMPUT 229 - Computer Organization and Architecture I
30
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
make[12-15]
make[0-3]
make[16-19]
make[4-7]
model[0-3]
$t6
$t9
CMPUT 229 - Computer Organization and Architecture I
31
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
make[12-15]
make[0-3]
make[4-7]
model[0-3]
$t6
model[0-3]
model[4-7]
$t9
CMPUT 229 - Computer Organization and Architecture I
32
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
model[0-3]
vehicleID
model[4-7]
$t9
model[4-7]
model[8-11]
model[8-11]$t6
CMPUT 229 - Computer Organization and Architecture I
33
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
model[4-7]
make[0-3]
model[8-11]
make[4-7]
model[12-15]
$t6
$t9
CMPUT 229 - Computer Organization and Architecture I
34
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
make[12-15]
make[0-3]
make[4-7]
model[12-15]
$t6
model[12-15]
model[16-19]
$t9
CMPUT 229 - Computer Organization and Architecture I
35
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
model[12-15]
vehicleID
model[16-19]
$t9
model[16-19]
year
year$t6
CMPUT 229 - Computer Organization and Architecture I
36
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
model[16-19]
make[0-3]
year
make[4-7]
mileage
$t6
$t9
CMPUT 229 - Computer Organization and Architecture I
37
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
vehicleID
make[16-19]
make[0-3]
make[4-7]
mileage
$t6
mileage
$t9
CMPUT 229 - Computer Organization and Architecture I
38
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
model[12-15]
vehicleID
$t9
cost1
cost1$t6
CMPUT 229 - Computer Organization and Architecture I
39
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
vehicleID
make[0-3]
make[4-7]
$t8
$t7
$t9
cost1
$t6
cost2
CMPUT 229 - Computer Organization and Architecture I
40
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
vehicleID
make[0-3]
make[4-7]
$t6
cost2
$t8
$t7
$t9
cost2
next
CMPUT 229 - Computer Organization and Architecture I
41
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
cost2
vehicleID
next
next
$t9$t6
CMPUT 229 - Computer Organization and Architecture I
42
The car.c program
main:subu $sp, 168sw $ra, 84($sp).frame $sp, 168, $ra
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 88jal ReadCar
# 27 PrintCar(mycar);addu $t6, $sp, 88move $t9, $spaddu $t0, $t6, 72
L1:lw $t8, 0($t6)addu $t6, $t6, 12sw $t8, 0($t9)lw $t7, -8($t6)addu $t9, $t9, 12sw $t7, -8($t9)lw $t8, -4($t6)sw $t8, -4($t9)bne $t6, $t0, L1lw $a0, 0($sp)lw $a1, 4($sp)lw $a2, 8($sp)lw $a3, 12($sp)jal PrintCar
# 28 }move $v0, $zerolw $ra, 84($sp)addu $sp, 168j $ra
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
$t0
$t8
$t7
cost2]
vehicleID
next
$t9
next
$t6
$a0vehicleID
$a1make[0-3]
$a2make[4-7]
$a3make[8-11]
CMPUT 229 - Computer Organization and Architecture I
43
The car.c program # 42 void PrintCar(CarNode car) # 43 {PrintCar:
subu $sp, 32sw $ra, 28($sp)sw $a0, 32($sp)sw $a1, 36($sp)sw $a2, 40($sp)sw $a3, 44($sp)
# 44 printf("vehicleID: %d\n",car.vehicleID);la $a0, string13lw $a1, 32($sp)jal printf
# 45 printf("make: %s\n",car.make);la $a0, string14addu $a1, $sp, 32addu $a1, $a1, 4jal printf
# 46 printf("model: %s\n",car.model);la $a0, string15addu $a1, $sp, 32addu $a1, $a1, 24jal printf
# 47 printf("year: %d\n",car.year);la $a0, string16lw $a1, 76($sp)jal printf
next
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
180
184
188
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
vehicleID
next
$a0vehicleID
$a1make[0-3]
$a2make[4-7]
$a3make[8-11]
CMPUT 229 - Computer Organization and Architecture I
44
The car.c program # 42 void PrintCar(CarNode car) # 43 {PrintCar:
subu $sp, 32sw $ra, 28($sp)sw $a0, 32($sp)sw $a1, 36($sp)sw $a2, 40($sp)sw $a3, 44($sp)
# 44 printf("vehicleID: %d\n",car.vehicleID);la $a0, string13lw $a1, 32($sp)jal printf
# 45 printf("make: %s\n",car.make);la $a0, string14addu $a1, $sp, 32addu $a1, $a1, 4jal printf
# 46 printf("model: %s\n",car.model);la $a0, string15addu $a1, $sp, 32addu $a1, $a1, 24jal printf
# 47 printf("year: %d\n",car.year);la $a0, string16lw $a1, 76($sp)jal printf
$ra<ra>
cost2
cost1
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
make[0-3]
make[0-3]
vehicleID
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
vehicleID
next
$a0vehicleID
$a1make[0-3]
$a2make[4-7]
$a3make[8-11] SP00
04
08
12
16
20
24
28
32
96
100
104
108
112116
120
124
<ra>
CMPUT 229 - Computer Organization and Architecture I
45
Why so much copying?
The program car.c passes the datastructure CarNodeto the PrintCar function by value.
A copy of each byte of CarNode must be made in thestack for each call of the function PrintCar.
We could, instead have passed the address of the copyof CarNode that we already had in the stack.
CMPUT 229 - Computer Organization and Architecture I
46
The car2.c Program#include <stdio.h>#define STRINGLENGTH 20
typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node next;} CarNode;
void ReadCar(CarNode car);void PrintCar(CarNode car);
main(){ CarNode mycar; ReadCar(&mycar); PrintCar(&mycar);}
void ReadCar(CarNode car){ car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74;}
void PrintCar(CarNode car){ printf("vehicleID: %d\n",car->vehicleID); printf("make: %s\n",car->make); printf("model: %s\n",car->model); printf("year: %d\n",car->year); printf("mileage: %d\n",car->mileage); printf("cost: %f\n",car->cost);}
CMPUT 229 - Computer Organization and Architecture I
47
car-O0.s car2-O0.s
#include <stdio.h>#define STRINGLENGTH 20
typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node next;} CarNode;
void ReadCar(CarNode car);void PrintCar(CarNode car);
main(){ CarNode mycar; ReadCar(&mycar); PrintCar(&mycar);}
main:subu $sp, 112sw $ra, 28($sp)
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 32jal ReadCar
# 27 PrintCar(&mycar);addu $a0, $sp, 32jal PrintCar
# 28 }move $v0, $zerolw $ra, 28($sp)addu $sp, 112j $ra
CMPUT 229 - Computer Organization and Architecture I
48
car2-O0.s
main:subu $sp, 112sw $ra, 28($sp)
# 23 CarNode mycar; # 25 ReadCar(&mycar);
addu $a0, $sp, 32jal ReadCar
# 27 PrintCar(&mycar);addu $a0, $sp, 32jal PrintCar
# 28 }move $v0, $zerolw $ra, 28($sp)addu $sp, 112j $ra
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
96
100
make[0-3]
vehicleID
$a0
CMPUT 229 - Computer Organization and Architecture I
49
car2-O0.sPrintCar:
subu $sp, 32sw $ra, 28($sp)sw $a0, 32($sp)
# 43 printf("vehicleID: %d\n",car->vehicleID);la $a0, $$13lw $t6, 32($sp)lw $a1, 0($t6)jal printf
# 44 printf("make: %s\n",car->make);la $a0, $$14lw $a1, 32($sp)addu $a1, $a1, 4jal printf
# 45 printf("model: %s\n",car->model);la $a0, $$15lw $a1, 32($sp)addu $a1, $a1, 24jal printf
# 46 printf("year: %d\n",car->year);la $a0, $$16lw $t7, 32($sp)lw $a1, 44($t7)jal printf
# 47 printf("mileage: %d\n",car->mileage);la $a0, $$17lw $t8, 32($sp)lw $a1, 48($t8)jal printf
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
SP00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
96
100
make[0-3]
vehicleID
$a0
CMPUT 229 - Computer Organization and Architecture I
50
car2-O0.sPrintCar:
subu $sp, 32sw $ra, 28($sp)sw $a0, 32($sp)
# 43 printf("vehicleID: %d\n",car->vehicleID);la $a0, string13lw $t6, 32($sp)lw $a1, 0($t6)jal printf
# 44 printf("make: %s\n",car->make);la $a0, string14lw $a1, 32($sp)addu $a1, $a1, 4jal printf
# 45 printf("model: %s\n",car->model);la $a0, string15lw $a1, 32($sp)addu $a1, $a1, 24jal printf
# 46 printf("year: %d\n",car->year);la $a0, string16lw $t7, 32($sp)lw $a1, 44($t7)jal printf
# 47 printf("mileage: %d\n",car->mileage);la $a0, string17lw $t8, 32($sp)lw $a1, 48($t8)jal printf
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
SP
make[0-3]
vehicleID
$a0
$ra<ra>
00
04
08
12
16
20
24
28 <ra>
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
32
96
100
104
108
112
116
120124128
132
SP+64
CMPUT 229 - Computer Organization and Architecture I
51
car2-O0.sPrintCar:
subu $sp, 32sw $ra, 28($sp)sw $a0, 32($sp)
# 43 printf("vehicleID: %d\n",car->vehicleID);la $a0, string13lw $t6, 32($sp)lw $a1, 0($t6)jal printf
# 44 printf("make: %s\n",car->make);la $a0, string14lw $a1, 32($sp)addu $a1, $a1, 4jal printf
# 45 printf("model: %s\n",car->model);la $a0, string15lw $a1, 32($sp)addu $a1, $a1, 24jal printf
# 46 printf("year: %d\n",car->year);la $a0, string16lw $t7, 32($sp)lw $a1, 44($t7)jal printf
# 47 printf("mileage: %d\n",car->mileage);la $a0, string17lw $t8, 32($sp)lw $a1, 48($t8)jal printf
next
cost
cost
mileage
year
model[16-19]
model[12-15]
model[8-11]
model[4-7]
model[0-3]
make[16-19]
make[12-15]
make[8-11]
make[4-7]
SP
make[0-3]
vehicleID
$a0
$ra<ra>
00
04
08
12
16
20
24
28 <ra>
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92
32
96
100
104
108
112
116
120124128
132
SP+64
$t6SP+64
$a1vehicleID
CMPUT 229 - Computer Organization and Architecture I
52
Quiz #1
predecessors and successors are two vectors of unsigned integers.Assume that:
predecessors’ first element is stored at SP+32successors’ first element is stored at SP+64
Where SP points to the base of the stack frame of the current procedure.
Write a sequence of assembly instructions that executes the followingC statement:
successors[5] = predecessors[7];
CMPUT 229 - Computer Organization and Architecture I
53
Quiz #1 - Solutionpredecessors and successors are two vectors of unsigned integers.predecessors’ first element is stored at SP+32successors’ first element is stored at SP+64
successors[5] = predecessors[7];
$t0 predecessors[7];successors[5] $t0;
$t1 Addr(predecessor[7]);$t0 0($t1);$t2 Addr(successors[5]);0($t2) $t0;
$t1 SP+32+7*4$t0 0($t1);$t2 SP+64+5*40($t2) $t0;
add $t1, $sp, 60lw $t0, 0($t1)add $t2, $sp, 84sw $t0,0($t2)
$t1 SP+60$t0 0($t1);$t2 SP+840($t2) $t0;
CMPUT 229 - Computer Organization and Architecture I
54
Quiz #2
names is an array of pointers to strings.
Each position of names contains a pointer to a null terminated string.
Assume that the first element of names is stored in the memory position SP+16.
Write a sequence of MIPS assembly instructionsthat counts the number of characters in the string
whose pointer is at names[7].
CMPUT 229 - Computer Organization and Architecture I
55
Quiz #2names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string.Assume that the first element of names is stored in the memory position SP+16.Write a sequence of MIPS assembly instructions that counts the number of characters in the string whose pointer is at names[7].
count = 0;c = names[7];while (*c != 0) { count = count+1; c = c+1; }
$t0 0;$t1 Address(names[7]);$t2 0($t1);while (*($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; }
$t0 0;$t2 names[7];while ( *($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; }
$t0 0;$t1 SP+16+7*4$t2 0($t1);while (0($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; }
CMPUT 229 - Computer Organization and Architecture I
56
Quiz #2names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string.Assume that the first element of names is stored in the memory position SP+16.Write a sequence of MIPS assembly instructions that counts the number of characters in the string whose pointer is at names[7].
$t0 0;$t1 SP+16+7*4$t2 0($t1);while (0($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; }
add $t0, $zero, $zeroadd $t1, $sp, 44lw $t2, 0($t1)lb $t3,0($t2)beq $t3, $zero, doneaddi $t0, $t0, 1addi $t2, $t2, 1j loop;
loop:
CMPUT 229 - Computer Organization and Architecture I
57
Quiz #3
Consider the following C function:
Write the MIPS assembly code for the first statement of this function.Assume: temp $s0 node $s1
int ReadGraph(FILE input_file, unisgned int successors, char ***names){ unsigned int temp = 0; unsigned int node = 0;
(*successors)[node] = temp; printf(“name = %d\n”,*names[node]);}
CMPUT 229 - Computer Organization and Architecture I
58
Quiz #3 - Solution
int ReadGraph(FILE input_file, unisgned int successors, char ***names){ unsigned int temp = 0; unsigned int node;
(*successors)[node] = temp; printf(“name = %d\n”,*names[node]);}
(*successors)[node] temp;
0($a1)[node] temp;
$t0 0($a1)$t1 Address($t0[node]) 0($t1) temp;
$t0 0($a1)$t0[node] temp;
$t0 0($a1)$t1 $t0 + 4*node0($t1) temp;
lw $t0, 0($a1)sll $t2, $s1, 2add $t1, $t0, $t2sw $s0,0($t1)
CMPUT 229 - Computer Organization and Architecture I
59
Quiz #4
Consider the following C function:
Write the MIPS assembly code to initialize the value of $a1 inthe function call to printf.
int ReadGraph(FILE input_file, unisgned int successors, char ***names){ unsigned int temp; unsigned int node;
(*successors)[node] = temp; printf(“name = %s\n”,(*names)[node]);}
CMPUT 229 - Computer Organization and Architecture I
60
How did we ended up with names ?
$a0A d a m \0
J a m e \0s
C e c i \0l y
M u r r \0a y
CMPUT 229 - Computer Organization and Architecture I
61
int ReadGraph(FILE input_file, unisgned int successors, char ***names){ printf(“”,(*names)[node]);}
$a2
lw $t0, 0($a2) # $t0 names
$t0
A d a m \0
J a m e \0s
C e c i \0l y
M u r r \0a y
CMPUT 229 - Computer Organization and Architecture I
62
int ReadGraph(FILE input_file, unisgned int successors, char ***names){ printf(“”,(*names)[node]);}
lw $t0, 0($a2) # $t0 names
$a2
$t3
lw $t0, 0($a2) # $t0 nameslw $t2, 8(sp) # $t2 nodesll $t2, $t2, 2 # $t2 4nodeadd $t3, $t0, $t2 # $t3 (names) + 4node
$t0
A d a m \0
J a m e \0s
C e c i \0l y
M u r r \0a y
CMPUT 229 - Computer Organization and Architecture I
63
int ReadGraph(FILE input_file, unisgned int successors, char ***names){ printf(“”,(*names)[node]);}
A d a m \0
J a m e \0s
C e c i \0l y
$a2
lw $t0, 0($a2) # $t0 nameslw $t2, 8(sp) # $t2 nodesll $t2, $t2, 2 # $t2 4nodeadd $t3, $t0, $t2 # $t3 (names) + 4nodelw $a1, 0(t3) # $a1 (names)[node]
$t3$a1
$t0M u r r \0a y
CMPUT 229 - Computer Organization and Architecture I
64
int ReadGraph(FILE input_file, unisgned int successors, char ***names){ unsigned int temp; unsigned int node;
(successors)[node] = temp; printf(“name = %s\n”,(names)[node]);}
lw $t0, 0($a2) # $t0 nameslw $t2, 8(sp) # $t2 nodesll $t2, $t2, 2 # $t2 4nodeadd $t3, $t1, $t2 # $t3 (names) + 4nodelw $a1, 0(t3) # $a1 (names)[node]
CMPUT 229 - Computer Organization and Architecture I
65
Quiz #5
Write the code for the following C function:
/* NumOfStores counts the number of store instructions that appear between the instruction pointed to by FirstInstruction and the instruction pointed to by LastInstruction */
int NumberOfStores(unsigned int FirstInstruction, unsigned int LastInstruction){}
0x23 13 13 56OpCode rs rt address
sw $13, 56($13)
For this question, we will consider that the only type of store operationavailable in the processor is a store word, such as:
100011 01101 01101 0000 0000 0011 100031 26 15 020 1625 21
CMPUT 229 - Computer Organization and Architecture I
66
Quiz #5
/* NumOfStores counts the number of store instructions that appear between the instruction pointed to by FirstInstruction and the instruction pointed to by LastInstruction */
int NumberOfStores(unsigned int FirstInstruction, unsigned int LastInstruction){ unsigned int *instruction; int count = 0; if(LastInstruction < FirstInstruction) return 0; for(instruction = FirstInstruction ; instruction <= LastInstruction ; instruction++) if((*instruction & 0xfc000000) == 0x8c000000) count++; return count;}
0x23 13 13 56OpCode rs rt address
sw $13, 56($13)
100011 01101 01101 0000 0000 0011 100031 26 15 020 1625 21
CMPUT 229 - Computer Organization and Architecture I
67
Quiz #6
For this quiz, assume that the only instructions that can effectcontrol flow program transfers are:
bne $s3, $s4, 8 PC PC + 4 if($1 $2) PC PC + 8
000101 10011 10100 0000 0000 0000 100031 26 15 020 1625 21
5 19 20 8OpCode rs rt address
j Exit PC concat(PC[31-28],IR[25-0])<<2
000010 00 0000 0000 0000 0000 0000 010031 26 25 0
2 4OpCode address
CMPUT 229 - Computer Organization and Architecture I
68
Quiz #6Assume that the following C functions are available for your use in thisquiz:
/* The following functions can manipulate a bit vector of arbitrary length */unsigned int *CreateBitvector(unsigned int VectorLenght);void SetBit(unsigned int *BitVector, unsigned int BitNumber);void ResetBitRange(unsigned int *BitVector, unsigned int FirstBit, unsigned int LastBit);void ResetBit(unsigned int *BitVector, unsigned int BitNumber);
/* BBLeaders returns a bit vector where the bit corresponding to instruction i is 1 if and only if i is a basic block leader. */
unsigned int *BBLeaders(unsigned int *FirstInstruction, unsigned int *LastInstruction){}
Write the BBLeaders function:
CMPUT 229 - Computer Organization and Architecture I
69
Quiz #6
/* BBLeaders returns a bit vector where the bit corresponding to instruction i is 1 if and only if i is a basic block leader. */void *BBLeaders(void *FirstInstruction, void *LastInstruction){ unsigned int *instruction; unsigned int *bitvector; int bitcount = 0; unsigned int opcode; unsigned int num_instructions;
if(LastInstruction <= FirstInstruction) return 0; num_instructions = LastInstruction - FirstInstruction +1; bitvector = CreateBitVector(num_instructions); ResetBitRange(bitvector,0,num_instructions-1); SetBit(bitvector,0); /* first instruction is a leader */ for(instruction = (unsigned int *) FirstInstruction ; instruction <= LastInstruction ; instruction++) { opcode = *instruction & 0xfc000000 if(opcode == 0x14000000)
{ /* process the branch instruction */ }
if(opcode == 0x08000000){ /* process jump */}
bitcount++; } return ((void *) bitvector);}
CMPUT 229 - Computer Organization and Architecture I
70
Quiz #7Write MIPS assembly for thefollowing program.Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
ind
Stackappleptr
$sp 0
4
8
CMPUT 229 - Computer Organization and Architecture I
71
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+4
Stackappleptr
$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
72
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+4
Stackappleptr
$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
73
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+4
Stackapple$sp+8
$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
74
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+4
Stackapple$sp+8
$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
75
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+4
Stack123
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
76
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+4
Stack123
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
77
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+8
Stack123
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
78
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+8
Stack123
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
79
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+8
Stack124
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
80
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+8
Stack124
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
81
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+8
Stack125
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
CMPUT 229 - Computer Organization and Architecture I
82
Quiz #7# 7 ind = &ptr;addi $t0, $sp, 4 # $t0 &ptr sw $t0, 0($sp) # ind &ptr# 8 *ind = &apple;lw $t0, 0($sp)addi $t1, $sp,8 # $t1 &applesw $t1, 0($t0) # *ind &apple# 9 **ind = 123;addi $t3,$zero,123lw $t4, 0($sp) # $t4 *indlw $t5, 0($t4) # $t5 **indsw $t3, 0($t5) # **ind 123# 10 ind++;lw $t0, 0($sp)addi $t0, $t0, 4sw $t0, 0($sp) # ind++# 11 *ptr++;lw $t6, 4($sp) # $t6 ptrlw $t7, 0($t6) # $t7 *ptraddi $t7, $t7, 1sw $t7, 0($t6) # *ptr++# 12 apple++;lw $t8, 8(sp) # $t8 appleaddi $t8, $t8, 1sw $t8, 8(sp) # apple++
1 #include <stdio.h> 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind;
7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123;10 ind++;11 *ptr++;12 apple++;
13 printf(“%x %x %d\n”, ind, ptr, apple);14 }
$sp+8
Stack125
$sp+8$sp 0
4
8
What are the values in the stackafter line 12 is executed?Assume that:
apple $sp+8 ptr $sp+4ind $sp+0
top related