CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.

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

Accessing Atmega32 SRAM data memory
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
There are two types of addressing schemes:
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
How does the stack work? On using the Pentium’s push, pop, call, and ret instructions.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
S. Barua – CPSC 240 CHAPTER 10 THE STACK Stack - A LIFO (last-in first-out) storage structure. The.
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.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
Chapter 9 Trap Routines TRAP number (go to service routine) & RET (return from service routine) Subroutines (or Functions) JSR offset or JSRR rn (go to.
Embedded Systems 7763B Mt Druitt College of TAFE
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..
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Today’s Lecture Unconditional branch instruction
9/20/6Lecture 2 - Prog Model Architecture, Data Types and Addressing Modes.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Stacks & Subroutines Content. Stacks A stack is a Last In First Out (LIFO) buffer containing a number of data items usually implemented as a block of.
Linear Data Structures
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.
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Winter 2005CS-2851 Dr. Mark L. Hornick 1 Recursion.
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.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
“ INSTRUCTIONS SET OF AVR MICROCONTROLLER ” SIGMA INSTITUTE OF ENGINEERING Prepared By: SR.NO NAME OF STUDENT ENROLLMENT 1 Abhishek Lakhara
Section 5: Procedures & Stacks
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.
Introduction to Smart Systems
Embedded Systems Programming Examples and Comparison
Figure 8.1 of course package
ECE 3430 – Intro to Microcomputer Systems
The Stack.
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
Subroutines and the Stack
Chapter 10 And, Finally... The Stack
Chapter 3 Addressing Modes
Chapter 10 The Stack.
Arithmetic using a stack
CS 301 Fall 2002 Control Structures
Stack and Subroutines Module M17.1 Section 11.2.
ECE 3430 – Intro to Microcomputer Systems
HKN ECE 220: Spring 2018 Midterm 1
MIPS Instructions.
Subroutines and the Stack
68000 Architecture, Data Types and Addressing Modes
Figure 8.1 of course package
Chapter 10 And, Finally... The Stack
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
8051 ASSEMBLY LANGUAGE PROGRAMMING
Program and memory layout
Subroutines and the Stack
COMP3221: Microprocessors and Embedded Systems
Program and memory layout
Program and memory layout
Computer Organization and Assembly Language
Computer Organization
Presentation transcript:

CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack

CS-280 Dr. Mark L. Hornick 2 CS2851 Review: the Queue Queue: a traditional CS data structure Classical semantics (behavior) are: Elements can only be inserted at the back and removed from the front of the collection This is known as First-In, First-Out (FIFO)

CS-280 Dr. Mark L. Hornick 3 JCF Queue – behavioral methods offer() – place an element at the back of the queue poll() or remove() – return and remove an element from the front of the queue poll() returns null if the queue is empty remove() throws an exception if the queue is empty peek() or element() – return (without removing) the element at the front of the queue peek() returns null if the queue is empty element() throws an exception if the queue is empty

CS-280 Dr. Mark L. Hornick 4 CS2851 Review: Stack Another traditional CS data structure Classical semantics (behavior) Elements can only be inserted and removed from the front of the collection This is known as Last-In, First-Out (LIFO) Less commonly: First-Out, Last-In (FILO) Random-access is not defined

CS-280 Dr. Mark L. Hornick 5 JCF Stack – behavioral methods The naming for the structure and the methods is an analogy for how the data elements within the structure are accessed Principal methods that define behavior: push() – place an element on (top of) the stack pop() – return and remove an element from the (top of the) stack peek() – return the top element of the stack Without removing (popping) it

CS-280 Dr. Mark L. Hornick 6 Most CPUs have a built-in implementation of Stack The actual stack data structure is just an ordered collection of bytes The stack “data structure” is maintained in Data Memory A special CPU register (SP) is used to keep track of the front of the stack Dedicated instructions are used to push data (bytes) onto and pull items from the stack

CS-280 Dr. Mark L. Hornick 7 By convention, the stack starts at (or very near) the end of data memory On our Atmega32 systems, the high address in Data Memory is 0x85F Because we have 2048 (0x800) bytes of SRAM And the first 96 (0x60) bytes are assigned to Registers and IO Ports The address 0x085F is.EQU’d in m32def.inc as RAMEND 0x0000 to 0x005F 0x0060 to 0x085F Usable SRAM Mapped to Registers and IO

CS-280 Dr. Mark L. Hornick 8 The special register (SP) used to maintain the stack is called the Stack Pointer Like X, Y, and Z, the SP is a 16-bit register divided into two 8- bit registers If SP contains the address 0x085F: SPL contains the low byte of the address of the front of the stack (e.g. 0x5F) SPH contains the high byte (e.g. 0x08)......

CS-280 Dr. Mark L. Hornick 9 The SP registers are located in the IO address space In the IO address space SPL address is 0x3D SPH address is 0x3E These are.DEF’d in m32def.inc SP registers are set using the OUT instruction similar to how PORTB is written to ldi TEMP, LOW(RAMEND) out SPL,TEMP ldi TEMP,HIGH(RAMEND) outSPH, TEMP 0x085B 0x085C 0x085D 0x085E 0x085F SP

CS-280 Dr. Mark L. Hornick 10 The stack starts out with a size of 0 Data (bytes) are pushed onto the front of the stack The front of the stack grows “downward” towards lower memory address SP start Front of Stack grows downward 0x085B 0x085C 0x085D 0x085E 0x085F

CS-280 Dr. Mark L. Hornick 11 Using the Stack – pushing a byte onto the stack LDI R20, 5 ; load 5 to R20 PUSH R20 ; push 5 to stack INCR20 PUSHR20 ; push 6 to stack INCR20 PUSHR20 ; push 7 to stack SP (after 3) SP (after 2) SP (after 1) SP (before) SP is automatically decremented by each PUSH instruction 0x085B 0x085C 0x085D 0x085E 0x085F

CS-280 Dr. Mark L. Hornick 12 Using the Stack – pulling a byte from the stack POP R21 ; pull 7 from stack POPR22 ; pull 6 from stack POPR23 ; pull 5 from stack SP (before) SP (after 1) SP (after 2) SP (after 3) SP is automatically incremented by each POP instruction Although the SP is incremented, the values in Data Memory are not actually removed 0x085B 0x085C 0x085D 0x085E 0x085F

CS-280 Dr. Mark L. Hornick 13 OK, so how do you actually use the stack to do something useful? Demo

CS-280 Dr. Mark L. Hornick 14 Instructions for calling a subroutine RCALL ; relative call Like RJMP, where the call target address must be no more than +/- 2K from the call origin Operand can be a raw address (e.g. 0x0123) or a label that represents an address CALL ; long call Like JMP, where the call target address can be any address 0x0-0xFFFF ICALL; indirect call Call target address is contained indirectly in the Z register Z register can contain any address 0x0-0xFFFF The address of the next instruction following RCALL, CALL, or ICALL is automatically pushed onto the Stack

CS-280 Dr. Mark L. Hornick 15 The address of the next instruction following RCALL, CALL, or ICALL is automatically pushed onto the Stack 0x00 0x2C 0x002Aldir0,1 ; sample instruction 0x002BRCALLsub1 ; call subroutine 0x002CCLRr0 ; sample instruction SP (after RCALL) SP (before RCALL) 1. SP is automatically decremented by RCALL by 2 bytes (2-byte address) 2. The low byte is pushed first, followed by the high byte 3. Program execution jumps to the first instruction in sub1 0x085B 0x085C 0x085D 0x085E 0x085F

CS-280 Dr. Mark L. Hornick 16 Only one instruction is used for returning from a subroutine RET ; return from subroutine The return address (the address of the instruction that immediately followed the RCALL, CALL, or ICALL) is popped from the Stack and placed in the PC SP is pre-decremented during execution of RET RET is the ONLY way to return from a subroutine NEVER use RJMP or JMP to return SP will not get decremented without RET Never use JMP, RJMP, or IJMP to call a subroutine The Stack will not get incremented Use only CALL, RCALL, or ICALL

CS-280 Dr. Mark L. Hornick 17 When the RET statement in the subroutine is executed, the return address is popped from the Stack and placed in the PC 0x00 0x2C 0x002Aldir0,1 ; sample instruction 0x002BRCALLsub1 ; call subroutine 0x002CCLRr0 ; sample instruction SP (before RET) SP (after RET) SP is automatically incremented by RETby 2 bytes (2-byte address) The high byte is popped first, followed by the low byte Program execution resumes at address 0x002C after the RET from sub1 0x085B 0x085C 0x085D 0x085E 0x085F

18 Some more uses for the stack Short term data storage vs. regular Data Memory storage Easy to just PUSH and POP data on/off the stack vs. LD/ST from memory Preserving registers during calculations PUSH a register value onto the Stack to save it Do some other work involving that register POP the value off the Stack back into the register Preserving registers prior to a subroutine call PUSH register values onto the Stack to save them Call subroutine(s) that might use those registers POP the values off the Stack back into the registers after the subroutine call returns