Lecture 5 Subroutines and stack

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
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)
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
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.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Purpose, scope, and expectations of the course Expectations & strategy.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
CSC 8505 Compiler Construction Runtime Environments.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
1 Assembly Language: Function Calls Jennifer Rexford.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Section 5: Procedures & Stacks
Chapter 14 Functions.
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
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
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
The Stack.
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Overview Introduction General Register Organization Stack Organization
Functions and Procedures
Chapter 9 :: Subroutines and Control Abstraction
CSCI206 - Computer Organization & Programming
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 8 Data structures
ECE 3430 – Intro to Microcomputer Systems
Program and memory layout
Procedures and Calling Conventions
Program and memory layout
Program and memory layout
Lecture 13 Harvard architecture Coccone OS demonstrator
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
CSC 497/583 Advanced Topics in Computer Security
© Craig Zilles (adapted from slides by Howard Huang)
Lecture 7 Separate compilation
Lecture 12 Input/Output (programmer view)
Implementing Functions: Overview
Lecture 3 - Instruction Set - Al
Presentation transcript:

Lecture 5 Subroutines and stack Computing platforms Novosibirsk State University University of Hertfordshire D. Irtegov, A.Shafarenko 2018

Stack Stack as a primitive (opaque type with predefined set of operations) Primitive means that we have semantic of the operations But do not know (or should not rely on) details of implementation. So we can change implementation without changing the semantics Two operations: push and pop Push stores data in some [internal] storage Pop retrieves them in LIFO (Last In First Out) order

Stack on CdM-8 SP register (we discussed it during CocoIDE demonstration) Main memory pointed by SP register (*SP) Push rn ((SP-1)→SP) then (rn → *SP) Pop rn (*SP → rn) then ((SP+1)→SP) At CPU power on, SP==0 First push makes SP==255, so stack starts from the top of the RAM Be careful!

How stack works

Be careful! If you push too many times, you can overwrite your program! If you pop more times than push, SP wraps over to 0 and you can overwrite your program again! Commercial CPU (x86, ARM) have hardware protection against this We will discuss it in Operating System course And this protection is not 100% bulletproof (you can mess your stack if you really want to) CdM-8, like most other 8-bit CPU, has no hardware protection (at least in basic configuration)

Wait, there is more! Ldsa rn, offset Addsp n Ldsp rn, Stsp rn SP+offset → rn Not in instruction-set.pdf (we’re working on this) Addsp n SP=SP+n Ldsp rn, Stsp rn Move SP to/from a GP register n

Subroutine call and return Jsr [const] SP-1→SP, then PC → *SP, then const → PC In most modern CPUs this instruction is called Call Jsr mnemonic comes from IBM 360 Rts *SP → *PC, then SP+1→SP Jsrr rn SP-1→SP, then PC → *SP, then rn → PC You can implement function pointers!

Subroutine activation record Create a space for local variables New space for every new call Allows recursion CdM-8 has no frame pointer Caller push param to stack Then jsr to callee Then callee addsp frame size And uses ldsa to access values

Special syntax for local variables (and structs!) 1 tplate foo 00: 2 dc "abcde" 05: 3 a: ds 13 12: 4 dc "this is it" 1c: 5 b: ds 7 23: 6 7 asect 0 8: main: 00: c9 05 9 ldsa r1,foo.a 02: ca 1c 10 ldsa r2,foo.b 04: cb 23 11 ldsa r3,foo._ ….

What exactly tplate directive does? A template is a named absolute section that starts at 0, does not allocate any memory dc parameters are only placeholders is accessible in the whole source file, the section’s text can not be interrupted and continued later Each label defined within a template is absolute and must be referenced using the prefix name.

Calling conventions How to pass parameters How to save registers? On registers? Fast, but CdM-8 has too few registers Cannot pass structures On stack? Relatively slow Who cleans the stack after the call? On CdM-8 it is hard for callee to clean the stack, but other CPU have means for that Callee must know size of parameters to clean the stack (impossible in C) How to save registers? Clean protocol (callee must save all registers before touching them) Dirty protocol (callee can change any register) Hybrid protocol (some registers must be saved, some are not)