Local workspace of a subroutine: Recursion and recursive subroutines:

Similar documents
Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

The stack and the stack pointer

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

Computer Systems Architecture

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

PROBLEMS (Cap. 4 - Istruzioni macchina)

Instruction Set Architecture

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

HC12 Assembly Language Programming

M6800. Assembly Language Programming

Introduction to Embedded Systems. Software Update Problem

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Reduced Instruction Set Computer (RISC)

Translating C code to MIPS

Typy danych. Data types: Literals:

University of Toronto Faculty of Applied Science and Engineering

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

How It All Works. Other M68000 Updates. Basic Control Signals. Basic Control Signals

Stack Allocation. Run-Time Data Structures. Static Structures

AN2183. Using FLASH as EEPROM on the MC68HC908GP32. Introduction. Semiconductor Products Sector Application Note

More MIPS: Recursion. Computer Science 104 Lecture 9

1 Classical Universal Computer 3

A3 Computer Architecture

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

Optimal Stack Slot Assignment in GCC

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Introduction to MIPS Assembly Programming

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

The 68000's Instruction Set

Chapter 7D The Java Virtual Machine

Common Operating-System Components

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Module 2 Stacks and Queues: Abstract Data Types

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

THUMB Instruction Set

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Instruction Set Architecture

MIPS Assembly Code Layout

10CS35: Data Structures Using C

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Fast Arithmetic Coding (FastAC) Implementations

1 Dispense scritte da M. Ancona CAPITOLO 2 - HEAP (v 1.0) Dispense di proprietà del DISI

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Lecture 27 C and Assembly

X86-64 Architecture Guide

U:\montages\dominique\servo\moteur_AV_AR\program\moteur_AV_AR.asm jeudi 28 avril :32

2) Write in detail the issues in the design of code generator.

Z80 Instruction Set. Z80 Assembly Language

A Utility for Programming Single FLASH Array HCS12 MCUs, with Minimum RAM Overhead

Chapter 7: Additional Topics

Recursive Fibonacci and the Stack Frame. the Fibonacci function. The Fibonacci function defines the Fibonacci sequence, using a recursive definition :

612 CHAPTER 11 PROCESSOR FAMILIES (Corrisponde al cap Famiglie di processori) PROBLEMS

What to do when I have a load/store instruction?

Introduction. What is an Operating System?

Tail call elimination. Michel Schinz

DATA ITEM DESCRIPTION

CSC 2405: Computer Systems II

Real-time system programs

x64 Cheat Sheet Fall 2015

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

DATA STRUCTURE - STACK

C Compiler Targeting the Java Virtual Machine

Assembly Language: Function Calls" Jennifer Rexford!

Stacks. Linear data structures

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

Exception and Interrupt Handling in ARM

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Figure 1: Graphical example of a mergesort 1.

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

MICROPROCESSOR AND MICROCOMPUTER BASICS

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Operating Systems. Virtual Memory

8085 INSTRUCTION SET

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering

1 The Java Virtual Machine

Keil C51 Cross Compiler

Assembly Language Programming

EC 362 Problem Set #2

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

Section 44. CPU with Extended Data Space (EDS)

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

High-speed image processing algorithms using MMX hardware

Glossary of Object Oriented Terms

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

VLIW Processors. VLIW Processors

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Instruction Set Architecture (ISA)

Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers. 8-bit Atmel Microcontrollers. Application Note.

Chapter 12. Paging an Virtual Memory Systems

Transcription:

Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary private variables used in addition to available data registers. Recursion and recursive subroutines: Recursion involves defining the solution of a problem in terms of itself. A recursive subroutine is one that calls itself. Re-entrant subroutines: In multi-tasking systems, a subroutine is re-entrant if more than one task or process are allowed to use (call) the subroutine simultaneously without any ill effects. #1 lec #7 Winter99 12-17-99

The Stack and Local Subroutine Variables: Stack Frames In order for a subroutine to be recursive or re-entrant, the subroutine s local workspace must be attached to each use or call of the subroutine. A stack frame (SF) of size d bytes is defined as a region of temporary storage in memory of size d bytes at the top of the current stack. Upon creating a stack frame: The frame pointer (FP) points to the bottom of the stack frame. Register is normally used as the frame pointer. The stack pointer, SP is updated to point to the top of the frame. In 68000 assembly, the LINK and UNLK instructions are used to facilitate the creation/destruction of local subroutine storage using stack frames. #2 lec #7 Winter99 12-17-99

Example: Factorial Using Iteration Computation: The factorial of a positive integer is defined as: n! = n x (n-1) x (n-2) x (n-3) x... x 1 Pseudo code: Factorial(N) If N = 1 THEN Factorial(N) := 1 ELSE Factorial(N) = N * Factorial(N-1) ENDIF Assembly Subroutine using iteration: FactorI MOVE.L D0,-(SP) Save the initial value of D0 MOVE.W D0,D1 Set the result to the input value Loop SUBQ.W #1,D0 WHILE N > 1 BEQ Exit N = N - 1 MULU D0,D1 Factorial_N = N * Factorial_N BRA Loop Exit MOVE.L (SP)+,D0 Restore the value of D0 RTS #3 lec #7 Winter99 12-17-99

Factorial Using Recursion Assembly program to compute factorial of a number using recursive subroutine calls. Subroutine parameter passing: by value via data registers. Main program: ORG $1000 MAIN MOVE.W NUMB,D0 get number JSR FACTOR go to factorial routine MOVE.W D0,F_NUMB store result STOP #$2700 ORG $2000 NUMB DC.W 5 number to be factorialized F_NUMB DS.W 1 factorial of number #4 lec #7 Winter99 12-17-99

Factorial Using Recursion: Subroutine * Initial conditions: D0.W = number to compute factorial of * where 0 < D0.W < 9 (range to avoid overflow) * Final conditions: D0.W = factorial of input number * Register usage: D0.W destructively used * Sample case: Input: D0.W = 5 * Output: D0.W = 120 FACTOR MOVE.W D0,-(SP) push input number onto stack SUBQ.W #1,D0 decrement number BNE F_CONT reached 1 yet? MOVE.W (SP)+,D0 yes: factorial = 1 RTS return F_CONT JSR FACTOR no: recursively call FACTOR MULU (SP)+,D0 multiply only after stack * contains all numbers RETURN RTS #5 lec #7 Winter99 12-17-99

Factorial Using Recursion Example: Effect On Stack #6 lec #7 Winter99 12-17-99

Creating A Stack Frame of Size d Bytes Word Stack just after a subroutine call Current SP Stack just after creating a stack frame of size d by the subroutine Stack Frame d Current FP Current SP LEA -4(SP), LEA -d(sp),sp #7 lec #7 Winter99 12-17-99

Destroying A Stack Frame Word Current SP Stack Frame d Stack after destroying the stack frame Current FP LEA d(sp),sp Current SP #8 lec #7 Winter99 12-17-99

LINK An,-# d Allocates or creates a frame in the stack for local use by the subroutine of size d bytes. An is an address register serving as the frame pointer (FP); is used. Function: Push the contents of address register An onto the stack. (includes predecrementing SP by 4). Save the stack pointer in An (An points to bottom of frame) Decrement the stack pointer by d (points to the top of the frame) Similar in functionality to the following instruction sequence: MOVEA.L LEA LEA LINK Instruction,-(SP) (SP), -d(sp),sp After creating the frame: parameters are accessed with a positive displacement with respect to FP, i.e MOVE.W 8(),D0 Local temporary storage variables are accessed with negative displacement with respect to i.e. MOVE.L D2,-10() #9 lec #7 Winter99 12-17-99

LINK Instruction Operation Word Current SP Stack just after a subroutine call before LINK LINK,- # d Stack Frame d Current FP original Current SP #10 lec #7 Winter99 12-17-99

UNLK An UNLK UNLinK Instruction Deallocates or destroys a stack frame. Where An is the address register used as frame pointer (FP); usually Function: Restore the stack pointer to the value in address register An i.e SP = An or SP = SP + d Restore register An by popping its value from the stack. (includes post-incrementing SP by 4). Similar in functionality to the following instruction sequence: LEA d(sp),sp MOVEA.L (SP)+,An #11 lec #7 Winter99 12-17-99

Current SP UNLK Instruction Operation Word Stack Frame d UNLK Stack just after UNLINK Current FP original Current SP #12 lec #7 Winter99 12-17-99

A segment of a main calling program: Example: Using A Stack Frame FP MOVE.W D0,-(SP) Push parameter #1 onto the stack MOVE.W D1,-(SP) Push parameter #2 onto the stack JSR SBRT Jump to subroutine SBRT A segment of a subroutine using a stack frame for local storage: SBRT LINK,-#$8 Establish FP and local storage... MOVE.W 10(),D5 Retrieve parameter #1... MOVE.W D4,-4() local write to stack frame... UNLK Deallocate stack frame RTS #13 lec #7 Winter99 12-17-99

SF Example: Effect On Stack LINK,-#$8 #14 lec #7 Winter99 12-17-99

The Effect of Multiple Subroutine Calls on Frame Pointers When Using Local Storage #15 lec #7 Winter99 12-17-99