Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

Similar documents
Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

Xuan Guo. CSC 3210 Computer Organization and Programming Georgia State University. March 31, Lecture XX: Subroutines (3) Xuan Guo

Lecture XXIV: Review Xuan Guo CSC 3210 Computer Organization and Programming Georgia State University April 23, 2015 Xuan Guo Lecture XXIV: Review

Computer Architecture and Organization

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Procedure Call. Procedure Call CS 217. Involves following actions

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

CPSC 352. Computer Organization. Chapter 5: Languages and the

Subroutine. Chapter 8

Grundlagen Microcontroller Processor Core. Günther Gridling Bettina Weiss

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Practical Malware Analysis

Summer 2003 Lecture 14 07/02/03

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

CPE300: Digital System Architecture and Design

Principles of Computer Architecture. Chapter 5: Languages and the Machine

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

Interrupt level Interrupt level 1

The von Neumann Architecture. IT 3123 Hardware and Software Concepts. The Instruction Cycle. Registers. LMC Executes a Store.

Topics in computer architecture

Lab 4 : MIPS Function Calls

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Run-time Environment

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Chapter 9 :: Subroutines and Control Abstraction

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

Subroutines and the Stack

EE4144: ARM Cortex-M Processor

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CSC 2400: Computer Systems. Using the Stack for Function Calls

CS 316: Procedure Calls/Pipelining

Computer Systems Lecture 9

Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

Programming Model 2 A. Introduction

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

8086 Interrupts and Interrupt Responses:

MACHINE LANGUAGE AND ASSEMBLY LANGUAGE

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

SRC Assembly Language Programming - III

Functions and Procedures

That is, we branch to JUMP_HERE, do whatever we need to do, and then branch to JUMP_BACK, which is where we started.

EE 3170 Microcontroller Applications

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

Buffer Overflow Attack (AskCypert CLaaS)

SOEN228, Winter Revision 1.2 Date: October 25,

ME 4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 6

Segmentation in Assembly Language Programming

Talen en Compilers. Johan Jeuring , period 2. December 15, Department of Information and Computing Sciences Utrecht University

S3.0 : A multicore 32-bit Processor

Description of the Simulator

Chapter 2A Instructions: Language of the Computer

Textbook chapter 10. Abstract data structures are. In this section, we will talk about. The array The stack Arithmetic using a stack

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

appendix a The LC-3 ISA A.1 Overview

PROGRAM CONTROL UNIT (PCU)

231 Spring Final Exam Name:

Parameter Passing. Procedure line. Calling procedure line. Most subroutines require parameters Can sometimes pass parameters via registers

Instruction Set Architecture

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Digital IP Cell 8-bit Microcontroller PE80

RECURSIVE FUNCTIONS ON STACK

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

CPE300: Digital System Architecture and Design

EKT222 Miroprocessor Systems Lab 5

Support for high-level languages

ECE232: Hardware Organization and Design

Course Administration

EC2304-MICROPROCESSOR AND MICROCONROLLERS 2 marks questions and answers UNIT-I

Course Introduction. Purpose: Objectives: Content: 27 pages 4 questions. Learning Time: 20 minutes

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Topic 3-a. Calling Convention 2/29/2008 1

SECTION 5 PROGRAM CONTROL UNIT

Computer Architecture Prof. Mainak Chaudhuri Department of Computer Science & Engineering Indian Institute of Technology, Kanpur

ECE 3210 Lab 4: Calculator

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Operating System Control Structures

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

MICROPROGRAMMED CONTROL

UNIMPLEMENTED INSTRUCTIONS

Chap. 8 :: Subroutines and Control Abstraction

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Compiling Code, Procedures and Stacks

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

icroprocessor istory of Microprocessor ntel 8086:

HT8 UL60730 Safety Library -- Program Counter Register Test

Subroutines & Traps. Announcements. New due date for assignment 2 5pm Wednesday, 5May

(2) Explain the addressing mode of OR What do you mean by addressing mode? Explain diff. addressing mode for 8085 with examples.

ET2640 Microprocessors


Implementing Subroutines. Outline [1]

Chapter 3 BRANCH, CALL, AND TIME DELAY LOOP

Transcription:

Chapter 7 Subroutines Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

2 Subroutines Subroutines allow us to either to repeat a computation or to repeat the computation with different arguments. 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

3 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 (.cmul chap 6)

4 Open Subroutines Efficient with no wasted instructions Flexible and can be as general as the programmer 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

5 Register Saving Almost any computation will involve the use of registers Usually when subroutines are called, registers are pushed onto the stack (memory) 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

6 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

7 SPARC Registers 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

REGISTER FILE REGISTER FILE 8-Global 8-Global

10

Current register window 11

12 After branch to subroutine

After the return back to the calling portion of the code 13

14 Window Overflow 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)

15 Restore Instructions 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

16 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

17 Call Instructions 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

18 jmpl Instructions 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

19 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

20 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 restore

21 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.

22 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