מוסכמות קריאה לשגרה ב- Assembly מיון מערך (merge sort)

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
The University of Adelaide, School of Computer Science
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Procedure call frame: Hold values passed to a procedure as arguments
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
תכן לוגי ומבוא למחשבים תרגול 131 קריאה לשיגרה (דוגמה) # simple example ‘swap two numbers’.text.globlmain main: la$a0, array addi$a1, $0, 0 jswap back:.data.
331 Week 3. 1Spring 2005 Review: MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
MIPS Programming Arrays Writing Procedures: Calling Convention.
Intro to Computer Architecture
מוסכמות קריאה לשגרה ב- Assembly מיון מערך (merge sort)
RISC Concepts, MIPS ISA and the Mini–MIPS project
Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.
331 Week 3. 1Fall 2003 Head’s Up  This week’s material l MIPS control flow operations (for, while, if-the-else, …) -Reading assignment - PH 3.5  Reminders.
CWRU EECS 3221 Language of the Machine EECS 322 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CWRU EECS 3141 Language of the Machine EECS 314 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
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.
Spr 2015, Feb 9... ELEC / Lecture 4 1 ELEC / Computer Architecture and Design Spring 2015 Compiling and Executing Programs.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
CS Computer Architecture Spring 2006 Week 4 Paul Durand ( Course url:
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
CPSC 388 – Compiler Design and Construction Code Generation.
MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.
CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013.
Csci 136 Computer Architecture II – MIPS Procedure Call Handling Xiuzhen Cheng
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
Morgan Kaufmann Publishers Dr. Zhao Zhang Iowa State University
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
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.
MIPS Functions and the Runtime Stack
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
Addressing in Jumps jump j Label go to Label op address 2 address
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
MIPS functions.
MIPS function continued
Program and memory layout
MIPS function continued
MIPS function continued
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

מוסכמות קריאה לשגרה ב- Assembly מיון מערך (merge sort)

merge-sort אם המקרה טריוויאלי (איבר אחד או שני איברים) מיין והחזר אחרת חלק את המערך לשני חלקים ומיין כל אחד מהם לחוד (קריאה רקורסיבית) מזג את שני החלקים הממוינים

דוגמא למיון מערך תהליך דומה...

פעולת מיזוג

ממשו בשפת אסמבלי את התכנית הבאה המבצעת merge-sort בצורה רקורסיבית: #define MAX_NUM20 void main(void){ int ar[ ] = {5, 2, 16, 4, 32, 1, 8, 12, 17, 6, 7, 9, -1}; int n = 13; int br[MAX_NUM]; sort(ar, n, br); }

// Sort array A[ ] of size n to array B[ ] void sort(int A[ ], int n, int B[ ]){ int A1[MAX_NUM], A2[MAX_NUM]; int n1, n2; if (n == 1) { B[0] = A[0]; return; } else if (n == 2) { merge(A, A+1, B, 1, 1); return; } else { n1 = n/2; n2 = n - n1; sort(A, n1, A1); sort(A+n1, n2, A2); merge(A1, A2, B, n1, n2); return; }

// Merge two sorted arrays P[ ] and Q[ ] of size p and q, respectively, to array R[ ] void merge(int P[ ], int Q[ ], int R[ ], int p, int q){ int i, j, k; i = j = k = 0; while (i < p && j < q) { if (P[i] < Q[j]) R[k++] = P[i++]; else R[k++] = Q[j++]; } while (i < p) R[k++] = P[i++]; while (j < q) R[k++] = Q[j++]; }

הירארכיה בין השגרות mainsort merge sort Caller Callee

NameReg #UsePreserved on call $zero0Constant 0No $v0-$v12-3Results and expressionsNo $a0-$a34-7ArgumentsYes $t0-$t78-15TempNo $s0-$s716-23“saved”Yes $t8-$t924-25TempNo $gp28Global PointerYes $sp29Stack PointerYes $fp30Frame PointerYes $ra31Return AddressYes

תחום הכתובות אליהן ניגשים ביחס ל- $fp עבור כל שיגרה מוקצה איזור זיכרון במחסנית הנקרא frame לטובת: - העברת ארגומנטים מעבר לארבעה - שמירת תוכן הרגיסטרים אשר השיגרה הקוראת לא מעוניינת בשינוי שלהם - עבור משתנים פנימיים של השיגרה מבנה המחסנית (תזכורת)

חישוב גודל המסגרות של השגרות MainSortMerge Callee saved registers $a0 $a1 $a2 $ra $fp $a0 $a1 $a2 $a3 $ra $fp $s0 $s1 $s2 $s3 $fp Local variablesA1[MAX_NUM] A2[MAX_NUM] Frame size5*4=206*4+2*20*4=1845*4=20

sortמבנה המסגרת של השגרה

מבנה המסגרת של sort לפני הקריאה ל- merge

מימוש השגרה main main: addiu$sp, $sp, -20# stack frame size is 20 bytes sw$ra, 4($sp)# save return address sw$fp, 0($sp)# save frame pointer addiu$fp, $sp, 16# set frame pointer sw$a0, 0($fp)# save $a0 sw$a1, -4($fp)# save $a1 sw$a2, -8($fp)# save $a2 la$a0, ar# initialize the first argument lw$a1, n# initialize the second argument la$a2, br# initialize the third argument jalsort# call factorial function lw$a0, 0($fp)# restore $a0 lw$a1, -4($fp)# restore $a1 lw$a2, -8($fp)# restore $a2 lw$ra, 4($sp)# restore return address lw$fp, 0($sp)# restore frame pointer addiu$sp, $sp, 20# pop the stack jr$ra# return to caller

Sort (prologue) sort: addiu$sp, $sp, -184# stack frame size is 184 bytes sw$ra, 164($sp)# save return address sw$fp, 160($sp)# save frame pointer addiu$fp, $sp, 180# set frame pointer sw$a0, 0($fp)# save the first argument sw$a1, -4($fp)# save the second argument sw$a2, -8($fp)# save the third argument sw$a3, -12($fp)# save the fourth argument

Sort (trivial case) li$t0, 2 # if (n > 2) bgt$a1, $t0, callagain # it’s not a trivial case, go to recursive call beq$a1, $t0, simplemerge lw$t1, 0($a0)# sw$t1, 0($a2)# B[0] = A[0]; jfinishsort simplemerge: addi$a1, $a0, 4 ori$a3, $zero, 1 ori$t1, $zero, 1 addiu$sp, $sp, -4# push the fifth argument to the stack sw$t1, 0($sp) jalmerge# call merge(A, A+1, B, 1, 1) addiu$sp, $sp, 4# pop the fifth argument jfinishsort

Sort (recursive call) callagain: srl$t1, $a1, 1 move$a1, $t1 addiu$a2, $fp, -100 jalsort add$t0, $a1, $a1 add$t0, $t0, $t0 add$a0, $a0, $t0 lw$t1, -4($fp) sub$a1, $t1, $a1 addiu$a2, $fp, -180 jalsort addiu$a0, $fp, -100 addiu$sp, $sp, -4# push the fifth argument to the stack sw$a1, 0($sp) lw$t1, -4($fp) sub$a3, $t1, $a1 addiu$a1, $fp, -180 lw$a2, -8($fp) jalmerge addiu$sp, $sp, 4# pop the fifth argument

Sort (epilogue) finishsort: lw$a0, 0($fp)# save the first argument lw$a1, -4($fp)# save the second argument lw$a2, -8($fp)# save the third argument lw$a3, -12($fp)# save the fourth argument lw$ra, 164($sp)# restore return address lw$fp, 160($sp)# restore frame pointer addiu$sp, $sp, 184# pop the stack jr$ra# return to caller

merge (prologue+) merge: addiu$sp, $sp, -20# stack frame size is 20 bytes sw$fp, 0($sp)# save frame pointer addiu$fp, $sp, 16# set frame pointer sw$s0, 0($fp) sw$s1, -4($fp) sw$s2, -8($fp) sw$s3, -12($fp) li$s0, 0# i = 0 li$s1, 0# j = 0 li$s2, -1# k = -1 lw$s3, 4($fp)# $s3 = n2

while: addi $s2, $s2, 1 bge $s0, $a3, padwithQ # if (i>=n1) then goto padwithQ bge $s1, $s3, padwithP # if (j>=n2) then goto padwithP add $t0, $s0, $s0 add $t0, $t0, $t0 add $t0, $t0, $a0 lw $t3, 0($t0) add $t1, $s1, $s1 add $t1, $t1, $t1 add $t1, $t1, $a1 lw $t4, 0($t1) add $t2, $s2, $s2 add $t2, $t2, $t2 add $t2, $t2, $a2 bge $t4, $t3, storeQ sw $t4, 0($t2) # R[k] = Q[j] addi $s1, $s1, 1 # j++ j while storeQ: sw $t3, 0($t2) # R[k] = P[i] addi $s0, $s0, 1 # i++ j while

padwithP: bge $s0, $a3, finishmerge add $t0, $s0, $s0 add $t0, $t0, $t0 add $t0, $t0, $a0 lw $t3, 0($t0) add $t2, $s2, $s2 add $t2, $t2, $t2 add $t2, $t2, $a2 sw $t3, 0($t2) addi $s0, $s0, 1 addi $s2, $s2, 1 j padwithP

padwithQ: bge $s1, $s3, finishmerge add $t1, $s1, $s1 add $t1, $t1, $t1 add $t1, $t1, $a1 lw $t4, 0($t1) add $t2, $s2, $s2 add $t2, $t2, $t2 add $t2, $t2, $a2 sw $t4, 0($t2) addi $s1, $s1, 1 addi $s2, $s2, 1 j padwithQ finishmerge: lw $s0, 0($fp) lw $s1, -4($fp) lw $s2, -8($fp) lw $s3, -12($fp) lw $fp, 0($sp) # restore frame pointer addiu $sp, $sp, 20 # pop the stack jr $ra # return to caller