1 / 82

CMPUT229 - Fall 2003

CMPUT229 - Fall 2003. TopicC: Pointers and Arrays José Nelson Amaral. 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.

brooke
Télécharger la présentation

CMPUT229 - Fall 2003

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 voidSwap(intfirstVal, intsecondVal); 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(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } CMPUT 229 - Computer Organization and Architecture I Patt and Patel, pp. 366

  4. Stack 4 $a0 $sp 0 $a1 $a2 Assembly for Swap Generated with -O0 (part 1) 4 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 3 4 3 # 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 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } CMPUT 229 - Computer Organization and Architecture I

  5. Stack 4 $a0 $sp 3 $a1 4 $a2 3 Assembly for Swap Generated with -O0 (part 2) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 .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 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } 4 3 0 CMPUT 229 - Computer Organization and Architecture I

  6. Stack 4 $a0 3 $sp 3 $a1 4 $a2 3 Assembly for Swap Generated with -O0 (part 3) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 # 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 .end Swap 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } 4 0 CMPUT 229 - Computer Organization and Architecture I

  7. $a0 3 $sp $a1 4 $a2 3 Assembly for Swap Generated with -O0 (part 3) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 # 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 .end Swap 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } Stack 4 3 28 4 3 24 20 3 4 $v1 3 16 $v0 4 12 8 $at 3 4 0 3 CMPUT 229 - Computer Organization and Architecture I

  8. $a0 3 $sp $a1 4 $a2 3 Assembly for Swap Generated with -O0 (part 3) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 # 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 .end Swap 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } Stack 4 4 0 3 3 4 $v1 3 $v0 4 $at 3 3 CMPUT 229 - Computer Organization and Architecture I

  9. $a0 3 $sp $a1 4 $a2 3 Assembly for Swap Generated with -O0 (part 3) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 .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 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } Stack 4 4 0 3 3 4 $v1 3 $v0 4 $at 3 3 CMPUT 229 - Computer Organization and Architecture I

  10. $sp Assembly for Swap Generated with -O0 (part 3) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 3 and valueB = 4 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 .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 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } Stack 4 4 $a0 3 0 3 $a1 3 3 $a2 4 4 $v1 3 $v0 4 $at 3 3 CMPUT 229 - Computer Organization and Architecture I

  11. 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. CMPUT 229 - Computer Organization and Architecture I

  12. Stack 4 $a0 $sp 0 $a1 $a2 Assembly for Swap Generated with -O3 (part 1) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 3 4 # 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] .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_b001 addiu $a1,$zero,3 # [5] jalr $t9 # [6] printf addiu $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] Swap addiu $a0,$zero,3 # [3] 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } CMPUT 229 - Computer Organization and Architecture I

  13. Stack 4 $a0 $sp 0 $a1 $a2 Assembly for Swap Generated with -O3 (part 1) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 4 3 # 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] .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_b001 addiu $a1,$zero,3 # [5] jalr $t9 # [6] printf addiu $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] Swap addiu $a0,$zero,3 # [3] 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } CMPUT 229 - Computer Organization and Architecture I

  14. Stack 4 $a0 $sp 0 $a1 4 $a2 3 Assembly for Swap Generated with -O3 (part 3) bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 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] 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } CMPUT 229 - Computer Organization and Architecture I

  15. Stack 4 $a0 $sp 0 $a1 3 $a2 4 Assembly for Swap Generated with -O3 (part 2) 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 .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] printf addiu $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_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 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } CMPUT 229 - Computer Organization and Architecture I

  16. 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. CMPUT 229 - Computer Organization and Architecture I

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

  18. Stack 4 $a0 $sp 0 $a1 $a2 $a3 Assembly for NewSwap Generated with -O3 (part 1) 4 0x7ffff0000 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 3 3 0x7ffff0004 # 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] #<freq> 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 4 4 1 #include <stdio.h> 2 voidNewSwap(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 voidNewSwap(int *firstVal, int *secondVal) 15 { 16 inttempVal; 17 18 tempVal = *firstVal; 19 *firstVal = *secondVal; 20 *secondVal = tempVal; 21 } CMPUT 229 - Computer Organization and Architecture I

  19. Stack 4 4 $a0 0x7ffff0000 $sp 0 3 $a1 0x7ffff0004 $a2 $a3 $v0 4 $at Assembly for NewSwap Generated with -O3 (part 1) 3 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4 1 #include <stdio.h> 2 voidNewSwap(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 voidNewSwap(int *firstVal, int *secondVal) 15 { 16 inttempVal; 17 18 tempVal = *firstVal; 19 *firstVal = *secondVal; 20 *secondVal = tempVal; 21 } 3 4 # 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) CMPUT 229 - Computer Organization and Architecture I

  20. Stack 4 3 $a0 0x7ffff0000 $sp 0 4 $a1 0x7ffff0004 $a2 $a3 4 Assembly for NewSwap Generated with -O3 (part 1) bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 4 and valueB = 3 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4 3 1 #include <stdio.h> 2 voidSwap(intfirstVal, intsecondVal); 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(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } $at 3 $v0 4 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) CMPUT 229 - Computer Organization and Architecture I

  21. The car.c program #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node *next; } CarNode; voidReadCar(CarNode *car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); } voidReadCar(CarNode *car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } voidPrintCar(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 Patt and Patel, pp. 419

  22. model[20] next make[20] year mileage vehicleID cost The car.c program 64 60 56 #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node next; } CarNode; 52 48 44 40 36 32 28 24 20 16 12 8 4 0 CMPUT 229 - Computer Organization and Architecture I

  23. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra The car.c program #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node next; } CarNode; voidReadCar(CarNode car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); } CMPUT 229 - Computer Organization and Architecture I

  24. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 vehicleID 164 68 $t7 160 64 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] 16 112 model[0-3] 12 108 make[16-19] 08 104 make[12-15] 100 04 make[8-11] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7]

  25. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 vehicleID 164 68 $t7 160 64 make[0-3] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] 16 112 model[0-3] 12 108 make[16-19] 08 104 make[12-15] 100 04 make[8-11] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  26. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 vehicleID make[4-7] 164 68 $t7 160 64 make[0-3] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] 16 112 model[0-3] 12 108 make[16-19] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  27. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 make[4-7] make[8-11] 164 68 $t7 160 64 make[0-3] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] 16 112 model[0-3] 12 108 make[16-19] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  28. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 make[8-11] 164 68 $t7 160 64 make[0-3] make[12-15] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] 16 112 model[0-3] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  29. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 make[16-19] make[8-11] 164 68 $t7 160 64 make[12-15] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  30. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 model[0-3] make[16-19] 164 68 $t7 160 64 make[12-15] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  31. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 model[0-3] 164 68 $t7 160 64 model[4-7] make[12-15] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  32. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 model[8-11] model[0-3] 164 68 $t7 160 64 model[4-7] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  33. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 model[12-15] model[8-11] 164 68 $t7 160 64 model[4-7] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  34. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 model[12-15] 164 68 $t7 160 64 model[16-19] make[12-15] 60 156 56 152 next 52 148 cost 144 48 cost 140 44 40 136 mileage 36 132 year model[12-15] 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  35. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 year model[12-15] 164 68 $t7 160 64 model[16-19] 60 156 56 152 next 52 148 cost2 144 48 cost1 140 44 year 40 136 mileage model[16-19] 36 132 year model[12-15] 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  36. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 mileage year 164 68 $t7 160 64 model[16-19] 60 156 56 152 next 52 148 cost2 144 48 cost1 140 44 year 40 136 mileage model[16-19] 36 132 year model[12-15] 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  37. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 $t8 168 72 mileage 164 68 $t7 160 64 make[16-19] 60 156 56 152 next 52 148 cost2 144 48 cost1 mileage 140 44 year 40 136 mileage model[16-19] 36 132 year model[12-15] 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  38. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 168 72 164 68 160 64 60 156 56 152 next cost1 52 148 cost2 144 48 cost1 mileage 140 44 year 40 136 $t8 mileage model[16-19] 36 132 year model[12-15] model[12-15] cost1 32 128 model[16-19] model[8-11] $t7 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  39. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t9 $t0 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 168 72 164 68 160 64 60 156 56 152 next cost1 52 148 cost2 144 48 cost1 mileage 140 44 year 40 136 $t8 mileage model[16-19] 36 132 year model[12-15] cost1 cost2 32 128 model[16-19] model[8-11] $t7 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  40. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 168 72 164 68 160 64 60 156 cost2 56 152 next cost1 52 148 cost2 144 48 cost1 mileage 140 44 year 40 136 $t8 mileage model[16-19] 36 132 year model[12-15] cost2 32 128 model[16-19] model[8-11] $t7 124 28 model[12-15] model[4-7] next 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  41. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $t0 $t9 The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 168 72 164 68 160 64 next 60 156 cost2 56 152 next cost1 52 148 cost2 144 48 cost1 mileage 140 44 year 40 136 $t8 mileage model[16-19] 36 132 year model[12-15] cost2 32 128 model[16-19] model[8-11] $t7 124 28 model[12-15] model[4-7] next 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  42. main: subu $sp, 168 sw $ra, 84($sp) .frame $sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 88 jal ReadCar # 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) jal PrintCar # 28 } move $v0, $zero lw $ra, 84($sp) addu $sp, 168 j $ra $t6 $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 $t0 $t9 make[8-11] The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 168 72 164 68 160 64 $t8 next 60 156 cost2 cost2] 56 152 next cost1 $t7 52 148 cost2 next 144 48 cost1 mileage 140 44 year 40 136 mileage model[16-19] 36 132 year model[12-15] 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

  43. # 42 void 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) jal printf # 45 printf("make: %s\n",car.make); la $a0, string14 addu $a1, $sp, 32 addu $a1, $a1, 4 jal printf # 46 printf("model: %s\n",car.model); la $a0, string15 addu $a1, $sp, 32 addu $a1, $a1, 24 jal printf # 47 printf("year: %d\n",car.year); la $a0, string16 lw $a1, 76($sp) jal printf $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 make[8-11] The car.c program 188 92 make[0-3] 184 88 vehicleID 180 84 80 176 172 76 168 72 164 68 160 64 next 60 156 cost2 56 152 next cost1 52 148 cost2 144 48 cost1 mileage 140 44 year 40 136 mileage model[16-19] 36 132 year model[12-15] 32 128 model[16-19] model[8-11] 124 28 model[12-15] model[4-7] 120 24 model[8-11] model[0-3] 20 116 model[4-7] make[16-19] 16 112 model[0-3] make[12-15] 12 108 make[16-19] make[8-11] 08 104 make[12-15] make[4-7] 100 04 make[8-11] make[0-3] 00 SP 96 CMPUT 229 - Computer Organization and Architecture I make[4-7] vehicleID

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

  45. 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. CMPUT 229 - Computer Organization and Architecture I

  46. The car2.c Program #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node next; } CarNode; voidReadCar(CarNode car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); } voidReadCar(CarNode car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } voidPrintCar(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. car2-O0.s car-O0.s main: subu $sp, 112 sw $ra, 28($sp) # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 32 jal ReadCar # 27 PrintCar(&mycar); addu $a0, $sp, 32 jal PrintCar # 28 } move $v0, $zero lw $ra, 28($sp) addu $sp, 112 j $ra #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node next; } CarNode; voidReadCar(CarNode car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); } CMPUT 229 - Computer Organization and Architecture I

  48. car2-O0.s main: subu $sp, 112 sw $ra, 28($sp) # 23 CarNode mycar; # 25 ReadCar(&mycar); addu $a0, $sp, 32 jal ReadCar # 27 PrintCar(&mycar); addu $a0, $sp, 32 jal PrintCar # 28 } move $v0, $zero lw $ra, 28($sp) addu $sp, 112 j $ra $a0 100 96 next 92 cost 88 cost 84 80 mileage 76 year 72 model[16-19] 68 model[12-15] 64 model[8-11] 60 model[4-7] 56 model[0-3] 52 make[16-19] 48 make[12-15] 44 make[8-11] 40 make[4-7] 36 make[0-3] 32 vehicleID 28 24 20 16 12 08 04 00 SP CMPUT 229 - Computer Organization and Architecture I

  49. 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) jal printf # 44 printf("make: %s\n",car->make); la $a0, $$14 lw $a1, 32($sp) addu $a1, $a1, 4 jal printf # 45 printf("model: %s\n",car->model); la $a0, $$15 lw $a1, 32($sp) addu $a1, $a1, 24 jal printf # 46 printf("year: %d\n",car->year); la $a0, $$16 lw $t7, 32($sp) lw $a1, 44($t7) jal printf # 47 printf("mileage: %d\n",car->mileage); la $a0, $$17 lw $t8, 32($sp) lw $a1, 48($t8) jal printf 100 96 next 92 cost 88 cost 84 80 mileage 76 year 72 model[16-19] 68 model[12-15] 64 model[8-11] 60 model[4-7] 56 model[0-3] 52 make[16-19] 48 make[12-15] 44 make[8-11] 40 make[4-7] 36 make[0-3] 32 vehicleID 28 24 20 16 12 08 $a0 04 00 SP CMPUT 229 - Computer Organization and Architecture I

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

More Related