faculty of computer science © 2006 cmput 229 pointers and arrays differentiating pointers from data

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

Post on 18-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

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

Faculty of Computer Science

CMPUT 229 © 2006

Pointers and Arrays

Differentiating Pointers from Data

Page 2: Faculty of Computer Science © 2006 CMPUT 229 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.

Page 3: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 4: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 5: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 6: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 7: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 8: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 9: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 10: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 11: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 12: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 13: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 14: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 15: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 16: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 17: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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.

Page 18: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 19: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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.

Page 20: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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)

Page 21: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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.

Page 22: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 23: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 24: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 25: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 26: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 27: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 28: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 29: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 30: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 31: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 32: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 33: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 34: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 35: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 36: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 37: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 38: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 39: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 40: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 41: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 42: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 43: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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.

Page 44: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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

Page 45: Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data

© 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