MIPS Procedure Calls CSE 378 – Section 3.

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.
10/6: Lecture Topics Procedure call Calling conventions The stack
MIPS Calling Convention Chapter 2.7 Appendix A.6.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
ELEN 468 Advanced Logic Design
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
CS 536 Spring Code generation I Lecture 20.
Accessing parameters from the stack and calling functions.
Intro to Computer Architecture
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Linked Lists in MIPS Let’s see how singly linked lists are implemented in MIPS on MP2, we have a special type of doubly linked list Each node consists.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
IA32 (Pentium) Processor Architecture. Processor modes: 1.Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2.Virtual 8086 modes.
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.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Oct. 25, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Alternative Instruction Sets * Jeremy R. Johnson Wed. Oct. 25, 2000.
Compiler Construction Code Generation Activation Records
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Assembly Language: Function Calls Jennifer Rexford.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Section 5: Procedures & Stacks
Programs – Calling Conventions
Assembly function call convention
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Assembly language.
Credits and Disclaimers
MIPS Assembly Language Programming
C function call conventions and the stack
Lecture 6: Assembly Programs
Functions and the Stack
CSCI206 - Computer Organization & Programming
ISA's, Compilers, and Assembly
ELEN 468 Advanced Logic Design
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
Programs – Calling Conventions
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Lecture 30 (based on slides by R. Bodik)
MIPS Instructions.
The University of Adelaide, School of Computer Science
10/4: Lecture Topics Overflow and underflow Logical operations
Computer Instructions
Program and memory layout
Other Processors Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Presentation transcript:

MIPS Procedure Calls CSE 378 – Section 3

Basics Jump to procedure: Return from a procedure: jal <label> Saves return address to $ra Return from a procedure: jr $ra $a0 - $a3 to pass arguments $v0 and $v1 to return values Save certain registers to preserve across procedure calls. Use the stack $t0-$t9, $a0-a3, $v0-v1 – caller-saved. Caller’s responsibility to save if expects to use these after a call. $s0-$s7, $ra, $fp – callee-saved. Callee’s responsibility to save if callee uses them. 10/8/2003 CSE 378 - Section 2

Calling procedures To call a procedure: Put arguments into $a0-a3 Save caller-saved registers jal <proc> Restore caller-saved registers Example: <some stuff here, uses $t2> … # set up a call to myproc(4) addi $a0, $0, 4 subu $sp, $sp, 4 sw $t2, 0($sp) jal myproc lw $t2, 0($sp) addiu $sp, $sp, 4 <use $t2 again> 10/8/2003 CSE 378 - Section 2

Setup at the start/end of procedure Before any procedure starts running, it must: Allocate memory for callee-saved registers Save callee-saved registers If calling another procedure inside, must save $ra! (why?) At the end of procedure: Place return value into $v0 Restore callee-saved regs jr $ra myproc: # wants to use $s0 inside subu $sp, $sp, 8 sw $ra, 4($sp) sw $s0, 0($sp) … <do some computation in $s0> addi $v0, $s0, 42 lw $s0, 0($sp) lw $ra, 4($sp) addiu $sp, $sp, 8 jr $ra 10/8/2003 CSE 378 - Section 2

Miscellaneous MIPS stack conventions: $sp double-word aligned Minimum frame size is 24 bytes (fits four arguments and return address) Don’t really have to use it for projects Other rules flexible too: have to use common sense for what you need to save and when to get best results. If >4 arguments, use the stack to pass them Caller, callee must agree on where they go in the stack and who pops them off. 10/8/2003 CSE 378 - Section 2

A bit about Intel 80x86 Appeared in 1978, CISC, originally 16-bit, registers have dedicated uses, not general-purpose register architecture Extended till now as 8086  80286  80386  80486  Pentium  Pentium Pro  MMX  … 80186 not used in PCs (used for embedded systems) 80386 extends architecture to 32-bit, makes it nearly a general-purpose machine. Throughout history, compatibility handcuffs architectural advancements Designed to make it easy to code asm by hand 10/8/2003 CSE 378 - Section 2

80x86 registers Only eight general-purpose registers: EAX, EBX, ECX, EDX ESI, EDI ESP – stack pointer, like $sp in MIPS EBP – base pointer, like frame pointer $fp in MIPS Some special registers: EIP = instruction pointer (MIPS PC) EFLAGS = condition codes/flags (like overflow) 10/8/2003 CSE 378 - Section 2

More about registers The general-purpose registers are 32-bit. Can also access lower 16-bits with AX, BX,…(i.e. by removing E at front). This is left over from 16-bit days. Can even access lower 8 bits and next 8 bits by AH/AL, etc.! 10/8/2003 CSE 378 - Section 2

Instructions Very complex, variable-length encoding Can take 1 byte up to 17 (!) bytes Examples (AT&T syntax): mov $5, %eax # eax = 5 add $1, %eax # eax = eax + 1 cmp $0, %esi # eflags = compare esi and 0 je label # jump if they were equal 10/8/2003 CSE 378 - Section 2

Addressing modes Source operands might be: immediate value: imm Register: reg Indirect address: [reg], [imm], [reg + imm] Indexed address (!): [reg + reg’], [reg + imm*reg’], [reg + imm*reg’ + imm’] Destination operands: same except immediate values. 10/8/2003 CSE 378 - Section 2

MIPS vs. Intel: implement x=x+1 (x is in memory) (assume $t1 has address of x): lw $t0, 0($t1) add $t0, $t0, 1 sw $t0, 0($t1) Intel 8086 (assume x is at address %ebp): add 1, (%ebp) 10/8/2003 CSE 378 - Section 2

Another one: x[i] = x[i]+1 MIPS: (assume $t1 = x, $t2 = i): sll $t3, $t2, 2 add $t3, $t2, $t1 lw $t0, 0($t3) add $t0, $t0, 1 sw $t0, 0($t3) x86: (assume x is at address %eax, i is in %ebx): add 1, (%eax, %ebx, 4) 10/8/2003 CSE 378 - Section 2