Fall 2002INEL 4206 Microprocessors Lecture 5 1 Programming Universal Computers Instruction Sets Prof. Bienvenido Velez Lecture 5.

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

Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Assembly Language II CPSC 321 Andreas Klappenecker.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
CS 536 Spring Code generation I Lecture 20.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 4.
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.
Fall 2004ICOM 4036 Programming Laguages Lecture 2 1 Low-Level Programming Prof. Bienvenido Velez ICOM 4036 Lecture 2.
CDA 3101 Spring 2016 Introduction to Computer Organization
Spring 2002INEL 4206 Microprocessors Lecture 5 1 Programming Universal Computers Instruction Sets Prof. Bienvenido Velez Lecture 5.
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Computer Architecture & Operations I
Computer Architecture Instruction Set Architecture
MIPS Instruction Set Advantages
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
ECE/CS 552: Instruction Sets – MIPS
MIPS coding.
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MPIS Instructions Functionalities of instructions Instruction format
MIPS Instructions.
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
Low-Level Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Review.
COMS 361 Computer Organization
MIPS coding.
9/27: Lecture Topics Memory Data transfer instructions
Presentation transcript:

Fall 2002INEL 4206 Microprocessors Lecture 5 1 Programming Universal Computers Instruction Sets Prof. Bienvenido Velez Lecture 5

Fall 2002INEL 4206 Microprocessors Lecture 5 2 What do we know? Instruction Set Architecture Processor Implementation From To How do we get here in the first place? What Next? Instruction Set Design

Fall 2002INEL 4206 Microprocessors Lecture 5 3 Outline Virtual Machines: Interpretation Revisited Example: From HLL to Machine Code Implementing HLL Abstractions –Control structures –Data Structures –Procedures and Functions

Fall 2002INEL 4206 Microprocessors Lecture 5 4 Virtual Machines (VM’s) Type of Virtual Machine ExamplesInstruction Elements Data ElementsComments Application Programs Spreadsheet, Word Processor Drag & Drop, GUI ops, macros cells, paragraphs, sections Visual, Graphical, Interactive Application Specific Abstractions Easy for Humans Hides HLL Level High-Level Language C, C++, Java, FORTRAN, Pascal if-then-else, procedures, loops arrays, structuresModular, Structured, Model Human Language/Thought General Purpose Abstractions Hides Lower Levels Assembly- Level SPIM, MASMdirectives, pseudo- instructions, macros registers, labelled memory cells Symbolic Instructions/Data Hides some machine details like alignment, address calculations Exposes Machine ISA Machine-Level (ISA) MIPS, Intel 80x86 load, store, add, branch bits, binary addresses Numeric, Binary Difficult for Humans

Fall 2002INEL 4206 Microprocessors Lecture 5 5 Computer Science in Perspective Application Programs High-Level Language Assembly Language Machine Language (ISA) CS1/CS2, Programming, Data Structures Programming Languages, Compilers Computer Architecture Computer Human Interaction, User Interfaces People computers INTERPRETATION A CORE theme all throughout Computer Science INEL 4206

Fall 2002INEL 4206 Microprocessors Lecture 5 6 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } We ignore procedures and I/O for now

Fall 2002INEL 4206 Microprocessors Lecture 5 7 Symbolic Name OpcodeAction I=0 Symbolic Name Action I=1 Comp AC ← not ACCompAC <- not AC ShR AC ← AC / 2ShRAC ← AC / 2 BrN i AC < 0 ⇒ PC ← XBrNAC < 0 ⇒ PC ← MEM[X] Jump i PC ← XJumpPC ← MEM[X] Store i MEM[X] ← ACStoreMEM[MEM[X]] ← AC Load i AC ← MEM[X]LoadAC ← MEM[MEM[X]] And i AC ← AC and XAndAC ← AC and MEM[X] Add i AC ← AC + XAddAC ← AC + MEM[X] Easy I A Simple Accumulator Processor Instruction Set Architecture (ISA) Instruction Set

Fall 2002INEL 4206 Microprocessors Lecture 5 8 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } Easy-I Assembly Language C++ HLL

Fall 2002INEL 4206 Microprocessors Lecture 5 9 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } Translate Data: Global Layout Easy-I Assembly Language C++ HLL Issues Memory allocation Data Alignment Data Sizing

Fall 2002INEL 4206 Microprocessors Lecture 5 10 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } Translate Code: Conditionals If-Then 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main:loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative exit: Easy-I Assembly Language C++ HLL Issues Must translate HLL boolean expression into ISA-level branching condition

Fall 2002INEL 4206 Microprocessors Lecture 5 11 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } Translate Code: Iteration (loops) 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main:loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative loop:loadi 1000 brni endloop jump loop endloop: exit: Easy-I Assembly Language C++ HLL

Fall 2002INEL 4206 Microprocessors Lecture 5 12 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } Translate Code: Arithmetic Ops 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main:loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative loop:loadi 1000 brni endloop loadi 1004# compute a – b in AC comp # using 2’s complement add addi 1 add 1000# Uses indirect bit I = 1 jumpi loop endloop: exit: Easy-I Assembly Language C++ HLL

Fall 2002INEL 4206 Microprocessors Lecture 5 13 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } Translate Code: Assignments 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main:loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative loop:loadi 1000 brni endloop loadi 1004# compute a – b in AC comp # using 2’s complement add addi 1 add 1000# Uses indirect bit I = 1 storei 1000 jump loop endloop: exit: Easy-I Assembly Language C++ HLL

Fall 2002INEL 4206 Microprocessors Lecture 5 14 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { if (a >= b) { while (a >= 0) { a = a - b; result ++; } Translate Code: Increments 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main:loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative loop:loadi 1000 brni endloop loadi 1004# compute a – b in AC comp # using 2’s complement add addi 1 add 1000# Uses indirect bit I = 1 storei 1000 loadi 1008# result = result + 1 addi 1 storei 1008 jumpi loop endloop: exit: Easy-I Assembly Language C++ HLL

Fall 2002INEL 4206 Microprocessors Lecture 5 15 Computing Integer Division Easy I Machine Code AddressI BitOpcode (binary) X (base 10) unused unused AddressContents 1000a 1004b 1008result Data Program Challenge Make this program as small and fast as possible

Fall 2002INEL 4206 Microprocessors Lecture 5 16 Computing Integer Division Iterative C++ Version int a = 0; int b = 4; int result = 0; main () { while (a >= b) { a = a - b; result ++; } Revised Version Easy-I Assembly Language C++ HLL 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main: loop: loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative loadi 1004# compute a – b in AC comp # using 2’s complement add addi 1 add 1000# Uses indirect bit I = 1 storei 1000 loadi 1008# result = result + 1 addi 1 storei 1008 jumpi loop endloop: exit:

Fall 2002INEL 4206 Microprocessors Lecture 5 17 Translating Conditional Expressions Translating Logical Expressions loop exit condition ~(a >= b)  ~((a – b) >= 0)  ((a – b) < 0) int a = 0; int b = 4; int result = 0; main () { while (a >= b) { a = a - b; result ++; } What if Loop Exit Condition was: ~(a < b)  

Fall 2002INEL 4206 Microprocessors Lecture :andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main: loop: loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative loadi 1004# compute a – b in AC comp # using 2’s complement add addi 1 add 1000# Uses indirect bit I = 1 storei 1000 loadi 1008# result = result + 1 addi 1 storei 1008 jumpi loop endloop: exit: Computing Integer Division Iterative C++ Version int a = 0; int b = 4; int result = 0; main () { while (a >= b) { a = a - b; result ++; } Peephole Optimization Easy-I Assembly Language C++ HLL 0:andi 0# AC = 0 addi 12 storei 1000# a = 12 (a 1000) andi 0# AC = 0 addi 4 storei 1004# b = 4 (b 1004) andi 0# AC = 0 storei 1008# result = ) main: loop: loadi 1004# compute a – b in AC comp# using 2’s complement add addi 1 add 1000 brni exit# exit if AC negative storei 1000 loadi 1008# result = result + 1 addi 1 storei 1008 jumpi loop endloop: exit:

Fall 2002INEL 4206 Microprocessors Lecture 5 19 The MIPS Architecture ISA at a Glance Reduced Instruction Set Computer (RISC) 32 general purpose 32-bit registers Load-store architecture: Operands in registers Byte Addressable 32-bit address space

Fall 2002INEL 4206 Microprocessors Lecture 5 20 The MIPS Architecture 32 Register Set (32-bit registers) Register #Reg NameFunction r0 Zero constant r4-r7a0-a3Function arguments r1atReserved for Operating Systems r30fpFrame pointer r28gpGlobal memory pointer r26-r27k0-k1Reserved for OS Kernel r31raFunction return address r16-r23s0-s7Callee saved registers r29spStack pointer r8-r15t0-t7Temporary variables r24-r25t8-t9Temporary variables r2-r3v0-v1Function return values

Fall 2002INEL 4206 Microprocessors Lecture 5 21 Simple and uniform 32-bit 3-operand instruction formats –R Format: Arithmetic/Logic operations on registers –I Format: Branches, loads and stores –J Format: Jump Instruction The MIPS Architecture Main Instruction Formats opcode 6 bits rs 5 bits rt 5 bits rd 5 bits shamt 5 bits funct 6 bits opcode 6 bits rs 5 bits rt 5 bits Address/Immediate 16 bits opcode 6 bits rs 5 bits rt 5 bits Address/Immediate 16 bits

Fall 2002INEL 4206 Microprocessors Lecture 5 22 The MIPS Architecture Examples of Native Instruction Set Instruction Group InstructionFunction Arithmetic/ Logic add $s1,$s2,$s3$s1 = $s2 + $s3 addi $s1,$s2,K$s1 = $s2 + K Load/Storelw $s1,K($s2)$s1 = MEM[$s2+K] sw $s1,K($s2)MEM[$s2+K] = $s1 Jumps and Conditional Branches beq $s1,$s2,Kif ($s1=$s2) goto PC K slt $s1,$s2,$s3if ($s2<$s3) $s1=1 else $s1=0 j Kgoto K Proceduresjal K$ra = PC + 4; goto K jr $ragoto $ra

Fall 2002INEL 4206 Microprocessors Lecture 5 23 The SPIM Assembler Examples of Pseudo-Instruction Set Instruction Group SyntaxTranslates to: Arithmetic/ Logic neg $s1, $s2sub $s1, $r0, $s2 not $s1, $s2nor $17, $18, $0 Load/Storeli $s1, Kori $s1, $0, K la $s1, Klui $at, 152 ori $s1, $at, move $s1, $s2 Jumps and Conditional Branches bgt $s1, $s2, Kslt $at, $s1, $s2 bne $at, $0, K sge $s1, $s2, $s3 bne $s3, $s2, foo ori $s1, $0, 1 beq $0, $0, bar foo: slt $s1, $s3, $s2 bar: Pseudo Instructions: translated to native instructions by Assembler

Fall 2002INEL 4206 Microprocessors Lecture 5 24 The SPIM Assembler Examples of Assembler Directives GroupDirectiveFunction Memory Segmentation.data Data Segment starting at.text Text (program) Segment.stack Stack Segment.ktext Kernel Text Segment.kdata Kernel Data Segment Data Allocationx:.word Allocates 32-bit variable x:.byte Allocates 8-bit variable x:.ascii “hello”Allocates 8-bit cell array Other.globl xx is external symbol Assembler Directives: Provide assembler additional info to generate machine code

Fall 2002INEL 4206 Microprocessors Lecture 5 25 Handy MIPS ISA References Appendix A: Patterson & Hennessy SPIM ISA Summary on class website Patterson & Hennessy Back Cover

Fall 2002INEL 4206 Microprocessors Lecture 5 26 The MIPS Architecture Memory Model

Fall 2002INEL 4206 Microprocessors Lecture 5 27 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { while (a >= b) { a = a - b; result ++; } MIPS/SPIM Version MIPS Assembly Language C++.data# Use HLL program as a comment x:.word12# int x = 12; y:.word4# int y = 4; res:.word0# int res = 0;.globlmain.text main:la$s0, x# Allocate registers for globals lw$s1, 0($s0)# x in $s1 lw$s2, 4($s0)# y in $s2 lw$s3, 8($s0)# res in $s3 while:bgt$s2, $s1, endwhile# while (x >= y) { sub$s1, $s1, $s2# x = x - y; addi$s3, $s3, 1# res ++; jwhile# } endwhile: la$s0, x# Update variables in memory sw$s1, 0($s0) sw$s2, 4($s0) sw$s3, 8($s0) jr$ra

Fall 2002INEL 4206 Microprocessors Lecture 5 28 SPIM Assembler Abstractions Symbolic Labels –Instruction addresses and memory locations Assembler Directives –Memory allocation –Memory segments Pseudo-Instructions –Extend native instruction set without complicating arquitecture Macros

Fall 2002INEL 4206 Microprocessors Lecture 5 29 Implementing Procedures Why procedures? –Abstraction –Modularity –Code re-use Initial Goal –Write segments of assembly code that can be re-used, or “called” from different points in the main program. –KISS: Keep It Simple Stupid: no parameters, no recursion, no locals, no return values

Fall 2002INEL 4206 Microprocessors Lecture 5 30 Procedure Linkage Approach I Problem –procedure must determine where to return after servicing the call Solution: Architecture Support –Add a jump instruction that saves the return address in some place known to callee MIPS: jal instruction saves return address in register $ra –Add an instruction that can jump to return address MIPS: jr instruction jumps to the address contained in its argument register

Fall 2002INEL 4206 Microprocessors Lecture 5 31 int a = 0; int b = 0; int res = 0; main () { a = 12; b = 5; res = 0; div(); printf(“Res = %d”,res); } void div(void) { while (a >= b) { a = a - b; res ++; } Computing Integer Division (Procedure Version) Iterative C++ Version MIPS Assembly Language C++.data x:.word 0 y:.word 0 res:.word 0 pf1:.asciiz "Result = " pf2:.asciiz "Remainder = ".globlmain.text main:# int main() { # // main assumes registers sx unused la$s0, x# x = 12; li$s1, 12 sw$s1, 0($s0) la$s0, y# y = 5; li$s2, 5 sw$s2, 0($s0) la$s0, res# res = 0; li$s3, 0 sw$s3, 0($s0) jald# div(); la$s0, x# // Must refresh registers from memory li$s1, 12 lw$s1, 0($s0) la$s0, y# y = 5; li$s2, 5 sw$s2, 0($s0) la$s0, res# res = 0; li$s3, 0 lw$s3, 0($s0) la$a0, pf1# printf("Result = %d \n"); li$v0, 4# //system call to print_str syscall move$a0, $s3 li$v0, 1# //system call to print_int syscall la$a0, pf2# printf("Remainder = %d \n"); li$v0, 4# //system call to print_str syscall move$a0, $s1 li$v0, 1# //system call to print_int syscall jr$ra# return // TO Operating System

Fall 2002INEL 4206 Microprocessors Lecture 5 32 Computing Integer Division (Procedure Version) Iterative C++ Version int a = 0; int b = 0; int res = 0; main () { a = 12; b = 5; res = 0; div(); printf(“Res = %d”,res); } void div(void) { while (a >= b) { a = a - b; res ++; } MIPS Assembly Language C++ # div function # PROBLEM: Must save args and registers before using them d:# void d(void) { # // Allocate registers for globals la$s0, x# // x in $s1 lw$s1, 0($s0) la$s0, y# // y in $s2 lw$s2, 0($s0) la$s0, res# // res in $s3 lw$s3, 0($s0) while:bgt$s2, $s1, ewhile# while (x <= y) { sub$s1, $s1, $s2# x = x - y addi$s3, $s3, 1# res ++ jwhile# } ewhile:# // Update variables in memory la$s0, x sw$s1, 0($s0) la$s0, y sw$s2, 0($s0) la$s0, res sw$s3, 0($s0) enddiv:jr$ra# return; # }

Fall 2002INEL 4206 Microprocessors Lecture 5 33 Pending Problems With Linkage Approach I Registers shared by all procedures Procedures should be able to call other procedures Lack of parameters forces access to globals Recursion requires multiple copies of local data Need a convention for returning function values

Fall 2002INEL 4206 Microprocessors Lecture 5 34 Solution: Use Stacks of Procedure Frames Stack frame contains: –Saved arguments –Saved registers –Return address –Local variables main stack frame div stack frame OS stack growth

Fall 2002INEL 4206 Microprocessors Lecture 5 35 Anatomy of a Stack Frame function arguments saved registers return address Contract: Every function must leave the stack the way it found it local variables of static size caller’s stack frame work area frame pointer stack pointer

Fall 2002INEL 4206 Microprocessors Lecture 5 36 Example: Function Linkage using Stack Frames int x = 0; int y = 0; int res = 0; main () { x = 12; y = 5; res = div(x,y); printf(“Res = %d”,res); } int div(int a,int b) { int res = 0; if (a >= b) { res = div(a-b,b) + 1; } else { res = 0; } return res; } Add return values Add parameters Add recursion Add local variables

Fall 2002INEL 4206 Microprocessors Lecture 5 37 MIPS: Procedure Linkage Summary First 4 arguments passed in $a0-$a3 Other arguments passed on the stack Return address passed in $ra Return value(s) returned in $v0-$v1 Sx registers saved by callee Tx registers saved by caller