CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

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.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
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.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
MIPS Assembly Language Programming
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
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data.
1 Lecture 5: MIPS Examples Today’s topics:  the compilation process  full example – sort in C Reminder: 2 nd assignment will be posted later today.
RISC Concepts, MIPS ISA and the Mini–MIPS project
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)
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays (part 2) Differentiating Pointers from Data.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
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.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CHAPTER 2 ISA Instructions (logical + procedure call)
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic7: Floating Point José Nelson Amaral.
MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CPSC 388 – Compiler Design and Construction Code Generation.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CENG 311 Starting a Program. Review (1/2) °IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with +/- infinity, Not-a-Number (Nan),
The Assembly Process Basically why does it all work.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic3: Instructions, The Language of the Machine José Nelson Amaral.
MIPS Functions and the Runtime Stack
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Prof. Hsien-Hsin Sean Lee
Lecture 6: Assembly Programs
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Chapter 16 Pointers and Arrays
Instructions - Type and Format
More C Stack Frames / Pointer variables
Chapter 16 Pointers and Arrays
Addressing in Jumps jump j Label go to Label op address 2 address
Pointers and Arrays.
The University of Adelaide, School of Computer Science
Logical and Decision Operations
Chapter 16 Pointers and Arrays
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Systems Architecture I
Chapter 16 Pointers and Arrays
Presentation transcript:

CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral

CMPUT Computer Organization and Architecture I2 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.

CMPUT Computer Organization and Architecture I3 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

CMPUT Computer Organization and Architecture I4 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 } # 5 main() # 6 { addiu $sp,$sp,-32 #.frame.len.main sd $gp,16($sp) #.lcl_spill_b002 sd $ra,8($sp) #.lcl_spill_b001 lui $a3,%hi(%neg(%gp_rel(main +0))) addiu $a3,$a3,%lo(%neg(%gp_rel(main +0))) addu $gp,$t9,$a3 # 7 int valueA = 3; addiu $a2,$zero,3 sw $a2,0($sp) # valueA # 8 int valueB = 4; addiu $a1,$zero,4 sw $a1,4($sp) # valueB # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata)($gp) addiu $a0,$a0,%got_ofst(.rodata) lw $a1,0($sp) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop $a1 Stack $a2 $sp $a bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I5 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 } Assembly for Swap Generated with -O0 (part 2).BB2.main: # 0x44 # 11 Swap(valueA, valueB); lw $a0,0($sp) # valueA lw $a1,4($sp) # valueB lw $t9,%call16(Swap)($gp) jalr $t9 # Swap nop.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata+48)($gp) addiu $a0,$a0,%got_ofst(.rodata+48) lw $a1,0($sp) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop.BB4.main: # 0x74 # 13 } or $v0,$zero,$zero ld $gp,16($sp) #.lcl_spill_b002 ld $ra,8($sp) #.lcl_spill_b001 addiu $sp,$sp,32 #.frame.len.main jr $ra nop 4 $a1 Stack 3 $a2 4 3 $sp $a bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I6 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 } Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { addiu $sp,$sp,-32 #.frame.len.Swap sw $a0,20($sp) # firstVal sw $a1,28($sp) # secondVal # 17 int tempVal; # 18 # 19 tempVal = firstVal; lw $v1,20($sp) # firstVal sw $v1,0($sp) # tempVal # 20 firstVal = secondVal; lw $v0,28($sp) # secondVal sw $v0,20($sp) # firstVal # 21 secondVal = tempVal; lw $at,0($sp) # tempVal sw $at,28($sp) # secondVal # 22 } addiu $sp,$sp,32 #.frame.len.Swap jr $ra nop.endSwap 4 $a1 3 $a2 3 $a0 Stack 4 3 $sp 0 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I7 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 } Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { addiu $sp,$sp,-32 #.frame.len.Swap sw $a0,20($sp) # firstVal sw $a1,28($sp) # secondVal # 17 int tempVal; # 18 # 19 tempVal = firstVal; lw $v1,20($sp) # firstVal sw $v1,0($sp) # tempVal # 20 firstVal = secondVal; lw $v0,28($sp) # secondVal sw $v0,20($sp) # firstVal # 21 secondVal = tempVal; lw $at,0($sp) # tempVal sw $at,28($sp) # secondVal # 22 } addiu $sp,$sp,32 #.frame.len.Swap jr $ra nop.endSwap 4 $a1 3 $a2 3 $a0 Stack 4 3 $sp 3 4 $v1 $v $at bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I8 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 } Assembly for Swap Generated with -O0 (part 3) # 15 void Swap(int firstVal, int secondVal) # 16 { addiu $sp,$sp,-32 #.frame.len.Swap sw $a0,20($sp) # firstVal sw $a1,28($sp) # secondVal # 17 int tempVal; # 18 # 19 tempVal = firstVal; lw $v1,20($sp) # firstVal sw $v1,0($sp) # tempVal # 20 firstVal = secondVal; lw $v0,28($sp) # secondVal sw $v0,20($sp) # firstVal # 21 secondVal = tempVal; lw $at,0($sp) # tempVal sw $at,28($sp) # secondVal # 22 } addiu $sp,$sp,32 #.frame.len.Swap jr $ra nop.endSwap 4 $a1 3 $a2 3 $a Stack 4 3 $sp 3 $v1 4 $v0 $at bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I9 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 }.BB2.main: # 0x44 # 11 Swap(valueA, valueB); lw $a0,0($sp) # valueA lw $a1,4($sp) # valueB lw $t9,%call16(Swap)($gp) jalr $t9 # Swap nop.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata+48)($gp) addiu $a0,$a0,%got_ofst(.rodata+48) lw $a1,0($sp) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop.BB4.main: # 0x74 # 13 } or $v0,$zero,$zero ld $gp,16($sp) #.lcl_spill_b002 ld $ra,8($sp) #.lcl_spill_b001 addiu $sp,$sp,32 #.frame.len.main jr $ra nop Assembly for Swap Generated with -O0 (part 3) 4 $a1 3 $a2 3 $a Stack 4 3 $sp 3 $v1 4 $v0 $at bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I10 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 }.BB2.main: # 0x44 # 11 Swap(valueA, valueB); lw $a0,0($sp) # valueA lw $a1,4($sp) # valueB lw $t9,%call16(Swap)($gp) jalr $t9 # Swap nop.BB3.main: # 0x58 # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata+48)($gp) addiu $a0,$a0,%got_ofst(.rodata+48) lw $a1,0($sp) # valueA lw $a2,4($sp) # valueB lw $t9,%call16(printf)($gp) jalr $t9 # printf nop.BB4.main: # 0x74 # 13 } or $v0,$zero,$zero ld $gp,16($sp) #.lcl_spill_b002 ld $ra,8($sp) #.lcl_spill_b001 addiu $sp,$sp,32 #.frame.len.main jr $ra nop Assembly for Swap Generated with -O0 (part 3) 3 $a1 4 $a2 3 $a Stack 4 3 $sp 3 $v1 4 $v0 $at bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I11 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 MIPSPro compiler with -O3.

CMPUT Computer Organization and Architecture I12 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 } Assembly for Swap Generated with -O3 (part 1) # 5 main() # 6 { lui $a1,%hi(%neg(%gp_rel(main +0)))# [0] addiu $sp,$sp,-16 # [0].frame.len.main sd $gp,8($sp) # [1].gra_spill_b002 addiu $a1,$a1,%lo(%neg(%gp_rel(main +0)))# [1] addu $gp,$t9,$a1 # [2] lw $t9,%got_disp(printf)($gp) # [3].loc # 7 int valueA = 3; # 8 int valueB = 4; # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata)($gp) # [4] addiu $a2,$zero,4 # [5] sd $ra,0($sp) # [5].gra_spill_b001 addiu $a1,$zero,3 # [5] jalr $t9 # [6] printf addiu $a0,$a0,%got_ofst(.rodata)# [6].BB2.main: # 0x30 # BB:2 frequency = (heuristic) # 11 Swap(valueA, valueB); lw $t9,%call16(Swap)($gp) # [0] addiu $a1,$zero,4 # [2] jalr $t9 # [3] Swap addiu $a0,$zero,3 # [3] $a1 Stack $a2 $sp $a bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I13 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 } Assembly for Swap Generated with -O3 (part 1) # 5 main() # 6 { lui $a1,%hi(%neg(%gp_rel(main +0)))# [0] addiu $sp,$sp,-16 # [0].frame.len.main sd $gp,8($sp) # [1].gra_spill_b002 addiu $a1,$a1,%lo(%neg(%gp_rel(main +0)))# [1] addu $gp,$t9,$a1 # [2] lw $t9,%got_disp(printf)($gp) # [3].loc # 7 int valueA = 3; # 8 int valueB = 4; # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata)($gp) # [4] addiu $a2,$zero,4 # [5] sd $ra,0($sp) # [5].gra_spill_b001 addiu $a1,$zero,3 # [5] jalr $t9 # [6] printf addiu $a0,$a0,%got_ofst(.rodata)# [6].BB2.main: # 0x30 # BB:2 frequency = (heuristic) # 11 Swap(valueA, valueB); lw $t9,%call16(Swap)($gp) # [0] addiu $a1,$zero,4 # [2] jalr $t9 # [3] Swap addiu $a0,$zero,3 # [3] $a1 Stack $a2 $sp $a bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I14 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 } Assembly for Swap Generated with -O3 (part 3) Swap: # 0x70.frame$sp, 0, $ra.BB1.Swap: # 0x70 # # BB:1 frequency = (heuristic) # # 18 # 19 tempVal = firstVal; # 20 firstVal = secondVal; # 21 secondVal = tempVal; # 22 } jr $ra # [0] nop # [0] $a1 Stack $a2 $sp $a bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I15 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 } Assembly for Swap Generated with -O3 (part 2).BB3.main: # 0x40 # # BB:3 frequency = (heuristic) # lw $t9,%got_disp(printf)($gp) # [0] # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata+48)($gp)# [1] addiu $a2,$zero,4 # [2] addiu $a1,$zero,3 # [2] jalr $t9 # [3] printf addiu $a0,$a0,%got_ofst(.rodata+48)# [3].BB4.main: # 0x58 # BB:4 frequency = (heuristic) # 13 } ld $ra,0($sp) # [0].gra_spill_b001 or $v0,$zero,$zero # [2] ld $gp,8($sp) # [3].gra_spill_b002 jr $ra # [3] addiu $sp,$sp,16 # [3].frame.len.main $a1 Stack $a2 $sp $a bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./swapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I16 What happened at -O3? While compiling the swap code, MIPSPro figured out that it was doing nothing of consequence, and thus replaced the body of the function with a simple return instruction. However during the compilation of main, MIPSPro did not know what swap was up to, and thus could not eliminate the call to swap. This happens because, even at -O3, MIPSPro does not do inter-procedural analysis (IPA), and thus the compilation of each procedure is done in isolation. In order to force MIPSPro do perform IPA, we would have to include the option -ipa in the command line. But then the compiler cannot produce the assembly files because IPA actually takes place during the linking phase of the compiler.

CMPUT Computer Organization and Architecture I17 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

CMPUT Computer Organization and Architecture I18 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 } Assembly for NewSwap Generated with -O3 (part 1) # 5 main() # 6 { lui $a2,%hi(%neg(%gp_rel(main +0)))# [0] addiu $sp,$sp,-32 # [0].frame.len.main sd $gp,16($sp) # [1].gra_spill_b002 addiu $a2,$a2,%lo(%neg(%gp_rel(main +0)))# [1] # 7 int valueA = 3; addiu $a1,$zero,3 # [2] sw $a1,0($sp) # [2] valueA addu $gp,$t9,$a2 # [2] lw $t9,%got_disp(printf)($gp) # [3] # 8 int valueB = 4; addiu $a3,$zero,4 # [4] # 10 printf("Before Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata)($gp) # [4] sd $ra,8($sp) # [5].gra_spill_b001 addiu $a2,$zero,4 # [5] addiu $a1,$zero,3 # [5] sw $a3,4($sp) # [6] valueB jalr $t9 # [6] printf addiu $a0,$a0,%got_ofst(.rodata)# [6] # BB:2 frequency = (heuristic) # 11 NewSwap(&valueA, &valueB); lw $t9,%call16(NewSwap)($gp) # [0] addiu $a1,$sp,4 # [2] valueB jalr $t9 # [3] NewSwap addiu $a0,$sp,0 # [3] valueA Stack 0 4 $a2 $a3 $a0 $a x7ffff x7ffff0000 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 $sp 4

CMPUT Computer Organization and Architecture I19 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 } Assembly for NewSwap Generated with -O3 (part 1) # 15 void NewSwap(int *firstVal, int *secondVal) # 16 { # 17 int tempVal; # 18 # 19 tempVal = *firstVal; lw $at,0($a0) # 20 *firstVal = *secondVal; lw $v0,0($a1) sw $v0,0($a0) # 21 *secondVal = tempVal; # 22 } jr $ra sw $at,0($a1) Stack $a2 $a3 4 $a0 0x7ffff0000 $a1 0x7ffff0004 $at $v0 $sp bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4

CMPUT Computer Organization and Architecture I20 Assembly for NewSwap Generated with -O3 (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 } lw $t9,%got_disp(printf)($gp) # 12 printf("After Swap: valueA = %d and valueB = %d\n", valueA, valueB); lw $a0,%got_page(.rodata+48)($gp) lw $a2,4($sp) # [2] valueB lw $a1,0($sp) # [3] valueA jalr $t9 # [3] printf addiu $a0,$a0,%got_ofst(.rodata+48) Stack $a2 $a3 4 $a0 0x7ffff0000 $a1 0x7ffff0004 $at 3 $v0 4 $sp 3 4 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 bash-2.01$./newswapO0 Before Swap: valueA = 3 and valueB = 4 After Swap: valueA = 4 and valueB = 3

CMPUT Computer Organization and Architecture I21 The car.c program #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node *next; } CarNode; void ReadCar(CarNode *car); void PrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); } void ReadCar(CarNode *car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } void PrintCar(CarNode car) { printf("vehicleID: %d\n",car.vehicleID); printf("make: %s\n",car.make); printf("model: %s\n",car.model); printf("year: %d\n",car.year); printf("mileage: %d\n",car.mileage); printf("cost: %f\n",car.cost); } Patt and Patel, pp. 419

CMPUT Computer Organization and Architecture I22 The car.c program #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node  next; } CarNode; vehicleID make[20]model[20] yearmileage cost next

CMPUT Computer Organization and Architecture I23 The car.c program #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node  next; } CarNode; void ReadCar(CarNode  car); void PrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); } main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra

CMPUT Computer Organization and Architecture I24 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t9 $t6 $t0 $t8 $t7 vehicleID

CMPUT Computer Organization and Architecture I25 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t9 $t0 $t8 $t7 vehicleID $t6 vehicleID make[0-3]

CMPUT Computer Organization and Architecture I26 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID $t6 vehicleID make[0-3] $t9 make[0-3] make[4-7]

CMPUT Computer Organization and Architecture I27 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 $t6 vehicleID make[0-3] $t9 make[0-3] make[4-7] make[8-11]

CMPUT Computer Organization and Architecture I28 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID make[0-3] $t9 make[0-3] make[4-7] make[8-11] $t6 make[8-11] make[12-15]

CMPUT Computer Organization and Architecture I29 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 make[8-11] vehicleID make[12-15] $t9 make[12-15] make[16-19] $t6

CMPUT Computer Organization and Architecture I30 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID make[12-15] make[0-3] make[16-19] make[4-7] model[0-3] $t6 $t9

CMPUT Computer Organization and Architecture I31 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID make[12-15] make[0-3] make[4-7] model[0-3] $t6 model[0-3] model[4-7] $t9

CMPUT Computer Organization and Architecture I32 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 model[0-3] vehicleID model[4-7] $t9 model[4-7] model[8-11] $t6

CMPUT Computer Organization and Architecture I33 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID model[4-7] make[0-3] model[8-11] make[4-7] model[12-15] $t6 $t9

CMPUT Computer Organization and Architecture I34 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID make[12-15] make[0-3] make[4-7] model[12-15] $t6 model[12-15] model[16-19] $t9

CMPUT Computer Organization and Architecture I35 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 model[12-15] vehicleID model[16-19] $t9 model[16-19] year $t6

CMPUT Computer Organization and Architecture I36 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID model[16-19] make[0-3] year make[4-7] mileage $t6 $t9

CMPUT Computer Organization and Architecture I37 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 $t8 $t7 vehicleID make[16-19] make[0-3] make[4-7] mileage $t6 mileage $t9

CMPUT Computer Organization and Architecture I38 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 model[12-15] vehicleID $t9 cost1 $t6

CMPUT Computer Organization and Architecture I39 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 vehicleID make[0-3] make[4-7] $t8 $t7 $t9 cost1 $t6 cost2

CMPUT Computer Organization and Architecture I40 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[0-3] vehicleID SP $t0 vehicleID make[0-3] make[4-7] $t6 cost2 $t8 $t7 $t9 cost2 next

CMPUT Computer Organization and Architecture I41 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 cost2 vehicleID next $t9 $t6

CMPUT Computer Organization and Architecture I42 The car.c program main: subu$sp, 168 sw$ra, 84($sp).frame$sp, 168, $ra # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 88 jalReadCar # 27 PrintCar(mycar); addu$t6, $sp, 88 move$t9, $sp addu$t0, $t6, 72 L1: lw$t8, 0($t6) addu$t6, $t6, 12 sw$t8, 0($t9) lw$t7, -8($t6) addu$t9, $t9, 12 sw$t7, -8($t9) lw$t8, -4($t6) sw$t8, -4($t9) bne$t6, $t0, L1 lw$a0, 0($sp) lw$a1, 4($sp) lw$a2, 8($sp) lw$a3, 12($sp) jalPrintCar # 28} move$v0, $zero lw$ra, 84($sp) addu$sp, 168 j$ra next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP $t0 $t8 $t7 cost2] vehicleID next $t9 next $t6 $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 make[8-11]

CMPUT Computer Organization and Architecture I43 The car.c program # 42void PrintCar(CarNode car) # 43{ PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) sw$a1, 36($sp) sw$a2, 40($sp) sw$a3, 44($sp) # 44 printf("vehicleID: %d\n",car.vehicleID); la$a0, string13 lw$a1, 32($sp) jalprintf # 45 printf("make: %s\n",car.make); la$a0, string14 addu$a1, $sp, 32 addu$a1, $a1, 4 jalprintf # 46 printf("model: %s\n",car.model); la$a0, string15 addu$a1, $sp, 32 addu$a1, $a1, 24 jalprintf # 47 printf("year: %d\n",car.year); la$a0, string16 lw$a1, 76($sp) jalprintf next cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID SP vehicleID next $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 make[8-11]

CMPUT Computer Organization and Architecture I44 The car.c program # 42void PrintCar(CarNode car) # 43{ PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) sw$a1, 36($sp) sw$a2, 40($sp) sw$a3, 44($sp) # 44 printf("vehicleID: %d\n",car.vehicleID); la$a0, string13 lw$a1, 32($sp) jalprintf # 45 printf("make: %s\n",car.make); la$a0, string14 addu$a1, $sp, 32 addu$a1, $a1, 4 jalprintf # 46 printf("model: %s\n",car.model); la$a0, string15 addu$a1, $sp, 32 addu$a1, $a1, 24 jalprintf # 47 printf("year: %d\n",car.year); la$a0, string16 lw$a1, 76($sp) jalprintf $ra cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID vehicleID next $a0 vehicleID $a1 make[0-3] $a2 make[4-7] $a3 make[8-11] SP

CMPUT Computer Organization and Architecture I45 Why so much copying? The program car.c passes the datastructure CarNode to the PrintCar function by value. A copy of each byte of CarNode must be made in the stack for each call of the function PrintCar. We could, instead have passed the address of the copy of CarNode that we already had in the stack.

CMPUT Computer Organization and Architecture I46 The car2.c Program #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node  next; } CarNode; void ReadCar(CarNode  car); void PrintCar(CarNode  car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); } void ReadCar(CarNode  car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } void PrintCar(CarNode  car) { printf("vehicleID: %d\n",car->vehicleID); printf("make: %s\n",car->make); printf("model: %s\n",car->model); printf("year: %d\n",car->year); printf("mileage: %d\n",car->mileage); printf("cost: %f\n",car->cost); }

CMPUT Computer Organization and Architecture I47 car-O0.s car2-O0.s #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node  next; } CarNode; void ReadCar(CarNode  car); void PrintCar(CarNode  car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); } main: subu$sp, 112 sw$ra, 28($sp) # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 32 jalReadCar # 27 PrintCar(&mycar); addu$a0, $sp, 32 jalPrintCar # 28} move$v0, $zero lw$ra, 28($sp) addu$sp, 112 j$ra

CMPUT Computer Organization and Architecture I48 car2-O0.s main: subu$sp, 112 sw$ra, 28($sp) # 23 CarNode mycar; # 25 ReadCar(&mycar); addu$a0, $sp, 32 jalReadCar # 27 PrintCar(&mycar); addu$a0, $sp, 32 jalPrintCar # 28} move$v0, $zero lw$ra, 28($sp) addu$sp, 112 j$ra next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] SP make[0-3] vehicleID $a0

CMPUT Computer Organization and Architecture I49 car2-O0.s PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) # 43 printf("vehicleID: %d\n",car->vehicleID); la$a0, $$13 lw$t6, 32($sp) lw$a1, 0($t6) jalprintf # 44 printf("make: %s\n",car->make); la$a0, $$14 lw$a1, 32($sp) addu$a1, $a1, 4 jalprintf # 45 printf("model: %s\n",car->model); la$a0, $$15 lw$a1, 32($sp) addu$a1, $a1, 24 jalprintf # 46 printf("year: %d\n",car->year); la$a0, $$16 lw$t7, 32($sp) lw$a1, 44($t7) jalprintf # 47 printf("mileage: %d\n",car->mileage); la$a0, $$17 lw$t8, 32($sp) lw$a1, 48($t8) jalprintf next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] SP make[0-3] vehicleID $a0

CMPUT Computer Organization and Architecture I50 car2-O0.s PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) # 43 printf("vehicleID: %d\n",car->vehicleID); la$a0, string13 lw$t6, 32($sp) lw$a1, 0($t6) jalprintf # 44 printf("make: %s\n",car->make); la$a0, string14 lw$a1, 32($sp) addu$a1, $a1, 4 jalprintf # 45 printf("model: %s\n",car->model); la$a0, string15 lw$a1, 32($sp) addu$a1, $a1, 24 jalprintf # 46 printf("year: %d\n",car->year); la$a0, string16 lw$t7, 32($sp) lw$a1, 44($t7) jalprintf # 47 printf("mileage: %d\n",car->mileage); la$a0, string17 lw$t8, 32($sp) lw$a1, 48($t8) jalprintf next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] SP make[0-3] vehicleID $a0 $ra SP+64

CMPUT Computer Organization and Architecture I51 car2-O0.s PrintCar: subu$sp, 32 sw$ra, 28($sp) sw$a0, 32($sp) # 43 printf("vehicleID: %d\n",car->vehicleID); la$a0, string13 lw$t6, 32($sp) lw$a1, 0($t6) jalprintf # 44 printf("make: %s\n",car->make); la$a0, string14 lw$a1, 32($sp) addu$a1, $a1, 4 jalprintf # 45 printf("model: %s\n",car->model); la$a0, string15 lw$a1, 32($sp) addu$a1, $a1, 24 jalprintf # 46 printf("year: %d\n",car->year); la$a0, string16 lw$t7, 32($sp) lw$a1, 44($t7) jalprintf # 47 printf("mileage: %d\n",car->mileage); la$a0, string17 lw$t8, 32($sp) lw$a1, 48($t8) jalprintf next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] SP make[0-3] vehicleID $a0 $ra SP+64 $t6 SP+64 $a1 vehicleID

CMPUT Computer Organization and Architecture I52 Quiz #1 predecessors and successors are two vectors of unsigned integers. Assume that: predecessors’ first element is stored at SP+32 successors’ first element is stored at SP+64 Where SP points to the base of the stack frame of the current procedure. Write a sequence of assembly instructions that executes the following C statement: successors[5] = predecessors[7];

CMPUT Computer Organization and Architecture I53 Quiz #1 - Solution predecessors and successors are two vectors of unsigned integers. predecessors’ first element is stored at SP+32 successors’ first element is stored at SP+64 successors[5] = predecessors[7]; $t0  predecessors[7]; successors[5]  $t0; $t1  Addr(predecessor[7]); $t0  0($t1); $t2  Addr(successors[5]); 0($t2)  $t0; $t1  SP+32+7*4 $t0  0($t1); $t2  SP+64+5*4 0($t2)  $t0; add$t1, $sp, 60 lw$t0, 0($t1) add$t2, $sp, 84 sw$t0,0($t2) $t1  SP+60 $t0  0($t1); $t2  SP+84 0($t2)  $t0;

CMPUT Computer Organization and Architecture I54 Quiz #2 names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string. Assume that the first element of names is stored in the memory position SP+16. Write a sequence of MIPS assembly instructions that counts the number of characters in the string whose pointer is at names[7].

CMPUT Computer Organization and Architecture I55 Quiz #2 names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string. Assume that the first element of names is stored in the memory position SP+16. Write a sequence of MIPS assembly instructions that counts the number of characters in the string whose pointer is at names[7]. count = 0; c = names[7]; while (*c != 0) { count = count+1; c = c+1; } $t0  0; $t1  Address(names[7]); $t2  0($t1); while (*($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; } $t0  0; $t2  names[7]; while ( *($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; } $t0  0; $t1  SP+16+7*4 $t2  0($t1); while (0($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; }

CMPUT Computer Organization and Architecture I56 Quiz #2 names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string. Assume that the first element of names is stored in the memory position SP+16. Write a sequence of MIPS assembly instructions that counts the number of characters in the string whose pointer is at names[7]. $t0  0; $t1  SP+16+7*4 $t2  0($t1); while (0($t2) != 0) { $t0 = $t0+1; $t2 = $t2+1; } add$t0, $zero, $zero add$t1, $sp, 44 lw$t2, 0($t1) lb$t3,0($t2) beq$t3, $zero, done addi$t0, $t0, 1 addi$t2, $t2, 1 jloop; loop:

CMPUT Computer Organization and Architecture I57 Quiz #3 Consider the following C function: Write the MIPS assembly code for the first statement of this function. Assume: temp  $s0 node  $s1 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) { unsigned int temp = 0; unsigned int node = 0; (*successors)[node] = temp; printf(“name = %d\n”,*names[node]); }

CMPUT Computer Organization and Architecture I58 Quiz #3 - Solution int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) { unsigned int temp = 0; unsigned int node; (*successors)[node] = temp; printf(“name = %d\n”,*names[node]); } (*successors)[node]  temp; 0($a1)[node]  temp; $t0  0($a1) $t1  Address($t0[node]) 0($t1)  temp; $t0  0($a1) $t0[node]  temp; $t0  0($a1) $t1  $t0 + 4*node 0($t1)  temp; lw$t0, 0($a1) sll$t2, $s1, 2 add$t1, $t0, $t2 sw$s0,0($t1)

CMPUT Computer Organization and Architecture I59 Quiz #4 Consider the following C function: Write the MIPS assembly code to initialize the value of $a1 in the function call to printf. int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) { unsigned int temp; unsigned int node; (*successors)[node] = temp; printf(“name = %s\n”,(*names)[node]); }

CMPUT Computer Organization and Architecture I60 How did we ended up with  names ? $a0 Adam\0Jame sCeci ly Murr a y

CMPUT Computer Organization and Architecture I61 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) {  printf(“  ”,(*names)[node]); } $a2 lw$t0, 0($a2)# $t0   names $t0 Adam\0Jame sCeci ly Murr a y

CMPUT Computer Organization and Architecture I62 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) {  printf(“  ”,(*names)[node]); } lw$t0, 0($a2)# $t0   names $a2 $t3 lw$t0, 0($a2)# $t0   names lw$t2, 8(sp)# $t2  node sll$t2, $t2, 2# $t2  4  node add$t3, $t0, $t2# $t3  (  names) + 4  node $t0 Adam\0Jame sCeci ly Murr a y

CMPUT Computer Organization and Architecture I63 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) {  printf(“  ”,(*names)[node]); } Adam\0Jame sCeci ly $a2 lw$t0, 0($a2)# $t0   names lw$t2, 8(sp)# $t2  node sll$t2, $t2, 2# $t2  4  node add$t3, $t0, $t2# $t3  (  names) + 4  node lw$a1, 0(t3)# $a1  (  names)[node] $t3 $a1 $t0 Murr\0a y

CMPUT Computer Organization and Architecture I64 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) { unsigned int temp; unsigned int node; (  successors)[node] = temp; printf(“name = %s\n”,(  names)[node]); } lw$t0, 0($a2)# $t0   names lw$t2, 8(sp)# $t2  node sll$t2, $t2, 2# $t2  4  node add$t3, $t1, $t2# $t3  (  names) + 4  node lw$a1, 0(t3)# $a1  (  names)[node]

CMPUT Computer Organization and Architecture I65 Quiz #5 Write the code for the following C function: /* NumOfStores counts the number of store instructions that appear between the instruction pointed to by FirstInstruction and the instruction pointed to by LastInstruction */ int NumberOfStores(unsigned int  FirstInstruction, unsigned int  LastInstruction) { } 0x OpCodersrtaddress sw $13, 56($13) For this question, we will consider that the only type of store operation available in the processor is a store word, such as:

CMPUT Computer Organization and Architecture I66 Quiz #5 /* NumOfStores counts the number of store instructions that appear between the instruction pointed to by FirstInstruction and the instruction pointed to by LastInstruction */ int NumberOfStores(unsigned int  FirstInstruction, unsigned int  LastInstruction) { unsigned int *instruction; int count = 0; if(LastInstruction < FirstInstruction) return 0; for(instruction = FirstInstruction ; instruction <= LastInstruction ; instruction++) if((*instruction & 0xfc000000) == 0x8c000000) count++; return count; } 0x OpCodersrtaddress sw $13, 56($13)

CMPUT Computer Organization and Architecture I67 Quiz #6 For this quiz, assume that the only instructions that can effect control flow program transfers are: bne $s3, $s4, 8  PC  PC + 4 if($1  $2) PC  PC OpCodersrtaddress j Exit  PC  concat(PC[31-28],IR[25-0])<< OpCodeaddress

CMPUT Computer Organization and Architecture I68 Quiz #6 Assume that the following C functions are available for your use in this quiz: /* The following functions can manipulate a bit vector of arbitrary length */ unsigned int *CreateBitvector(unsigned int VectorLenght); void SetBit(unsigned int *BitVector, unsigned int BitNumber); void ResetBitRange(unsigned int *BitVector, unsigned int FirstBit, unsigned int LastBit); void ResetBit(unsigned int *BitVector, unsigned int BitNumber); /* BBLeaders returns a bit vector where the bit corresponding to instruction i is 1 if and only if i is a basic block leader. */ unsigned int *BBLeaders(unsigned int *FirstInstruction, unsigned int *LastInstruction) { } Write the BBLeaders function:

CMPUT Computer Organization and Architecture I69 Quiz #6 /* BBLeaders returns a bit vector where the bit corresponding to instruction i is 1 if and only if i is a basic block leader. */ void *BBLeaders(void *FirstInstruction, void *LastInstruction) { unsigned int *instruction; unsigned int *bitvector; int bitcount = 0; unsigned int opcode; unsigned int num_instructions; if(LastInstruction <= FirstInstruction) return 0; num_instructions = LastInstruction - FirstInstruction +1; bitvector = CreateBitVector(num_instructions); ResetBitRange(bitvector,0,num_instructions-1); SetBit(bitvector,0); /* first instruction is a leader */ for(instruction = (unsigned int *) FirstInstruction ; instruction <= LastInstruction ; instruction++) { opcode = *instruction & 0xfc if(opcode == 0x ) { /* process the branch instruction */ } if(opcode == 0x ) { /* process jump */ } bitcount++; } return ((void *) bitvector); }

CMPUT Computer Organization and Architecture I70 Quiz #7 Write MIPS assembly for the following program. Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 }  ind  Stack  apple   ptr  $sp 0 4 8

CMPUT Computer Organization and Architecture I71 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+4 Stack  apple   ptr  $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I72 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+4 Stack  apple   ptr  $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I73 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+4 Stack  apple  $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I74 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+4 Stack  apple  $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I75 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+4 Stack 123 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I76 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+4 Stack 123 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I77 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+8 Stack 123 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I78 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+8 Stack 123 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I79 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+8 Stack 124 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I80 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+8 Stack 124 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I81 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+8 Stack 125 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0

CMPUT Computer Organization and Architecture I82 Quiz #7 # 7 ind = &ptr; addi$t0, $sp, 4# $t0  &ptr sw$t0, 0($sp)# ind  &ptr # 8 *ind = &apple; lw$t0, 0($sp) addi$t1, $sp,8# $t1  &apple sw$t1, 0($t0)# *ind  &apple # 9 **ind = 123; addi$t3,$zero,123 lw$t4, 0($sp) # $t4  *ind lw$t5, 0($t4)# $t5  **ind sw$t3, 0($t5)# **ind  123 # 10 ind++; lw$t0, 0($sp) addi$t0, $t0, 4 sw$t0, 0($sp)# ind++ # 11 *ptr++; lw$t6, 4($sp)# $t6  ptr lw$t7, 0($t6)# $t7  *ptr addi$t7, $t7, 1 sw$t7, 0($t6)# *ptr++ # 12 apple++; lw$t8, 8(sp)# $t8  apple addi$t8, $t8, 1 sw$t8, 8(sp)# apple++ 1 #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 *ptr++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } $sp+8 Stack 125 $sp+8 $sp What are the values in the stack after line 12 is executed? Assume that: apple  $sp+8 ptr  $sp+4 ind  $sp+0