Lecture 20: 11/12/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.

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

Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
Solution 2nd Exam.
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.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Organization CS224
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
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
CS3350B Computer Architecture Winter 2015 Lecture 4
Lecture 8: MIPS Instruction Set
Intro to Computer Architecture
CSCE 212 Quiz 2 – 2/2/11 1.What is the purpose of the jal instruction? 2.What are the two conditional branching (if, goto; not the slt instruction) instructions.
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.
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)
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Lecture 17: 10/31/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
Computer Architecture & Operations I
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
CSCI206 - Computer Organization & Programming
Addressing in Jumps jump j Label go to Label op address 2 address
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
Lecture 6: Assembly Programs
Systems Architecture I
CS148 Introduction to Programming II
Presentation transcript:

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 20: 11/12/2002

CS170 Fall Outline A more complex exampleProcedure sort (section 3.10) Arrays versus pointersSection 3.11

Lecture 20: 11/12/2002CS170 Fall sort procedure 1/4 void sort (int v[], int n) { int i, j; for (i=0; i < n; i = i+1) for (j = i-1 ; j >= 0 && v[j] > v[j+1] ; j = j –1) swap (v, j); } What is the generated MIPS assembly code? v, n assigned to $a0, $a1 i, j assigned to registers $s0, $s1 Outer for loop move $s0, $zero# i  0 for1tst:slt $t0,$s0,$a1#$t0  0 if $s0 >= $a1 beq $t0, $zero, exit1# if i >= n go to exit1 …. …..(body of loop) addi $s0,$s0,1# i = i +1 j for1tst# jump to test Exit1:

Lecture 20: 11/12/2002CS170 Fall sort procedure 2/4 Inner for loop addi $s1, $s0,-1# j = i-1 for2tst:slti $t0,$s1,0# $t0  1 if $s1 <0 bne $t0,$zero,exit2# if j <0 go to exit 2 add $t1,$s1,$s1# $t1  j *2 add $t1,$t1,$t1# $t1  j *4 add $t2,$a0,$t1# $t2  v + (j*4) lw $t3, 0($t2)#$t3  v[j] lw $t4,4($t2)#$t4  v[j+1] slt $t0,$t4,$t3# $t0  0 if $t4 >= $t3 beq $t0, $zero,exit2 # if $t4 >= $t3 go to exit2 …. addi $s1.$s1,-1# j = j-1 j for2tst# jump to test Exit2: void sort (int v[], int n) { …… for (j = i-1 ; j >= 0 && v[j] > v[j+1] ; j = j –1) swap (v, j); } What is the generated MIPS assembly code? v, n assigned to $a0, $a1 i, j assigned to registers $s0, $s1

Lecture 20: 11/12/2002CS170 Fall sort procedure 3/4 Procedure call and parameter passing ….. move $s2,$a0# copy $a0 into $s2 move $s3,$a1# copy $a1 into $s3 ….. move $a0, $s2 #first swap parameter is v move $a1,$s1 # second swap parameter is j jal swap Preserving registers in sort ($ra,$s0-$s3) addi $sp,$sp,-20# make room for 5 regs sw $ra, 16($sp)# save $ra on stack sw $s3,12($sp)# save $s3 on stack.. … void sort (int v[], int n) { …… for (j = i-1 ; j >= 0 && v[j] > v[j+1] ; j = j –1) swap (v, j); } What is the generated MIPS assembly code? v, n assigned to $a0, $a1 i, j assigned to registers $s0, $s1 swap needs parameters in $a0, $a1, then sort must save these registers before calling swap. An efficient solution stores $a0, $a1 in other registers instead of saving on stack

Lecture 20: 11/12/2002CS170 Fall sort procedure 4/4

Lecture 20: 11/12/2002CS170 Fall Arrays versus Pointers 1/2 void clear1 (int array[], int size) { int i; for ( i = 0; i < size ; i = i+1) array[i] = 0; } What is the generated MIPS assembly code? void clear2 (int *array, int size) { int *p; for ( p = &array[0]; p < &array[size] ; p =p+1) *p = 0; } What is the generated MIPS assembly code? move $t0, $zero #i = 0 Loop1:add $t1, $t0,$t0 #$t1 = i*2 add $t1,$t1,$t1 #$t1= i *4 add $t2,$a0,$t1 # $t2 = array + 4 *I sw $zero,0($t2) #array[i] = 0 addi $t0,$t0,1 # i = i+1 slt $t3,$t0,$a1 # $t3 = 1 if i < size bne $t3,$zero,loop1 # go to loop1 if more iterations #This code works as long as size is greater than zero move $t0, $a0 #p = address of array[0] Loop2:sw $zero 0($t0) # Memory[p] = 0 addi $t0, $t0,4 # p = p +4 add $t1,$a1,$a1 # $t1  size * 2 add $t1,$t1,$t1 # $t1  size*4 add $t2,$a0,$t1 #$t2  address of array[size] slt $t3,$t0,$t2 #$t3 = 1 if (p < &array[size]) bne $t3,$zero,loop2 # go to loop2 if more iterations #This code works as long as size is greater than zero

Lecture 20: 11/12/2002CS170 Fall Arrays versus Pointers 2/2 void clear1 (int array[], int size) { int i; for ( i = 0; i < size ; i = i+1) array[i] = 0; } What is the generated MIPS assembly code? void clear2 (int *array, int size) { int *p; for ( p = &array[0]; p < &array[size] ; p =p+1) *p = 0; } What is the generated MIPS assembly code? move $t0, $zero #i = 0 Loop1:add $t1, $t0,$t0 #$t1 = i*2 add $t1,$t1,$t1 #$t1= i *4 add $t2,$a0,$t1 # $t2 = array + 4 *I sw $zero,0($t2) #array[i] = 0 addi $t0,$t0,1 # i = i+1 slt $t3,$t0,$a1 # $t3 = 1 if i < size bne $t3,$zero,loop1 # go to loop1 if more iterations #This code works as long as size is greater than zero A more optimized solution move $t0, $a0 #p = address of array[0] add $t1,$a1,$a1 # $t1  size * 2 add $t1,$t1,$t1 # $t1  size*4 add $t2,$a0,$t1 #$t2  address of array[size] Loop2:sw $zero 0($t0) # Memory[p] = 0 addi $t0, $t0,4 # p = p +4 slt $t3,$t0,$t2 #$t3 = 1 if (p < &array[size]) bne $t3,$zero,loop2 # go to loop2 if more iterations #This code works as long as size is greater than zero