1 / 45

Pointers and Arrays

Differentiating Pointers from Data. Pointers and Arrays. 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.

kaspar
Download Presentation

Pointers and Arrays

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. Differentiating Pointers from Data Pointers and Arrays CMPUT 229

  2. CMPUT 229 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 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 } Patt and Patel, pp. 366

  4. CMPUT 229 Stack D1 SP SP SP SP D0 A6 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 .LC0 4 Assembly for Swap Generated with -O0 (part 1) 3 3 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 voidSwap(intfirstVal, intsecondVal) 15 { 16 inttempVal; 17 18 tempVal = firstVal; 19 firstVal = secondVal; 20 secondVal = tempVal; 21 } .LC0: .string “Before Swap: valueA = %d and valueB = %d\n” .LC1: .string “After Swap: valueA = %d and valueB = %d\n” # 5 main() # 6 { link.w A6, #-8 # 7 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # valueA # 8 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # valueB # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA pea .LC0 jbsr printf # printf lea (12,SP),SP

  5. CMPUT 229 SP SP SP A6 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 2) .BB2.main: # 0x44 # 11 Swap(valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA jbsr Swap # Swap addq.l #8, SP .BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf .BB4.main: # 0x74 # 13 } lea (12,SP), SP unlk A6 rts 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 D1 4 D0 3 .LCO 3 4 4 3

  6. CMPUT 229 SP A6 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 2) .BB2.main: # 0x44 # 11 Swap(valueA, valueB); lea (12,SP),SP move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA jbsr Swap # Swap addq.l #8, SP .BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf .BB4.main: # 0x74 # 13 } lea (12,SP), SP unlk A6 rts 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 D1 4 D0 3 <ret adr> 3 4 4 3

  7. CMPUT 229 SP D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { link.w A6,#-4 # 18 tempVal = firstVal; move.l 8(A6),-4(A6) # 20 firstVal = secondVal; move.l 12(A6), 8(A6) # 21 secondVal = tempVal; move.l -4(A6),12(A6) # 22 } unlk A6 rts 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 <ret adr> 3 4 4 3

  8. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { link.w A6,#-4 # 18 tempVal = firstVal; move.l 8(A6),-4(A6) # 20 firstVal = secondVal; move.l 12(A6), 8(A6) # 21 secondVal = tempVal; move.l -4(A6),12(A6) # 22 } unlk A6 rts 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 <old A6> <ret adr> 3 4 4 3

  9. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { link.w A6,#-4 # 18 tempVal = firstVal; move.l 8(A6),-4(A6) # 20 firstVal = secondVal; move.l 12(A6), 8(A6) # 21 secondVal = tempVal; move.l -4(A6),12(A6) # 22 } unlk A6 rts 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 3 <old A6> <ret adr> 3 4 4 3

  10. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { link.w A6,#-4 # 18 tempVal = firstVal; move.l 8(A6),-4(A6) # 20 firstVal = secondVal; move.l 12(A6), 8(A6) # 21 secondVal = tempVal; move.l -4(A6),12(A6) # 22 } unlk A6 rts 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 3 <old A6> <ret adr> 4 4 4 3

  11. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { link.w A6,#-4 # 18 tempVal = firstVal; move.l 8(A6),-4(A6) # 20 firstVal = secondVal; move.l 12(A6), 8(A6) # 21 secondVal = tempVal; move.l -4(A6),12(A6) # 22 } unlk A6 rts 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 3 <old A6> <ret adr> 4 3 4 3

  12. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { link.w A6,#-4 # 18 tempVal = firstVal; move.l 8(A6),-4(A6) # 20 firstVal = secondVal; move.l 12(A6), 8(A6) # 21 secondVal = tempVal; move.l -4(A6),12(A6) # 22 } unlk A6 rts 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 <ret adr> 4 3 4 3

  13. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) .BB2.main: # 0x44 # 11 Swap(valueA, valueB); lea (12,SP),SP move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA jbsr Swap # Swap addq.l #8, SP .BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf .BB4.main: # 0x74 # 13 } lea (12,SP), SP unlk A6 rts 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 4 3

  14. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) .BB2.main: # 0x44 # 11 Swap(valueA, valueB); lea (12,SP),SP move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA jbsr Swap # Swap addq.l #8, SP .BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf .BB4.main: # 0x74 # 13 } lea (12,SP), SP unlk A6 rts 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

  15. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) .BB2.main: # 0x44 # 11 Swap(valueA, valueB); lea (12,SP),SP move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA jbsr Swap # Swap addq.l #8, SP .BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf .BB4.main: # 0x74 # 13 } lea (12,SP), SP unlk A6 rts 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 LC1 3 4 4 3

  16. CMPUT 229 SP A6 D1 4 D0 3 bash-2.01$ ./swapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 3 and valueB = 4 Assembly for Swap Generated with -O0 (part 3) .BB2.main: # 0x44 # 11 Swap(valueA, valueB); lea (12,SP),SP move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA jbsr Swap # Swap addq.l #8, SP .BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l -8(A6), -(SP) # valueB move.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf .BB4.main: # 0x74 # 13 } lea (12,SP), SP unlk A6 rts 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 LC1 3 4 4 3

  17. CMPUT 229 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 gcc compiler with option -O1.

  18. CMPUT 229 Assembly for Swap Generated with -O1 # 5 main() # 6 { link.w A6 move.l A2, -(SP) # 7 int valueA = 3; # 8 int valueB = 4; # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); pea 4.w pea 3.w pea .LC0 lea printf, A2 jbsr (A2) # 11 Swap(valueA, valueB); pea 4.w pea 3.w jbsr Swap # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB pea 4.w pea 3.w pea .LC1 lea printf, A2 jbsr (A2) 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 } Swap: link.w A6,#0 unlk A6 rts

  19. CMPUT 229 What happened at -O1? While compiling the swap code, gcc figured out that swap was doing nothing of consequence, and thus replaced the body of the function with a simple return instruction. However during the compilation of main, gcc did not know what swap was up to, and thus could not eliminate the call to swap. This happens because, at -O1, gcc does not do inter-procedural analysis (IPA), and thus the compilation of each procedure is done in isolation.

  20. CMPUT 229 Assembly for Swap Generated with -O3 # 5 main() # 6 { link.w A6 move.l A2, -(SP) # 7 int valueA = 3; # 8 int valueB = 4; # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); pea 4.w pea 3.w pea .LC0 lea printf, A2 jbsr (A2) # 11 Swap(valueA, valueB); # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB pea 4.w pea 3.w pea .LC1 lea printf, A2 jbsr (A2) 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 }

  21. CMPUT 229 What happened at -O3? The inter-procedural analysis (IPA) determined that Swap made no change to the observable state of main. Thus the call to Swap itself was eliminated.

  22. CMPUT 229 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 } Patt and Patel, pp. 371

  23. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O0 (part 1) # 4 main() # 5 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } 4 3

  24. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack 4028 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O0 (part 1) # 4 main() # 5 { link.w A6, #-8 # 7 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 8 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } 4 3

  25. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack 4020 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O3 (part 1) # 5 main() # 6 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } 4 3

  26. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack 4020 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O3 (part 1) # 4 main() # 5 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } 4020 4 3

  27. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack 4028 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O3 (part 1) # 4 main() # 5 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } 4020 4 3

  28. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack 4024 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O3 (part 1) # 4 main() # 5 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } 4020 4 3

  29. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack 4024 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O3 (part 1) # 4 main() # 5 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } 4024 4020 4 3

  30. CMPUT 229 D0 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 Stack 4024 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O3 (part 1) # 4 main() # 5 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap 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 } <ret adr> 4024 4020 4 3

  31. CMPUT 229 A0 A1 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O0 (part 1) 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 } <ret adr> 4024 4020 4 3 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  32. CMPUT 229 A1 A0 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 Assembly for NewSwap Generated with -O0 (part 1) 4028 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 } <ret adr> 4024 4020 4 3 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts A6

  33. CMPUT 229 A1 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4024 Assembly for NewSwap Generated with -O0 (part 1) 4028 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 } <ret adr> 4024 4020 4 3 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  34. CMPUT 229 A1 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4024 Assembly for NewSwap Generated with -O0 (part 1) 3 4028 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 } <ret adr> 4024 4020 4 3 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  35. CMPUT 229 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4024 A1 Assembly for NewSwap Generated with -O0 (part 1) 4024 3 4028 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 } <ret adr> 4024 4020 4 3 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  36. CMPUT 229 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4020 A1 Assembly for NewSwap Generated with -O0 (part 1) 4024 3 4028 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 } <ret adr> 4024 4020 4 3 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  37. CMPUT 229 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4020 A1 Assembly for NewSwap Generated with -O0 (part 1) 4024 3 4028 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 } <ret adr> 4024 4020 4 4 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  38. CMPUT 229 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4020 A1 Assembly for NewSwap Generated with -O0 (part 1) 4024 3 4028 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 } <ret adr> 4024 4020 4 4 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  39. CMPUT 229 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4020 A1 Assembly for NewSwap Generated with -O0 (part 1) 4024 3 4028 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 } <ret adr> 4024 4020 3 4 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  40. CMPUT 229 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 4020 A1 Assembly for NewSwap Generated with -O0 (part 1) 4024 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 } <ret adr> 4024 4020 3 4 # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  41. CMPUT 229 4000 SP A6 4004 4008 400C 4010 4014 4018 401C 4020 4024 4028 D0 Stack 4024 A0 bash-2.01$ ./newswapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 4 and valueB = 3 4020 A1 Assembly for NewSwap Generated with -O0 (part 1) 4024 # 4 main() # 5 { link.w A6, #-8 # 6 int valueA = 3; moveq #3, D0 move.l D0, -4(A6) # 7 int valueB = 4; moveq #4, D0 move.l D0, -8(A6) # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• # 10 NewSwap(&valueA, &valueB); move.l A6, D0 subq.l #8, D0 move.l D0, -(SP) move.l A6, D0 subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap # 11 printf(”After Swap: valueA = %d and valueB = %d\n", valueA, valueB); ••• 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 } 4024 4020 3 4

  42. CMPUT 229 Assembly for NewSwap Generated with -O3 # 4 main() # 5 { link.w A6, #0 move.l A2, -(SP) # Save A2 # 6 int valueA = 3; # 7 int valueB = 4; # 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); pea 4.w pea 3.w pea .LC0 lea printf,A2 # A2  <Address of printf> jbsr (A2) addq.w #8, SP # 10 NewSwap(&valueA, &valueB); # 11 printf(”After Swap: valueA = %d and valueB = %d\n", valueA, valueB); move.l #3, (SP) pea.w 4.w pea .LC1 jbsr (A2) move.l -4(A6), A2 # Restore A2 unlk A6 rts 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 }

  43. CMPUT 229 What Happened with NewSwap at -O3? Gcc performed inter-procedural constant propagation and the analysis was able to figure out what NewSwap was doing. Thus gcc eliminated the function call altogether. But the compiler still has to generate the code for NewSwap in case the function is called from another file. Only at link time the code for NewSwap can be eliminated if it is not called from anywhere in the program.

  44. CMPUT 229 How would you improve the Assembly generated by gcc at -O1? 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 } # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #-4 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l (A0), -4(A6) # 20 *firstVal = *secondVal; move.l 8(A6), A1 move.l 12(A6), A0 move.l (A0), (A1) # 21 *secondVal = tempVal; move.l 12(A6), A0 move.l -4(A6), (A0) # 22 } unlk A6 rts

  45. CMPUT 229 Assembly for NewSwap Generated with -O3 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 } # 14 void NewSwap(int *firstVal, int *secondVal) # 15 { link.w A6, #0 # 16 int tempVal; # 18 tempVal = *firstVal; move.l 8(A6), A0 move.l 12(A6), A1 move.l (A0), D0 # 20 *firstVal = *secondVal; move.l (A1), (A0) # 21 *secondVal = tempVal; move.l D0, (A1) # 22 } unlk A6 rts

More Related