Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/

Slides:



Advertisements
Similar presentations
OutLine of Tutorial 3 Memory Allocation Const string New string(byte array) Read memory in simulator Function invocation How Import library files Modify.
Advertisements

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.
The University of Adelaide, School of Computer Science
OutLine of Tutorial 5 Summary of Load/Store instructions Exercises reading memory in Simulator Exercises writing code.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
More on Assembly CS 210 Tutorial 4. Detail of Echo program entry main.enter; import "../IMPORT/callsys.h"; block main uses CALLSYS { code { public enter:
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CS 536 Spring Run-time organization Lecture 19.
CS 536 Spring Code generation I Lecture 20.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Intro to Computer Architecture
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Run-time Environment and Program Organization
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
OCC - CS/CIS CS116-Ch00-Orientation Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 1CS 116 Fall 2003 Not quite finished Creating.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 4: MIPS Instruction Set
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Lecture 19: Control Abstraction (Section )
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
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
Computer structure: Procedure Calls
MIPS Assembly Language Programming
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Functions and Procedures
Stack Frame Linkage.
MIPS Instructions.
Procedures and Calling Conventions
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
© Craig Zilles (adapted from slides by Howard Huang)
Runtime Stack Activation record for hanoi;
Presentation transcript:

Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/

getChar public block getChar uses proc, CALLSYS { code { public enter: lda $sp, -sav0($sp); stq $ra, savRet($sp); body: ldiq $a0, CALLSYS_GETCHAR; call_pal CALL_PAL_CALLSYS; return: ldq $ra, savRet($sp); lda $sp, +sav0($sp); ret; } code } block getChar

The program counter. This register points to the address of the next instruction to execute. It is always longword aligned. ra This register is used to hold the return address of a function. The program counter is saved in this register by the bsr instruction and restored from this register by the ret instruction. Hence a function that invokes another function must save the value of this register in its activation record on entry, and restore it before exit. $sp This register is used to hold the stack pointer (the address of the “top” of stack). The invoked function allocates space for itself on the stack by subtracting the size of the activation record from the stack pointer. On return, the invoked function deallocates the stack space by adding the size of the activation record to the stack pointer.

Evaluate the parameters. The first six parameters are stored in registers $a0, $a1, $a2,... $a5. (Any additional parameters are stored at the low address end of the activation record of the invoker. However, we will never deal with functions with more than six parameters.) A bsr instruction is used to invoke the function. The program counter is saved in the $ra (return address) register, and the program counter is set to the address of the start of the function. The invoker can assume that, after the invocation  The result of the function will be in register $v0.  The “saved” registers will contain the same values they had before the invocation.  The stack pointer register will be the same as it was before the invocation.

mov..., $a0; // Set up $a0 mov..., $a1; // Set up $a1 mov..., $a2; // Set up $a2... //... bsr f.enter; // Invoke f mov $v0,...; // Assign return value

If the function needs any local memory, allocate space for the activation record on the stack by subtracting the number of bytes needed from the stack pointer. This is usually done by the instruction  “lda $sp, -frameSize($sp);”. If the function invokes another function, save the return address register in the activation record of the function. (This is because invoking another function will overwrite the return address register.) If the function wants to make use of the “saved” registers for its own local variables, or saving the arguments, save these registers in the activation record of the function. If the function invokes another function, save the argument registers in “saved” registers. (This is better than saving them directly in the activation record, because heavy use is normally made of the arguments, and it is faster to access them via registers than from memory.)

Temporary registers can be used, without needing to save and restore them. Evaluate the body of the function. Use the saved registers for simple local variables that can fit into registers, and are not referred to by “reference”. Use memory on the stack for local arrays, etc. Store the return value in register $v0. Restore any registers that were saved on entry to the function. If the function allocated any local memory for an activation record, deallocate this space by adding the size of the space to the stack pointer. This is usually done by the instruction “lda $sp, frameSize($sp)”. A ret (return) instruction is used to return to just after the bsr instruction used to invoke the function.

For convenience, we define a block, proc, with a local section with symbolic names for the offsets for the saved values of the $ra, $fp, $s0, $s1, $s2, $s3, $s4, and $s5 registers. Because a register contains 8 bytes, the offsets saveRet, savFP, sav0, sav1, sav2,... are 0, 8, 16, 24, 32,... block proc {  local {  protected savRet: quad;  protected savFP: quad;  protected sav0: quad;  protected sav1: quad;  protected sav2: quad;  protected sav3: quad;  protected sav4: quad;  protected sav5: quad;  protected sav6: quad;  } local } block proc

block f uses proc { code { public enter: // Entry Code lda $sp, -frameSize($sp); // Allocate space on stack stq $ra, savRet($sp); // Save $ra on stack stq $s0, sav0($sp); // Save $s0 on stack stq $s1, sav1($sp); // Save $s1 on stack... //... // Initialisation of variables init: mov $a0, $s0; // Save $a0 in $s0 mov $a1, $s1; // Save $a1 in $s1 // (only needed if invokes another function)... // Body of function body:... mov..., $v0; // Store result in $v0 // Exit Code return:... ldq $s1, sav1($sp); // Restore $s1 ldq $s0, sav0($sp); // Restore $s0 ldq $ra, savRet($sp); // Restore $ra lda $sp, +frameSize($sp); // Deallocate space on stack ret; } code } block f

Recursion When writing recursive functions, we have to be particularly careful about saving and restoring registers. There will always be conflicts between the registers used by the invoking and the invoked function (they are after all the same function). Consider the following C program, that generates Pascal’s triangle rather inefficiently.

Recursion in Assembly Studwww.cs.auckland.ac.nz/~mngu01 2/public.html/210/7/