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.

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

EET 2261 Unit 6 The Stack; Subroutines
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
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.
Subroutines and Stacks Lecture L3.1. Subroutine and Stacks The System Stack Subroutines A Data Stack.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
Design of Embedded Systems Using 68HC12(11) Microcontrollers - R. E. Haskell Subroutine and Stacks Chapter 2.
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
© 2010 Kettering University, All rights reserved..
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Stacks and Subroutines. Some example stacks Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
Today’s Lecture Unconditional branch instruction
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
ECE 265 – LECTURE 5 The M68HC11 Basic Instruction Set 12/8/ ECE265.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
1 Stacks, Subroutines, I/O Routines Today: First Hour: Stacks, Subroutines –Section 3.9,3.10 of Huang’s Textbook –In-class Activity #1 Second Hour: I/O.
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
Computer Organization 1 Instruction Fetch and Execute.
ECE Lecture 21 Typical Assembly Language Program Bugs.
Advanced Assembly Language Programming
© 2010 Kettering University, All rights reserved..
ECE 447: Lecture 11 Introduction to Programming in Assembly Language.
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.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Physics 413 Chapter 4: Advanced Assembly Programming.
1 Subroutines Advanced Programming. 2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer.
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.
©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine.
ELE22MIC Lecture 6 Continuation of Lecture 5 Instruction Set Overview, Part 4 –HC-COM - Lab notes –Stack Pointer, Push, Pull Call/return Data –Conditional.
Stacks and Subroutines May 23. Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory system The stack.
CPEN 231: Microcomputer Architecture and Assembly Programming Lecture 10: Stack and Subroutines John Tadrous, Ph.D. ECE Rice University
EET 2261 Unit 6 The Stack; Subroutines
Computer Architecture & Operations I
Computer Science 210 Computer Organization
ECE 3430 – Intro to Microcomputer Systems
Figure 8.1 of course package
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Subroutines and the Stack
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
The CPU12 Microprocessor Core
Registers in the CPU Inside the CPU.
Subroutines and the Stack
The University of Adelaide, School of Computer Science
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
8085 MICROPROCESSOR 8085 CPU Registers and Status Flags S Z AC P C A B
by Richard P. Paul, 2nd edition, 2000.
Figure 8.1 of course package
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
8051 ASSEMBLY LANGUAGE PROGRAMMING
ECE 447: Lecture 15 Stack Operations.
Subroutines and the Stack
EET 2261 Unit 6 The Stack; Subroutines
Some Assembly (Part 2) set.html.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Computer Organization
Presentation transcript:

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 execution The stack has a top and a bottom The SP register is the special register that controls the address in the stack

The 68HC11 Stack SP low address high address top element bottom A 16-bit stack pointer (SP) points to the next available location in the stack. -The 68HC11 CPU registers can be pushed into the stack. -The top element (s) of the stack can be pulled into a CPU register. -The stack grows from high addresses toward lower addresses. Push and Pull Instructions -PSHA: Push A onto the stack -PSHB:Push B onto the stack -PSHX:Push X onto the stack (low order byte is pushed first) -PSHY:Push Y onto the stack (low order byte is pushed first) -PULA:Pull A from the stack -PULB:Pull B from the stack -PULX:Pull X from the stack (high order byte is pulled first) -PULY:Pull Y from the stack (high order byte is pulled first)

Example: Suppose that [A] = $33, [B] = $20, [SP] = $00FF. What will be the contents of the top byte of the stack before and after the execution of PSHA? What will be the contents of the top two bytes of the stack if PSHB is executed? Solution: The contents of the stack before and after the execution of PSHA and PSHB are: XX original stack [SP] = $00FF $33 XX [SP] = $00FE XX $20 $33 [SP] = $00FD after PSHAafter PSHB

Stack as a storage Buffer ORG $0200 ;start address of program BEGINLDS #$FF ;You must define the stack first LDAA $1031 ;Get first data, x TAB ;and square it MUL ADCA #$00 ;round it to 8-bit result PSHA ;and save it LDAA $1032 ;Get second data, y TAB ;and square it MUL ADCA #$00 ;round it to 8-bit result PULB ;retrieve first result ABA ;and add them HEREBRA HERE

Subroutine A subroutine is a self-contained subprogram. Each subroutine contains one or more instructions associated with a specific task. To call a subroutine JSR (jump to subroutine) and BSR (Branch to subroutine) instructions can be used. Each subroutine must end with RTS (Return From Subroutine) instruction. Whenever a JSR/BSR instruction is executed, the address of the instruction following the JSR/BSR is copied into the Stack (the contents of PC is pushed into the Stack). Once that the subroutine has ended (RTS), the address of the following instruction in the main program will be pulled from the Stack into the PC.

Program example

Subroutine Example

Continue

Chapter problems 1,2,3