Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.

Slides:



Advertisements
Similar presentations
Week 8 Stack and Subroutines. Stack  The stack is a section of data memory (or RAM) that is reserved for storage of temporary data  The data may represent.
Advertisements

CPU Review and Programming Models CT101 – Computing Systems.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
INSTRUCTION SET ARCHITECTURES
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
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.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 1: Bits, bytes and a simple processor dr.ir. A.C. Verschueren.
TK 2633 Microprocessor & Interfacing
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Room: Timbalan Pengarah Pusat Komputer Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 4: Introduction.
TK 2633 Microprocessor & Interfacing
LAB 9 Simulator Chap 14 REED. Datapath Simulator accompanying the text is a datapath simulator a.k.a. the Knob & Switch Computer developed by Grant Braught.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Tannenbaum Machine. Key Features Simple architecture Simple instruction set Focus on –Input -> Higher Level Language –Output -> Machine Language Addressing.
Microcontroller Architecture PIC18F Family
The Computer Processor
Embedded Systems 7763B Mt Druitt College of TAFE
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Machine Instruction Characteristics
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Programming and Problem Solving ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
Application Programs ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
CPU Design. Introduction – The CPU must perform three main tasks: Communication with memory – Fetching Instructions – Fetching and storing data Interpretation.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
PIC18F Programming Model and Instruction Set
Model Computer CPU Arithmetic Logic Unit Control Unit Memory Unit
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
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:
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,
Today’s Lecture Unconditional branch instruction
Subroutines A subroutine is a block of code that is called from different places from within a main program or other subroutines. Saves code space in that.
Advanced Assembly Language Programming
Computer Architecture Lecture 4 by Engineer A. Lecturer Aymen Hasan AlAwady 17/11/2013 University of Kufa - Informatics Center for Research and Rehabilitation.
Structure and Role of a Processor
CPU performs the bulk of data processing operations CPU performs the bulk of data processing operations The CPU is made up of three parts. They are Control.
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.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.
Interrupts ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Interrupts ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
8085 INTERNAL ARCHITECTURE.  Upon completing this topic, you should be able to: State all the register available in the 8085 microprocessor and explain.
Department of Electronic & Electrical Engineering Lecture 3. ● Template program. ● Introduction to IO ● PORTA PORTB TRISA TRISB ● Using a subroutine ●
1 Stack Advanced Programming. 2 The Stack It is a special area of memory used as temporary storage A stack is a LIFO data structure Putting data into.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
Dr. Iyad Jafar Introducing the PIC 16 Series and the 16F84A.
Chapter 10 Interrupts. Basic Concepts in Interrupts  An interrupt is a communication process set up in a microprocessor or microcontroller in which:
Memory Organization 1.  3 types of memory are used in PIC18 microcontroller devices: Program Memory - Flash Memory Data RAM - Static RAM Data EEPROM.
“ INSTRUCTIONS SET OF AVR MICROCONTROLLER ” SIGMA INSTITUTE OF ENGINEERING Prepared By: SR.NO NAME OF STUDENT ENROLLMENT 1 Abhishek Lakhara
Microprocessor and Microcontroller Fundamentals
Computer Science 210 Computer Organization
The Stack.
Microprocessor Systems Design I
The Stack.
Lab 3 - Branching & Subroutines
The Processor and Machine Language
PIC Microcontroller What is PC LATH?????.
Chapter 6 - Procedures and Macros
Central Processing Unit
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
ECE 3430 – Intro to Microcomputer Systems
EECE.3170 Microprocessor Systems Design I
Program and memory layout
Program and memory layout
Computer Organization and Assembly Language
Computer Operation 6/22/2019.
Presentation transcript:

Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning

Subroutine  A group of instructions that performs a specified task  Written independent of main program  Can be called multiple times to perform task By main program By another subroutine 330_072

Stack  Temporary memory storage space used during the execution of a program  Can be part of R/W memory or specially designed group of registers  Stack Pointer (SP) Keeps track of available stack locations 330_073

PIC18 Microcontroller Stack  Hardware Stack 31 registers 21-bits wide Not part of program memory or data registers  Stack Pointer (STKPTR) 5-bit address  Top of the Stack (TOS) Pointed to by the stack pointer Copied into three special function registers TOSU (Upper), TOSH (High), and TOSL (Low) 330_074

STKPTR Register  SP4-SP0: Stack Address  STKOF: Stack overflow Attempt to use more than 31 registers to store information on the stack  STKUNF: Stack underflow Attempt to retrieve more information than was stored previously on the stack 330_075

Stack Instructions  PUSH Increment the memory address in the stack pointer and store the contents of the program counter (PC+2) on the top of the stack  POP Discard the address of the top of the stack and decrement the stack pointer by one 330_076

Subroutine  A group of instructions that performs a specified task  Written independent of a main program  Can be called multiple times to perform task By main program By another subroutine  Call and Return instructions Call a subroutine Return from the subroutine 330_077

Call and Return Instructions  CALL Label, s;Call subroutine at Label  CALL Label, FAST ;FAST equivalent to s = 1 If s = 0:Increment the stack pointer Store the return address (PC+4) on the TOS Go to the subroutine located at Label If s = 1:Also copy W, STATUS, and BSR to their respective shadow registers  RCALL, n;Relative call to subroutine Increment the stack pointer Store the return address (PC+2) on the TOS Branch to the location Label ± 512 words 330_078

Call and Return Instructions  RETURN, s;Return from subroutine  RETURN FAST:FAST equivalent to s = 1 If s = 0:Get the return address from the TOS (to PC) Decrement the stack pointer If s = 1:Also retrieve W, STATUS, and BSR from their shadow registers  RETLW 8-bit;Return literal to WREG Get the return address from the TOS (to PC) Decrement the stack pointer Return 8-bit literal to WREG 330_079

Example 7.2  Program Listing with Memory Addresses 330_0710

Parameter Passing  Parameter passing Information exchanged between a calling program and a subroutine  Subroutine documentation should include Function of the subroutine Input parameters Output parameters Registers modified List of subroutines called 330_0711

Illustrative Program  Problem Statement Write a program to copy a given number of unsigned 8-bit numbers from program memory into specified data registers Add the numbers and display the 16-bit sum at PORTB and PORTC The program should be subdivided into general purpose subroutines 330_0712