by Richard P. Paul, 2nd edition, 2000.

Slides:



Advertisements
Similar presentations
Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
Advertisements

Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
The Assembly Language Level
Chapter 7: Subroutines Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition,
S. Barua – CPSC 240 CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program.
1 CSC 3210 Computer Organization and Programming Chapter 7 SUBROUTINES D.M. Rasanjalee Himali.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Runtime Environments Compiler Construction Chapter 7.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
6-1 Chapter 6 - Languages and the Machine Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Objective At the conclusion of this chapter you will be able to:
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
Ass Prof Dr Masri Ayob TK 2633: Microprocessor & Interfacing Lecture 6: Control Instructions.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Lecture 3 Translation.
Chapter 14 Functions.
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
Computer structure: Procedure Calls
Figure 8.1 of course package
Part of the Assembler Language Programmers Toolbox
ECE 3430 – Intro to Microcomputer Systems
Computer Architecture
Subroutines and the Stack
Functions and Procedures
Chapter 7 Subroutines Dr. A.P. Preethy
Chapter 10 The Stack.
Arithmetic using a stack
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Computer Organization and ASSEMBLY LANGUAGE
Computer Science 210 Computer Organization
Fundamentals of Programming
CSC 3210 Computer Organization and Programming
MIPS Instructions.
Subroutines and the Stack
Chapter 8 Central Processing Unit
The University of Adelaide, School of Computer Science
Computer Science 210 Computer Organization
Topic 3-a Calling Convention 1/10/2019.
Chapter 10 And, Finally... The Stack
Procedures and Calling Conventions
8051 ASSEMBLY LANGUAGE PROGRAMMING
Subroutines and the Stack
Runtime Environments What is in the memory?.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

by Richard P. Paul, 2nd edition, 2000. Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2nd edition, 2000. By Anu G. Bourgeois Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

Subroutines allow us to either to repeat a computation or to repeat the computation with different arguments. Subroutines can be used in such situations Subroutines may be either open or closed Open subroutine inserts code whenever it is needed in the program --- macros arguments are passed in the registers that are given as arguments to the subroutine. Closed subroutine code appears only once in the program; whenever it is needed, a jump to the code is executed, and when it completes, a return is made to the instruction occurring after the call instruction (after the delay) arguments may be placed in registers or on the stack

A subroutine also allows you to debug code once and then ensure that all future instantiations of the code will be correct Any register that the subroutine uses must first be saved and then restored after the subroutine completes execution Arguments to subroutines are normally considered to be local variables of the subroutine, and the subroutine is free to change them However, this is not always the case, for e.g., in multiplication, multiplicand is not changed

Open Subroutines are very efficient with no wasted instructions Open Subroutines are very flexible and can be as general as the program wishes to make them Every time open subroutine referenced, the code is expanded, resulting in long code So it is better to write code once as a closed subroutine and to branch to the code, whenever needed

Register Saving Almost any computation will involve the use of registers Usually when subroutines are called, registers are pushed onto the stack and popped from, when it returns To avoid the execution time involved, in CISC, sometimes a special register save mask is used, that would indicate, by bits that were set, which registers were to be saved

SPARC Registers SPARC architecture provides a register file with a mapping register that indicates the active registers It provides 128 registers, with the programmer having access to the eight global registers, and only 24 of the mapped registers at a time save instruction changes the register mapping so that new registers are provided restore instruction restores the register mapping on subroutine return

The 32 registers are divided into four groups : in, local, out and general The eight general register %g0 to %g8 are not mapped and are global to all subroutines “in” & “out” register are used to pass arguments to closed subroutine “local” registers are used for subroutine’s local variables When save instruction is executed the out register become the in register, and a new set of local and out registers is provided

Register Saving When the save instruction is executed the out registers become the in registers, and a new set of local and out registers is provided. The mapping pointer into the register file is changed by 16 registers

8-Global REGISTER FILE 8-Global REGISTER FILE

Current register window

After branch to subroutine

After the return back to the calling portion of the code

If a further five subroutine calls are made without any returns, window overflow will occur saves and restores can be made in a range of six without window overflows or underflows (it is expensive if recursive subroutine calls are frequently made)

Restore Instruction restore instruction restores the register window set. On doing this, a register window can underflow if the cwp is moved to the wim. When this happens the window trap routine restores the registers from the stack and resets the pointers restore is also an add instruction and is used as the final add instruction in a subroutine

Subroutine Linkage The SPARC architecture supports two instructions, call and jmpl, for linking to subroutines The address of instruction which called the subroutine is stored in %o7 The return from subroutine is to %o7 + 8, which is the address of the next instruction to be executed in the main program If a save instruction is executed at the beginning of the subroutine, the contents of %o7 will become %i7, and the return will have to be to %i7 + 8

Call Instruction If the subroutine name is known at assembly time, the call instruction may be used call instruction has a target address label It stores %pc contents to %o7 always followed by a delay slot instruction

jmpl Instruction Used when the address of the subroutine is computed and not known – address is loaded into a register subroutine address is the sum of the source arguments, and the address of the jmpl instruction is stored in the destination register always followed by a delay slot instruction to call a subroutine whose address is in register %o0 and to store the return address into %o7, we would write: jmpl %o0, %o7

Call vs jmpl The assembler recognizes call %o0 as jmpl %o0, %07 The return from a subroutine also makes use of the jmpl instruction We need to return to %i7 + 8 Assembler recognizes ret for: jmpl %i7 + 8, %g0

The call to subroutine is: call subr nop And at the entry of the subroutine subr: save %sp, … %sp with the return ret restore The restore instruction is normally used to fill the delay slot of the ret instruction The ret is expanded to: jmpl %i7 + 8, %g0

Arguments to Subroutines 1. Arguments follow in-line after the call instruction: For example, a Fortran routine to add two numbers, 3 and 4, together would be called by: and handled by the following subroutine code: Note that the return is to %i7 + 16 jumping over the arguments. This type of argument passing is very efficient but is limited. Recursive calls are not possible, nor is it possible to compute any of the arguments.

Return Values Functions are subroutines which return a value In SPARC, the return value is always returned in an “out” register, e.g. %o0, i.e. %i0 of called program We have to put the return value in the corresponding “in”register before executing restore instruction Consider the .mul routine – we pass arguments to the routine using %o0 and %o1 and then expect the result to be passed back into %o0 – this means the routine places the result into %i0 before returning