תכן לוגי ומבוא למחשבים תרגול 131 קריאה לשיגרה (דוגמה) # simple example ‘swap two numbers’.text.globlmain main: la$a0, array addi$a1, $0, 0 jswap back:.data.

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.
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.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
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.
Pointers הרצאה קריטית. השאלות הפתוחות מה זה ה- & שמופיע ב scanf מדוע כשמעבירים מחרוזת ל scanf אין צורך ב & האם ניתן להכריז על מערך שגדלו אינו ידוע בתחילת.
ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly.
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.
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.
מבוא למדעי המחשב © אריק פרידמן 1 מצביעים כמערכים דוגמה.
MIPS Programming Arrays Writing Procedures: Calling Convention.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
מוסכמות קריאה לשגרה ב- Assembly מיון מערך (merge sort)
SPIM : A MIPS Simulator Archi & Net Lab 이용석
Multicycle MIPS תרגול כיתה מס' 13.
RISC Concepts, MIPS ISA and the Mini–MIPS project
9/16/091 From C to MIPS David E. Culler CS61CL Sept 16, 2009 Lecture 4 UCB CS61CL F09 Lec 4.
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.
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.
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
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
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.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
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.
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.
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
MIPS Assembly Language Programming
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.
Assembly Language Chapter 3.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
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 Assembly Language Programming ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
MIPS Functions and the Runtime Stack
מוסכמות קריאה לשגרה ב- Assembly מיון מערך (merge sort)
CSCI206 - Computer Organization & Programming
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
MIPS Instructions.
CSCI206 - Computer Organization & Programming
Computer Architecture and Assembly Language
Program and memory layout
Computer Architecture Procedure Calls
MIPS function continued
MIPS function continued
Presentation transcript:

תכן לוגי ומבוא למחשבים תרגול 131 קריאה לשיגרה (דוגמה) # simple example ‘swap two numbers’.text.globlmain main: la$a0, array addi$a1, $0, 0 jswap back:.data array:.word 5, 4, 3, 2, 1 #swap(int v[], int k) #{ #int temp; #temp = v[k]; #v[k] = v[k+1]; #v[k+1] = temp; #} swap:add$t1, $a1, $a1 add$t1, $t1, $t1 add$t1, $a0, $t1 lw$t0, 0($t1) lw$t2, 4($t1) sw$t2, 0($t1) sw$t0, 4($t1) jback - למה לא לחזור מהשיגרה עם הפקודה j Back כאשר Back היא תווית המשוייכת לשורה שאחרי j ? - מה הבעיה בשיטת הקריאה הנ"ל ?

תכן לוגי ומבוא למחשבים תרגול 132 קריאה לשיגרה (דוגמה) # simple example ‘swap two numbers’.text.globlmain main: la$a0, array addi$a1, $0, 0 jalswap.data array:.word 5, 4, 3, 2, 1 #swap(int v[], int k) #{ #int temp; #temp = v[k]; #v[k] = v[k+1]; #v[k+1] = temp; #} swap:add$t1, $a1, $a1 add$t1, $t1, $t1 add$t1, $a0, $t1 lw$t0, 0($t1) lw$t2, 4($t1) sw$t2, 0($t1) sw$t0, 4($t1) jr$ra - נעדיף לבצע את הקריאה ע"י jal, ואת החזרה מהשגרה ע"י jr $ra. מדוע?

תכן לוגי ומבוא למחשבים תרגול 133 מוסכמות קריאה לשיגרה (1) ישנן מוסכמות בין השיגרה הקוראת (Caller) לבין השיגרה הנקראת (Callee) לגבי: - העברת ארגומנטים לשיגרה. - החזרת תוצאות. - באילו רגיסטרים מותר לשיגרה הנקראת להשתמש בלי לשמור על תוכנם ובאילו אסור. אפנדיקס של הספר A22-A32

תכן לוגי ומבוא למחשבים תרגול 134 מוסכמות קריאה לשיגרה (2) - העברת ארבעת הארגומנטים הראשונים לשיגרה ברגיסטרים $a0-$a3 - החזרת תוצאות ברגיסטרים $v0-$v1 - השיגרה הנקראת יכולה להשתמש ברגיסטרים $t0-$t9 בלי לשמור על תוכנם - לאחר קריאה לשיגרה, תוכנם של הרגיסטרים $s0-$s7 חייב להישאר כפי שהיה לפניה שאלות פתוחות: - איך מעבירים יותר מארבעה ארגומנטים? - מה עושים אם השיגרה הקוראת צריכה את תוכנם של חלק או כל הרגיסטרים $t0-$t9 לאחר הקריאה? - מה עושים אם השיגרה הנקראת רוצה להשתמש בחלק או כל הרגיסטרים $s0-$s7?

תכן לוגי ומבוא למחשבים תרגול 135 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 מוסכמות קריאה לשיגרה (3)

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

תכן לוגי ומבוא למחשבים תרגול 137 מוסכמות קריאה לשיגרה (5) Caller pre-call שלב ראשון – העברת ארגומנטים: - ארבעת הראשונים מועברים בתוך $a0-$a3 - שאר הארגומנטים מועברים במחסנית (בסדר כזה שהארגומנט החמישי בראש המחסנית) שלב שני – שמירת רגיסטרים אשר באחריות השיגרה הקוראת: - שמירת אותם ערכי רגיסטרים מתוך $t0-$t9 אשר רוצים להשתמש בהם לאחר הקריאה שלב שלישי – קריאה לשיגרה - ביצוע פקודת jal

תכן לוגי ומבוא למחשבים תרגול 138 מוסכמות קריאה לשיגרה (6) Callee prologue שלב ראשון – הקצאת מסגרת זיכרון חדשה: - החסרת גודל המסגרת הרצויה מ- $sp addiu $sp, $sp, - - בצורה אופיינית, גודל המסגרת המינימלית הוא 32 בתים (8 מילים) שלב שני – שמירת רגיסטרים אשר באחריות השיגרה הנקראת: - רגיסטר $fp נשמר תמיד - רגיסטר $ra נשמר אם שיגרה זו מבצעת קריאה לשיגרה אחרת - רגיסטרים $a0-$a3 נשמרים אם הולכים לשנות אותם - רגיסטרים $s0-$s7 נשמרים אם הולכים להשתמש בהם שלב שלישי – עידכון המצביע $fp addiu $fp, $sp,

תכן לוגי ומבוא למחשבים תרגול 139 מוסכמות קריאה לשיגרה (7) Callee epilogue שלב ראשון – החזרת תוצאה: - השמת תוצאה ל- $v0 ו- $v1 במידת הצורך שלב שני – שיחזור ערכי הרגיסטרים אשר באחריות השיגרה הנקראת: - $s0-$s7, $ra, $fp שלב שלישי – שיחרור זיכרון המוקצה למסגרת: addiu $sp, $sp, שלב רביעי – חזרה לשיגרה הקוראת jr $ra

תכן לוגי ומבוא למחשבים תרגול 1310 מוסכמות קריאה לשיגרה (8) Caller post-call שלב ראשון – שיחזור ערכי הרגיסטרים אשר באחריות השיגרה הקוראת: - טעינת אותם הרגיסטרים מתוך $t0-$t9 אשר נשמרו במחסנית לפני הקריאה

תכן לוגי ומבוא למחשבים תרגול 1311 דוגמה (חישוב עצרת בצורה רקורסיבית) main() { return (fact(10)); } int fact (int n) { if (n < 1) return (1); return (n * fact(n-1)); }

תכן לוגי ומבוא למחשבים תרגול 1312 דוגמה (חישוב עצרת בצורה רקורסיבית).text.globl main main: [0x ] addiu$sp, $sp, -32# stack frane size is 32 bytes [0x ] sw$ra, 20($sp)# save return address [0x ] sw$fp, 16($sp)# save frame pointer [0x C] addiu$fp, $sp, 28# set frame pointer [0x ] li$a0, 10# initialize the first argument [0x ] jalfact# call factorial function [0x ] lw$ra, 20($sp)# restore return address [0x C]lw$fp, 16($sp)# restore frame pointer [0x ] addiu$sp, $sp, 32# pop the stack [0x ] jr$ra# return to caller

תכן לוגי ומבוא למחשבים תרגול 1313.text fact: [0x ] addiu$sp, $sp, -32# stack frane size is 32 bytes [0x C] sw$ra, 20($sp)# save return address [0x ] sw$fp, 16($sp)# save frame pointer [0x ] addiu$fp, $sp, 28# set frame pointer [0x ] sw$a0, 0($fp)# save argument (n) [0x C] bgtz$a0, callagain# if (n >0) goto callagain [0x ] li$v0, 1# return 1 [0x ] jcomplete callagain: [0x ] subiu$a0, $a0, 1# $a0 = n - 1 [0x C] jalfact# call factorial function again [0x ] lw$a0, 0($fp)# load n [0x ] mul$v0, $a0, $v0# $v0 = n*fact(n-1) complete: [0x ] lw$ra, 20($sp)# restore return address [0x C] lw$fp, 16($sp)# restore frame pointer [0x ] addiu$sp, $sp, 32# pop the stack [0x ] jr$ra# return to caller

תכן לוגי ומבוא למחשבים תרגול 1314 תמונת המחסנית במהלך הריצה כיוון גדילת המחסנית