Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data.

Similar presentations


Presentation on theme: "Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data."— Presentation transcript:

1 Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

2 © 2006 Department of Computing Science 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 © 2006 Department of Computing Science CMPUT 229 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 © 2006 Department of Computing Science CMPUT 229 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 }.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 D1 Stack D0 3 4 3 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 SP A6 4 3.LC0 SP

5 © 2006 Department of Computing Science CMPUT 229 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); 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.LCO 3 4 4 3 4 D1 Stack 3 D0 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6 SP

6 © 2006 Department of Computing Science CMPUT 229 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); 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 3 4 4 3 4 D1 Stack 3 D0 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 SP A6

7 © 2006 Department of Computing Science CMPUT 229 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 { 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 4 D1 3 D0 3 4 4 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

8 © 2006 Department of Computing Science CMPUT 229 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 { 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 4 D1 3 D0 3 4 4 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

9 © 2006 Department of Computing Science CMPUT 229 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 { 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 4 D1 3 D0 3 4 4 3 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

10 © 2006 Department of Computing Science CMPUT 229 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 { 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 4 D1 3 D0 4 4 4 3 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

11 © 2006 Department of Computing Science CMPUT 229 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 { 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 4 D1 3 D0 4 3 4 3 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

12 © 2006 Department of Computing Science CMPUT 229 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 { 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 4 D1 3 D0 4 3 4 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

13 © 2006 Department of Computing Science CMPUT 229 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); 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 Assembly for Swap Generated with -O0 (part 3) 4 D1 3 D0 4 3 4 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

14 © 2006 Department of Computing Science CMPUT 229 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); 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 Assembly for Swap Generated with -O0 (part 3) 4 D1 3 D0 4 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

15 © 2006 Department of Computing Science CMPUT 229 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); 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 Assembly for Swap Generated with -O0 (part 3) 4 D1 3 D0 LC1 3 4 4 3 Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

16 © 2006 Department of Computing Science CMPUT 229 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); 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 Assembly for Swap Generated with -O0 (part 3) 4 D1 3 D0 LC1 3 4 4 3 Stack SP A6 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 3 and valueB = 4

17 © 2006 Department of Computing Science 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 © 2006 Department of Computing Science CMPUT 229 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 -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) Swap: link.w A6,#0 unlk A6 rts

19 © 2006 Department of Computing Science 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 © 2006 Department of Computing Science CMPUT 229 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 # 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)

21 © 2006 Department of Computing Science 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 © 2006 Department of Computing Science CMPUT 229 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

23 © 2006 Department of Computing Science CMPUT 229 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 -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 Stack 4 3 D0 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

24 © 2006 Department of Computing Science CMPUT 229 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 -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 Stack 4 3 D0 4028 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

25 © 2006 Department of Computing Science CMPUT 229 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 { 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 Stack 4 3 D0 4020 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

26 © 2006 Department of Computing Science CMPUT 229 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) # 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 Stack 4020 4 3 D0 4020 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

27 © 2006 Department of Computing Science CMPUT 229 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) # 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 Stack 4020 4 3 D0 4028 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

28 © 2006 Department of Computing Science CMPUT 229 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) # 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 Stack 4020 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

29 © 2006 Department of Computing Science CMPUT 229 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) # 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 Stack 4024 4020 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

30 © 2006 Department of Computing Science CMPUT 229 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) # 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 Stack 4024 4020 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

31 © 2006 Department of Computing Science CMPUT 229 Assembly for NewSwap Generated with -O0 (part 1) 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 } # 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 Stack 4024 4020 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1

32 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 A1 A6 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

33 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4024 A1 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

34 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 3 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4024 A1 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

35 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 3 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4024 A1 4024 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

36 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 3 4 3 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4020 A1 4024 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

37 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 3 4 4 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4020 A1 4024 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

38 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 3 4 4 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4020 A1 4024 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

39 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4028 4024 4020 3 3 4 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4020 A1 4024 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

40 © 2006 Department of Computing Science CMPUT 229 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 } # 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 Assembly for NewSwap Generated with -O0 (part 1) Stack 4024 4020 3 4 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4020 A1 4024 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

41 © 2006 Department of Computing Science CMPUT 229 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 } # 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); Assembly for NewSwap Generated with -O0 (part 1) Stack 4024 4020 3 4 D0 4024 SP A6 4010 4014 4018 401C 4008 400C 4020 4024 4000 4004 4028 A0 4020 A1 4024 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 4 and valueB = 3

42 © 2006 Department of Computing Science CMPUT 229 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 } # 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  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 Assembly for NewSwap Generated with -O3

43 © 2006 Department of Computing Science 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 © 2006 Department of Computing Science CMPUT 229 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 } How would you improve the Assembly generated by gcc at -O1? # 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 © 2006 Department of Computing Science CMPUT 229 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 # 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


Download ppt "Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data."

Similar presentations


Ads by Google