Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral.

Similar presentations


Presentation on theme: "CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral."— Presentation transcript:

1 CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral

2 CMPUT 229 - Computer Organization and Architecture I2 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.

3 CMPUT 229 - Computer Organization and Architecture I3 Example: A swap function 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } Patt and Patel, pp. 366

4 CMPUT 229 - Computer Organization and Architecture I4 Assembly for Swap Generated with -O0 (part 1) 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } # 5 main() # 6 { addiu $sp,$sp,-32 #.frame.len.main sd $gp,16($sp) #.lcl_spill_b002 sd $ra,8($sp) #.lcl_spill_b001 lui $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) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop $a1 Stack $a2 $sp $a0 0 4 3 4 3 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

5 CMPUT 229 - Computer Organization and Architecture I5 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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) # valueA lw $a1,4($sp) # valueB lw $t9,%call16(Swap)($gp) jalr $t9 # Swap nop.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) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop.BB4.main: # 0x74 # 13 } or $v0,$zero,$zero ld $gp,16($sp) #.lcl_spill_b002 ld $ra,8($sp) #.lcl_spill_b001 addiu $sp,$sp,32 #.frame.len.main jr $ra nop 4 $a1 Stack 3 $a2 4 3 $sp $a0 3 0 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

6 CMPUT 229 - Computer Organization and Architecture I6 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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.Swap sw $a0,20($sp) # firstVal sw $a1,28($sp) # secondVal # 17 int tempVal; # 18 # 19 tempVal = firstVal; lw $v1,20($sp) # firstVal sw $v1,0($sp) # tempVal # 20 firstVal = secondVal; lw $v0,28($sp) # secondVal sw $v0,20($sp) # firstVal # 21 secondVal = tempVal; lw $at,0($sp) # tempVal sw $at,28($sp) # secondVal # 22 } addiu $sp,$sp,32 #.frame.len.Swap jr $ra nop.endSwap 4 $a1 3 $a2 3 $a0 Stack 4 3 $sp 0 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

7 CMPUT 229 - Computer Organization and Architecture I7 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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.Swap sw $a0,20($sp) # firstVal sw $a1,28($sp) # secondVal # 17 int tempVal; # 18 # 19 tempVal = firstVal; lw $v1,20($sp) # firstVal sw $v1,0($sp) # tempVal # 20 firstVal = secondVal; lw $v0,28($sp) # secondVal sw $v0,20($sp) # firstVal # 21 secondVal = tempVal; lw $at,0($sp) # tempVal sw $at,28($sp) # secondVal # 22 } addiu $sp,$sp,32 #.frame.len.Swap jr $ra nop.endSwap 4 $a1 3 $a2 3 $a0 Stack 4 3 $sp 3 4 $v1 $v0 3 3 4 4 $at 3 3 0 4 8 12 16 20 24 28 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

8 CMPUT 229 - Computer Organization and Architecture I8 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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.Swap sw $a0,20($sp) # firstVal sw $a1,28($sp) # secondVal # 17 int tempVal; # 18 # 19 tempVal = firstVal; lw $v1,20($sp) # firstVal sw $v1,0($sp) # tempVal # 20 firstVal = secondVal; lw $v0,28($sp) # secondVal sw $v0,20($sp) # firstVal # 21 secondVal = tempVal; lw $at,0($sp) # tempVal sw $at,28($sp) # secondVal # 22 } addiu $sp,$sp,32 #.frame.len.Swap jr $ra nop.endSwap 4 $a1 3 $a2 3 $a0 4 3 3 Stack 4 3 $sp 3 $v1 4 $v0 $at 3 0 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

9 CMPUT 229 - Computer Organization and Architecture I9 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 }.BB2.main: # 0x44 # 11 Swap(valueA, valueB); lw $a0,0($sp) # valueA lw $a1,4($sp) # valueB lw $t9,%call16(Swap)($gp) jalr $t9 # Swap nop.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) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop.BB4.main: # 0x74 # 13 } or $v0,$zero,$zero ld $gp,16($sp) #.lcl_spill_b002 ld $ra,8($sp) #.lcl_spill_b001 addiu $sp,$sp,32 #.frame.len.main jr $ra nop Assembly for Swap Generated with -O0 (part 3) 4 $a1 3 $a2 3 $a0 4 3 3 Stack 4 3 $sp 3 $v1 4 $v0 $at 3 0 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

10 CMPUT 229 - Computer Organization and Architecture I10 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 }.BB2.main: # 0x44 # 11 Swap(valueA, valueB); lw $a0,0($sp) # valueA lw $a1,4($sp) # valueB lw $t9,%call16(Swap)($gp) jalr $t9 # Swap nop.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) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop.BB4.main: # 0x74 # 13 } or $v0,$zero,$zero ld $gp,16($sp) #.lcl_spill_b002 ld $ra,8($sp) #.lcl_spill_b001 addiu $sp,$sp,32 #.frame.len.main jr $ra nop Assembly for Swap Generated with -O0 (part 3) 3 $a1 4 $a2 3 $a0 4 3 3 Stack 4 3 $sp 3 $v1 4 $v0 $at 3 0 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 3 and valueB = 4

11 CMPUT 229 - Computer Organization and Architecture I11 Addresses and Values The problem with our swap program is that the main is passing the values of variables valueA and valueB to the swap function. Thus the swap function does all its work within its own frame in the stack and never actually changes the state of the variables in the main function. Could a “smarter” compiler figure out that the swap function is doing nothing? Lets try the MIPSPro compiler with -O3.

12 CMPUT 229 - Computer Organization and Architecture I12 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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.main sd $gp,8($sp) # [1].gra_spill_b002 addiu $a1,$a1,%lo(%neg(%gp_rel(main +0)))# [1] addu $gp,$t9,$a1 # [2] lw $t9,%got_disp(printf)($gp) # [3].loc1 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_b001 addiu $a1,$zero,3 # [5] jalr $t9 # [6] printf addiu $a0,$a0,%got_ofst(.rodata)# [6].BB2.main: # 0x30 # BB:2 frequency = 1.00000 (heuristic) # 11 Swap(valueA, valueB); lw $t9,%call16(Swap)($gp) # [0] addiu $a1,$zero,4 # [2] jalr $t9 # [3] Swap addiu $a0,$zero,3 # [3] $a1 Stack $a2 $sp $a0 0 4 4 3 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

13 CMPUT 229 - Computer Organization and Architecture I13 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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.main sd $gp,8($sp) # [1].gra_spill_b002 addiu $a1,$a1,%lo(%neg(%gp_rel(main +0)))# [1] addu $gp,$t9,$a1 # [2] lw $t9,%got_disp(printf)($gp) # [3].loc1 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_b001 addiu $a1,$zero,3 # [5] jalr $t9 # [6] printf addiu $a0,$a0,%got_ofst(.rodata)# [6].BB2.main: # 0x30 # BB:2 frequency = 1.00000 (heuristic) # 11 Swap(valueA, valueB); lw $t9,%call16(Swap)($gp) # [0] addiu $a1,$zero,4 # [2] jalr $t9 # [3] Swap addiu $a0,$zero,3 # [3] $a1 Stack $a2 $sp $a0 0 4 4 3 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

14 CMPUT 229 - Computer Organization and Architecture I14 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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 # # BB:1 frequency = 1.00000 (heuristic) # # 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$./swapO0 Before Swap: valueA = 3 and valueB = 4

15 CMPUT 229 - Computer Organization and Architecture I15 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } Assembly for Swap Generated with -O3 (part 2).BB3.main: # 0x40 # # BB:3 frequency = 1.00000 (heuristic) # 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] printf addiu $a0,$a0,%got_ofst(.rodata+48)# [3].BB4.main: # 0x58 # BB:4 frequency = 1.00000 (heuristic) # 13 } ld $ra,0($sp) # [0].gra_spill_b001 or $v0,$zero,$zero # [2] ld $gp,8($sp) # [3].gra_spill_b002 jr $ra # [3] addiu $sp,$sp,16 # [3].frame.len.main $a1 Stack $a2 $sp $a0 0 4 3 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 3 and valueB = 4

16 CMPUT 229 - Computer Organization and Architecture I16 What happened at -O3? While compiling the swap code, MIPSPro figured out that it was doing nothing of consequence, and thus replaced the body of the function with a simple return instruction. However during the compilation of main, MIPSPro did not know what swap was up to, and thus could not eliminate the call to swap. This happens because, even at -O3, MIPSPro does not do inter-procedural analysis (IPA), and thus the compilation of each procedure is done in isolation. In order to force MIPSPro do perform IPA, we would have to include the option -ipa in the command line. But then the compiler cannot produce the assembly files because IPA actually takes place during the linking phase of the compiler.

17 CMPUT 229 - Computer Organization and Architecture I17 Example2: A new swap function 1 #include 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 } 13 14 void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 17 18 tempVal = *firstVal; 19 *firstVal = *secondVal; 20 *secondVal = tempVal; 21 } Patt and Patel, pp. 371

18 CMPUT 229 - Computer Organization and Architecture I18 1 #include 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 } 13 14 void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 17 18 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.main sd $gp,16($sp) # [1].gra_spill_b002 addiu $a2,$a2,%lo(%neg(%gp_rel(main +0)))# [1] # 7 int valueA = 3; addiu $a1,$zero,3 # [2] sw $a1,0($sp) # [2] valueA addu $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_b001 addiu $a2,$zero,4 # [5] addiu $a1,$zero,3 # [5] sw $a3,4($sp) # [6] valueB jalr $t9 # [6] printf addiu $a0,$a0,%got_ofst(.rodata)# [6] # BB:2 frequency = 1.00000 (heuristic) # 11 NewSwap(&valueA, &valueB); lw $t9,%call16(NewSwap)($gp) # [0] addiu $a1,$sp,4 # [2] valueB jalr $t9 # [3] NewSwap addiu $a0,$sp,0 # [3] valueA Stack 0 4 $a2 $a3 $a0 $a1 3 3 4 0x7ffff0004 4 0x7ffff0000 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 $sp 4

19 CMPUT 229 - Computer Organization and Architecture I19 1 #include 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 } 13 14 void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 17 18 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) Stack 4 3 0 4 $a2 $a3 4 $a0 0x7ffff0000 $a1 0x7ffff0004 $at $v0 $sp 3 3 4 4 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

20 CMPUT 229 - Computer Organization and Architecture I20 Assembly for NewSwap Generated with -O3 (part 1) 1 #include 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 } 13 14 void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 17 18 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] valueB lw $a1,0($sp) # [3] valueA jalr $t9 # [3] printf addiu $a0,$a0,%got_ofst(.rodata+48) Stack 3 4 0 4 $a2 $a3 4 $a0 0x7ffff0000 $a1 0x7ffff0004 $at 3 $v0 4 $sp 3 4 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 4 and valueB = 3

21 CMPUT 229 - Computer Organization and Architecture I21 The car.c program #include #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

22 CMPUT 229 - Computer Organization and Architecture I22 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 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64

23 CMPUT 229 - Computer Organization and Architecture I23 The car.c program #include #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, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra

24 CMPUT 229 - Computer Organization and Architecture I24 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t9 $t6 $t0 $t8 $t7 vehicleID

25 CMPUT 229 - Computer Organization and Architecture I25 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t9 $t0 $t8 $t7 vehicleID $t6 vehicleID make[0-3]

26 CMPUT 229 - Computer Organization and Architecture I26 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 vehicleID $t6 vehicleID make[0-3] $t9 make[0-3] make[4-7]

27 CMPUT 229 - Computer Organization and Architecture I27 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 $t6 vehicleID make[0-3] $t9 make[0-3] make[4-7] make[8-11]

28 CMPUT 229 - Computer Organization and Architecture I28 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 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]

29 CMPUT 229 - Computer Organization and Architecture I29 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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] 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 make[8-11] vehicleID make[12-15] $t9 make[12-15] make[16-19] $t6

30 CMPUT 229 - Computer Organization and Architecture I30 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 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

31 CMPUT 229 - Computer Organization and Architecture I31 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 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

32 CMPUT 229 - Computer Organization and Architecture I32 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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] 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 model[0-3] vehicleID model[4-7] $t9 model[4-7] model[8-11] $t6

33 CMPUT 229 - Computer Organization and Architecture I33 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 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

34 CMPUT 229 - Computer Organization and Architecture I34 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next 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 112 116 120 124 128 132 136 140 144 148 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

35 CMPUT 229 - Computer Organization and Architecture I35 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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] 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 model[12-15] vehicleID model[16-19] $t9 model[16-19] year $t6

36 CMPUT 229 - Computer Organization and Architecture I36 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 vehicleID model[16-19] make[0-3] year make[4-7] mileage $t6 $t9

37 CMPUT 229 - Computer Organization and Architecture I37 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 vehicleID make[16-19] make[0-3] make[4-7] mileage $t6 mileage $t9

38 CMPUT 229 - Computer Organization and Architecture I38 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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] 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 model[12-15] vehicleID $t9 cost1 $t6

39 CMPUT 229 - Computer Organization and Architecture I39 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 vehicleID make[0-3] make[4-7] $t8 $t7 $t9 cost1 $t6 cost2

40 CMPUT 229 - Computer Organization and Architecture I40 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 vehicleID make[0-3] make[4-7] $t6 cost2 $t8 $t7 $t9 cost2 next

41 CMPUT 229 - Computer Organization and Architecture I41 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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] 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 cost2 vehicleID next $t9 $t6

42 CMPUT 229 - Computer Organization and Architecture I42 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$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] 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 $t0 $t8 $t7 cost2] vehicleID next $t9 next $t6 $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 make[8-11]

43 CMPUT 229 - Computer Organization and Architecture I43 The car.c program # 42void PrintCar(CarNode car) # 43{ PrintCar: subu$sp, 32 sw$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, string13 lw$a1, 32($sp) jalprintf # 45 printf("make: %s\n",car.make); la$a0, string14 addu$a1, $sp, 32 addu$a1, $a1, 4 jalprintf # 46 printf("model: %s\n",car.model); la$a0, string15 addu$a1, $sp, 32 addu$a1, $a1, 24 jalprintf # 47 printf("year: %d\n",car.year); la$a0, string16 lw$a1, 76($sp) jalprintf 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] 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 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 vehicleID next $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 make[8-11]

44 CMPUT 229 - Computer Organization and Architecture I44 The car.c program # 42void PrintCar(CarNode car) # 43{ PrintCar: subu$sp, 32 sw$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, string13 lw$a1, 32($sp) jalprintf # 45 printf("make: %s\n",car.make); la$a0, string14 addu$a1, $sp, 32 addu$a1, $a1, 4 jalprintf # 46 printf("model: %s\n",car.model); la$a0, string15 addu$a1, $sp, 32 addu$a1, $a1, 24 jalprintf # 47 printf("year: %d\n",car.year); la$a0, string16 lw$a1, 76($sp) jalprintf $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] vehicleID 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 vehicleID next $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 make[8-11] SP00 04 08 12 16 20 24 28 32 96 100 104 108 112 116 120 124

45 CMPUT 229 - Computer Organization and Architecture I45 Why so much copying? The program car.c passes the datastructure CarNode to the PrintCar function by value. A copy of each byte of CarNode must be made in the stack for each call of the function PrintCar. We could, instead have passed the address of the copy of CarNode that we already had in the stack.

46 CMPUT 229 - Computer Organization and Architecture I46 The car2.c Program #include #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); }

47 CMPUT 229 - Computer Organization and Architecture I47 car-O0.s car2-O0.s #include #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, 112 sw$ra, 28($sp) # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 32 jalReadCar # 27 PrintCar(&mycar); addu$a0, $sp, 32 jalPrintCar # 28} move$v0, $zero lw$ra, 28($sp) addu$sp, 112 j$ra

48 CMPUT 229 - Computer Organization and Architecture I48 car2-O0.s main: subu$sp, 112 sw$ra, 28($sp) # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 32 jalReadCar # 27 PrintCar(&mycar); addu$a0, $sp, 32 jalPrintCar # 28} move$v0, $zero lw$ra, 28($sp) addu$sp, 112 j$ra next 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

49 CMPUT 229 - Computer Organization and Architecture I49 car2-O0.s PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) # 43 printf("vehicleID: %d\n",car->vehicleID); la$a0, $$13 lw$t6, 32($sp) lw$a1, 0($t6) jalprintf # 44 printf("make: %s\n",car->make); la$a0, $$14 lw$a1, 32($sp) addu$a1, $a1, 4 jalprintf # 45 printf("model: %s\n",car->model); la$a0, $$15 lw$a1, 32($sp) addu$a1, $a1, 24 jalprintf # 46 printf("year: %d\n",car->year); la$a0, $$16 lw$t7, 32($sp) lw$a1, 44($t7) jalprintf # 47 printf("mileage: %d\n",car->mileage); la$a0, $$17 lw$t8, 32($sp) lw$a1, 48($t8) jalprintf next 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

50 CMPUT 229 - Computer Organization and Architecture I50 car2-O0.s PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) # 43 printf("vehicleID: %d\n",car->vehicleID); la$a0, string13 lw$t6, 32($sp) lw$a1, 0($t6) jalprintf # 44 printf("make: %s\n",car->make); la$a0, string14 lw$a1, 32($sp) addu$a1, $a1, 4 jalprintf # 45 printf("model: %s\n",car->model); la$a0, string15 lw$a1, 32($sp) addu$a1, $a1, 24 jalprintf # 46 printf("year: %d\n",car->year); la$a0, string16 lw$t7, 32($sp) lw$a1, 44($t7) jalprintf # 47 printf("mileage: %d\n",car->mileage); la$a0, string17 lw$t8, 32($sp) lw$a1, 48($t8) jalprintf next 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 00 04 08 12 16 20 24 28 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 32 96 100 104 108 112 116 120 124 128 132 SP+64

51 CMPUT 229 - Computer Organization and Architecture I51 car2-O0.s PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) # 43 printf("vehicleID: %d\n",car->vehicleID); la$a0, string13 lw$t6, 32($sp) lw$a1, 0($t6) jalprintf # 44 printf("make: %s\n",car->make); la$a0, string14 lw$a1, 32($sp) addu$a1, $a1, 4 jalprintf # 45 printf("model: %s\n",car->model); la$a0, string15 lw$a1, 32($sp) addu$a1, $a1, 24 jalprintf # 46 printf("year: %d\n",car->year); la$a0, string16 lw$t7, 32($sp) lw$a1, 44($t7) jalprintf # 47 printf("mileage: %d\n",car->mileage); la$a0, string17 lw$t8, 32($sp) lw$a1, 48($t8) jalprintf next 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 00 04 08 12 16 20 24 28 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 32 96 100 104 108 112 116 120 124 128 132 SP+64 $t6 SP+64 $a1 vehicleID

52 CMPUT 229 - Computer Organization and Architecture I52 Quiz #1 predecessors and successors are two vectors of unsigned integers. Assume that: predecessors’ first element is stored at SP+32 successors’ 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 following C statement: successors[5] = predecessors[7];

53 CMPUT 229 - Computer Organization and Architecture I53 Quiz #1 - Solution predecessors and successors are two vectors of unsigned integers. predecessors’ first element is stored at SP+32 successors’ 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*4 0($t2)  $t0; add$t1, $sp, 60 lw$t0, 0($t1) add$t2, $sp, 84 sw$t0,0($t2) $t1  SP+60 $t0  0($t1); $t2  SP+84 0($t2)  $t0;

54 CMPUT 229 - Computer Organization and Architecture I54 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 instructions that counts the number of characters in the string whose pointer is at names[7].

55 CMPUT 229 - Computer Organization and Architecture I55 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 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; }

56 CMPUT 229 - Computer Organization and Architecture I56 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 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, $zero add$t1, $sp, 44 lw$t2, 0($t1) lb$t3,0($t2) beq$t3, $zero, done addi$t0, $t0, 1 addi$t2, $t2, 1 jloop; loop:

57 CMPUT 229 - Computer Organization and Architecture I57 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]); }

58 CMPUT 229 - Computer Organization and Architecture I58 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*node 0($t1)  temp; lw$t0, 0($a1) sll$t2, $s1, 2 add$t1, $t0, $t2 sw$s0,0($t1)

59 CMPUT 229 - Computer Organization and Architecture I59 Quiz #4 Consider the following C function: Write the MIPS assembly code to initialize the value of $a1 in the 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]); }

60 CMPUT 229 - Computer Organization and Architecture I60 How did we ended up with  names ? $a0 Adam\0Jame sCeci ly Murr a y

61 CMPUT 229 - Computer Organization and Architecture I61 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) {  printf(“  ”,(*names)[node]); } $a2 lw$t0, 0($a2)# $t0   names $t0 Adam\0Jame sCeci ly Murr a y

62 CMPUT 229 - Computer Organization and Architecture I62 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   names lw$t2, 8(sp)# $t2  node sll$t2, $t2, 2# $t2  4  node add$t3, $t0, $t2# $t3  (  names) + 4  node $t0 Adam\0Jame sCeci ly Murr a y

63 CMPUT 229 - Computer Organization and Architecture I63 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) {  printf(“  ”,(*names)[node]); } Adam\0Jame sCeci ly $a2 lw$t0, 0($a2)# $t0   names lw$t2, 8(sp)# $t2  node sll$t2, $t2, 2# $t2  4  node add$t3, $t0, $t2# $t3  (  names) + 4  node lw$a1, 0(t3)# $a1  (  names)[node] $t3 $a1 $t0 Murr\0a y

64 CMPUT 229 - Computer Organization and Architecture I64 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   names lw$t2, 8(sp)# $t2  node sll$t2, $t2, 2# $t2  4  node add$t3, $t1, $t2# $t3  (  names) + 4  node lw$a1, 0(t3)# $a1  (  names)[node]

65 CMPUT 229 - Computer Organization and Architecture I65 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) { } 0x2313 56 OpCodersrtaddress sw $13, 56($13) For this question, we will consider that the only type of store operation available in the processor is a store word, such as: 10001101101 0000 0000 0011 1000 31 2615 020 1625 21

66 CMPUT 229 - Computer Organization and Architecture I66 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; } 0x2313 56 OpCodersrtaddress sw $13, 56($13) 10001101101 0000 0000 0011 1000 31 2615 020 1625 21

67 CMPUT 229 - Computer Organization and Architecture I67 Quiz #6 For this quiz, assume that the only instructions that can effect control flow program transfers are: bne $s3, $s4, 8  PC  PC + 4 if($1  $2) PC  PC + 8 00010110011101000000 0000 0000 1000 31 2615 020 1625 21 519208 OpCodersrtaddress j Exit  PC  concat(PC[31-28],IR[25-0])<<2 00001000 0000 0000 0000 0000 0000 0100 31 2625 0 24 OpCodeaddress

68 CMPUT 229 - Computer Organization and Architecture I68 Quiz #6 Assume that the following C functions are available for your use in this quiz: /* 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:

69 CMPUT 229 - Computer Organization and Architecture I69 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); }

70 CMPUT 229 - Computer Organization and Architecture I70 Quiz #7 Write MIPS assembly for the following program. Assume that: apple  $sp+8 ptr  $sp+4 ind  $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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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  Stack  apple   ptr  $sp 0 4 8

71 CMPUT 229 - Computer Organization and Architecture I71 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack  apple   ptr  $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

72 CMPUT 229 - Computer Organization and Architecture I72 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack  apple   ptr  $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

73 CMPUT 229 - Computer Organization and Architecture I73 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack  apple  $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

74 CMPUT 229 - Computer Organization and Architecture I74 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack  apple  $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

75 CMPUT 229 - Computer Organization and Architecture I75 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 123 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

76 CMPUT 229 - Computer Organization and Architecture I76 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 123 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

77 CMPUT 229 - Computer Organization and Architecture I77 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 123 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

78 CMPUT 229 - Computer Organization and Architecture I78 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 123 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

79 CMPUT 229 - Computer Organization and Architecture I79 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 124 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

80 CMPUT 229 - Computer Organization and Architecture I80 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 124 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

81 CMPUT 229 - Computer Organization and Architecture I81 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 125 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

82 CMPUT 229 - Computer Organization and Architecture I82 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  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 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 Stack 125 $sp+8 $sp 0 4 8 What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0


Download ppt "CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral."

Similar presentations


Ads by Google