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

Slides:



Advertisements
Similar presentations
Chapter 16 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Advertisements

Ch. 7 Local Variables and Parameter Passing From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 9: MIPS Instruction Set
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
Stack Frames: Using LINK. CEG 320/5208: Stack frames and LINK2 Assembling source code One source file: –Use ORG statements for data and code –Assemble.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Organization CS224
SPARC Architecture & Assembly Language
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
The University of Adelaide, School of Computer Science
Faculty of Computer Science © 2006 CMPUT 229 Why Computer Architecture? An Introduction to CMPUT 229.
Lecture 8: MIPS Instruction Set
Procedures and Control Flow CS351 – Programming Paradigms.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself.
The Structure of the CPU
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral.
Faculty of Computer Science © 2006 CMPUT 229 Representing Information Numbers, Numbers, and Numbers.
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
Overview Projects – Project 1’s Project 2’s no later than Dec 14th Homework Problem – 14.X Pointer Variables Pass by Value Pass by Reference (or by Pointer)
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays (part 2) Differentiating Pointers from Data.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Faculty of Computer Science © 2006 CMPUT 229 Why Computer Architecture? An Introduction to CMPUT 229.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
Guide To UNIX Using Linux Third Edition
Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
CMPUT Efficiency  Often several ways to write the same program  Want to choose the most efficient implementation  Space efficiency  using small.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
CHAPTER 2 ISA Instructions (logical + procedure call)
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
7/23 C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation.
1 Memory and Data Structures Patt and Patel Ch. 10 & 16.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
C Programming Chapters 11, . . .
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
The University of Adelaide, School of Computer Science
Procedures (Functions)
Procedures (Functions)
Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)
Chapter 16 Pointers and Arrays
More C Stack Frames / Pointer variables
Chapter 16 Pointers and Arrays
Pointers and Arrays.
Logical and Decision Operations
Chapter 16 Pointers and Arrays
Simulating Reference Parameters in C
Chapter 16 Pointers and Arrays
Presentation transcript:

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, 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 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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 SP A6 4 3.LC0 SP

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D1 Stack 3 D0 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6 SP

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D1 Stack 3 D0 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 SP A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 D Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 LC Stack SP bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 A6

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 LC Stack SP A6 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 After 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 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.

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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 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.

© 2006 Department of Computing Science CMPUT #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 } void Swap(int firstVal, int secondVal) 15 { 16 int tempVal; 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)

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

© 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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; tempVal = *firstVal; 19 *firstVal = *secondVal; 20 *secondVal = tempVal; 21 } Patt and Patel, pp. 371

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 A C C bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1 A0

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C bash-2.01$./newswapO0 Before 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 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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A0 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 A1

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A0 A1 A6 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A1 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A1 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 D SP A C C A A bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 4 and valueB = 3

© 2006 Department of Computing Science CMPUT #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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

© 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 #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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 #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 } void NewSwap(int *firstVal, int *secondVal) 15 { 16 int tempVal; 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