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

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

The University of Adelaide, School of Computer Science
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Chapter 2 Instructions: Language of the Computer CprE 381 Computer Organization and Assembly Level Programming, Fall 2013 Zhao Zhang Iowa State University.
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.
Ch. 8 Functions.
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.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
רקורסיות 18 יוני יוני יוני 1518 יוני יוני יוני 1518 יוני יוני יוני 151.
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
Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
COMP3221: Microprocessors and Embedded Systems Lecture 13: Functions II Lecturer: Hui Wu Session 2, 2005.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
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.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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.
R3000/001 Assembly Programming using MIPS R3000 CPU R3000 CPU Chip Manufactured by IDT What is MIPS R3000 Processor? A 32-bit RISC CPU developed by MIPS.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
CPSC 388 – Compiler Design and Construction Code Generation.
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
CS61C Midterm 1 Review Summer 2004 Pooya Pakzad Ben Huang Navtej Sadhal.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 4.
Assembly Language Chapter 3.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
1 CS232 Exam 1 Solutions February 20, 2004 Name:  This exam has 5 pages, including this cover.  There are three questions, worth a total of 100 points.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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.
מוסכמות קריאה לשגרה ב- Assembly מיון מערך (merge sort)
Assembly function call convention
Computer Architecture & Operations I
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Program and memory layout
MIPS function continued
Runtime Stack Activation record for hanoi;
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

תחום הכתובות אליהן ניגשים ביחס ל- $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)# $t0 = n2