Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from...
-
date post
18-Dec-2015 -
Category
Documents
-
view
213 -
download
1
Transcript of Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from...
Faculty of Computer Science
CMPUT 229 © 2006
Pointers and Arrays
Differentiating Pointers from Data
© 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.
© 2006
Department of Computing Science
CMPUT 229
Example: A swap function
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Patt and Patel, pp. 366
© 2006
Department of Computing Science
CMPUT 229
Assembly for Swap Generated with -O0 (part 1)
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
.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
34
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
SP
A6
43
.LC0
SP
SP
SP
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 2)
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA jbsr Swap # Swapaddq.l #8, SP
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf
.BB4.main: # 0x74 # 13 }
lea (12,SP), SP unlk A6
rts
.LCO3443
4D1
Stack
3D0
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
SPSPSP
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 2) .BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lea (12,SP),SP move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA jbsr Swap # Swapaddq.l #8, SP
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA pea .LC1 jbsr printf # printf
.BB4.main: # 0x74 # 13 }
lea (12,SP), SP unlk A6
rts
<ret adr>3443
4D1
Stack
3D0
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
SP
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 { 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 4D1
3D0
<ret adr>3443
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 { 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 4D1
3D0
<ret adr>344
<old A6>
3
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 { 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 4D1
3D0
<ret adr>344
3<old A6>
3
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 { 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 4D1
3D0
<ret adr>444
3<old A6>
3
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 { 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 4D1
3D0
<ret adr>434
3<old A6>
3
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O0 (part 3)
# 15 void Swap(int firstVal, int secondVal) # 16 { 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 4D1
3D0
<ret adr>4343
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lea (12,SP),SP move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA jbsr Swap # Swapaddq.l #8, SP
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
move.l -8(A6), -(SP) # valueBmove.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)
4D1
3D0
4343
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lea (12,SP),SP move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA jbsr Swap # Swapaddq.l #8, SP
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
move.l -8(A6), -(SP) # valueBmove.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)
4D1
3D0
43
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lea (12,SP),SP move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA jbsr Swap # Swapaddq.l #8, SP
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
move.l -8(A6), -(SP) # valueBmove.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)
4D1
3D0
LC13443
Stack
SP
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4
A6
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
.BB2.main: # 0x44 # 11 Swap(valueA, valueB);
lea (12,SP),SP move.l -8(A6), -(SP) # valueBmove.l -4(A6), -(SP) # valueA jbsr Swap # Swapaddq.l #8, SP
.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);
move.l -8(A6), -(SP) # valueBmove.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)
4D1
3D0
LC13443
Stack
SP
A6
bash-2.01$ ./swapO0Before Swap: valueA = 3 and valueB = 4After Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
Addresses and Values
The problem with our swap program is that the main is passingthe values of variables valueA and valueB to the swap function.
Thus the swap function does all its work within its own frame inthe stack and never actually changes the state of the variablesin the main function.
Could a “smarter” compiler figure out that the swap function is doing nothing? Lets try the gcc compiler with option -O1.
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -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
© 2006
Department of Computing Science
CMPUT 229
What happened at -O1?
While compiling the swap code, gcc figured out that swap wasdoing nothing of consequence, and thus replaced the body of thefunction with a simple return instruction.
However during the compilation of main, gcc did not knowwhat swap was up to, and thus could not eliminate the call toswap.
This happens because, at -O1, gcc does not do inter-procedural analysis (IPA), and thus the compilation ofeach procedure is done in isolation.
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void Swap(int firstVal, int secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 Swap(valueA, valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void Swap(int firstVal, int secondVal)15 {16 int tempVal;1718 tempVal = firstVal;19 firstVal = secondVal;20 secondVal = tempVal;21 }
Assembly for Swap Generated with -O3
# 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.wpea 3.wpea .LC0
lea printf, A2 jbsr (A2)# 11 Swap(valueA, valueB);# 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB
pea 4.wpea 3.wpea .LC1
lea printf, A2 jbsr (A2)
© 2006
Department of Computing Science
CMPUT 229
What happened at -O3?
The inter-procedural analysis (IPA) determined that Swapmade no change to the observable state of main. Thus the call to Swap itself was eliminated.
© 2006
Department of Computing Science
CMPUT 229
Example2: A new swap function
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Patt and Patel, pp. 371
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
43
D0
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
43
D0 4028
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1)
# 5 main() # 6 {
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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
43
D0 4020
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1)
# 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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
402043
D0 4020
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1)
# 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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
402043
D0 4028
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1)
# 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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
402043
D0 4024
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1)
# 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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
40244020
43
D0 4024
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3 (part 1)
# 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, D0subq.l #8, D0move.l D0, -(SP)move.l A6, D0
subq.l #4, D0 move.l D0, -(SP) jbsr NewSwap
Stack
<ret adr>40244020
43
D0 4024
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
A0
© 2006
Department of Computing Science
CMPUT 229
Assembly for NewSwap Generated with -O0 (part 1)
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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
<ret adr>40244020
43
D0 4024
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
A0bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
A1
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
43
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0
A1
A6
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
43
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0 4024
A1
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
3
43
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0 4024
A1
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
3
43
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0 4024
A1 4024
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
3
43
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0 4020
A1 4024
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
3
44
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0 4020
A1 4024
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
3
44
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0 4020
A1 4024
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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<ret adr>
40244020
3
34
D0 4024
SPA6 4010
40144018401C
4008400C
40204024
40004004
4028
A0 4020
A1 4024
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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
<ret adr>40244020
34
D0 4024
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
A0 4020
A1 4024
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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, D0subq.l #8, D0move.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
40244020
34
D0 4024
SP
A6
401040144018401C
4008400C
40204024
40004004
4028
A0 4020
A1 4024
bash-2.01$ ./newswapO0Before Swap: valueA = 3 and valueB = 4After Swap: valueA = 4 and valueB = 3
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
# 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
Assembly for NewSwap Generated with -O3
© 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.
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
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
© 2006
Department of Computing Science
CMPUT 229
1 #include <stdio.h> 2 void NewSwap(int *firstVal, int *secondVal); 3 4 main() 5 { 6 int valueA = 3; 7 int valueB = 4; 8 9 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB);10 NewSwap(&valueA, &valueB);11 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB);12 }1314 void NewSwap(int *firstVal, int *secondVal)15 {16 int tempVal;1718 tempVal = *firstVal;19 *firstVal = *secondVal;20 *secondVal = tempVal;21 }
Assembly for NewSwap Generated with -O3
# 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