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.

Slides:



Advertisements
Similar presentations
Embedded Software 1. General 8051 features (excluding I/O) CPU 8 bit microcontroller The basic registers include (more discussed later) The 8-bit A (accumulator)
Advertisements

Programming 8086 – Part IV Stacks, Macros
Suranaree University Of Technology มทส  2002 Anant Oonsivilai 2002/2/27 Microcomputers and Microprocessors Chapter Assembly Language Programming.
Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
CPU Review and Programming Models CT101 – Computing Systems.
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
There are two types of addressing schemes:
1 Chapter 3 Jump, Loop, and Call Instructions. 2 Sections 3.1 Loop and Jump Instructions 3.2 Call Instructions 3.3 Time Delay Generation and Calculation.
Msc. Ivan A. Escobar Broitman Microprocessors 1 1 The 8051 Instruction Set.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Lecture 6 Machine Code: How the CPU is programmed.
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.
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
8051 ASSEMBLY LANGUAGE PROGRAMMING
Microcontroller Intel 8051
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
CoE3DJ4 Digital Systems Design Chapter 3: instruction set summary.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
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.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
The 8051 Microcontroller and Embedded Systems
Today’s Lecture Unconditional branch instruction
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Lecture Set 4 Programming the 8051.
Functions/Methods in Assembly
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Interrupt I/O (again!) Arithmetic using a stack.
JUMP, LOOP, AND CALL INSTRUCTIONS
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.
8085 INTERNAL ARCHITECTURE.  Upon completing this topic, you should be able to: State all the register available in the 8085 microprocessor and explain.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
1 Contents: 3.1 Instruction format and Addressing Modes 3.2 Instruction Introduction Chapter 3 Instruction system.
Instruction set Architecture
Figure 8.1 of course package
ECE 3430 – Intro to Microcomputer Systems
The Stack.
© Craig Zilles (adapted from slides by Howard Huang)
Lecture Set 5 The 8051 Instruction Set.
Subroutines and the Stack
Chapter 10 And, Finally... The Stack
8051 Addressing Modes The way, using which the data source or destination addresses are specified in the instruction mnemonic for moving the data, is.
Data Processing Instructions
(The Stack and Procedures)
Chapter 3 Addressing Modes
Chapter 10 The Stack.
Arithmetic using a stack
CS 301 Fall 2002 Control Structures
SCHOOL OF ELECTRONICS ENGINEERING Electronics and Communication
Subroutine Call; Stack
Branching Instructions
Introduction to Micro Controllers & Embedded System Design Addressing Mode Department of Electrical & Computer Engineering Missouri University of Science.
MIPS Instructions.
Subroutines and the Stack
(The Stack and Procedures)
Chapter 10 And, Finally... The Stack
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
8051 ASSEMBLY LANGUAGE PROGRAMMING
Subroutines and the Stack
Some Assembly (Part 2) set.html.
Where is all the knowledge we lost with information? T. S. Eliot
(The Stack and Procedures)
Computer Organization and Assembly Language
© Craig Zilles (adapted from slides by Howard Huang)
Computer Operation 6/22/2019.
(The Stack and Procedures)
Presentation transcript:

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 addresses  The stack operates in a last-in first-out (LIFO) manner  This means that access to the stack is in a restricted manner, whereby new data is added to the top of the stack and data is removed from the top also  While it appear that such a structure for storing and retrieving data seems limited, the stack actually makes handling of certain programming tasks very easy  See important uses for stack soon

Stack  Think of a stack of plates  It is natural to add a plate to the top of stack  It is natural to remove a plate from the top also  The terminology used for stacks  Push  Meaning add to of stack  Pop  Meaning remove from top of stack The terminology associated with stacks comes from the spring loaded plate containers common in dining halls. When a plate is washed it is pushed on the stack. When someone needs food, a clean plate is popped off the stack.

Stack  Data is pushed on top and poped from the top of a stack  To access a stack in memory, the address of the top of the stack is needed  Processers have a special register called a S tack P ointer (SP) to store the address of the stack top  Actually processor designers differ in how this is handled.  Some processors have a stack pointer that points to the last data pushed on the stack, while others point to the next available location!

Stack: Example operation

Stack: 8051 Details  8051 has a SP reg  8 bit register in the SFR block (address  Used to address (indirectly) a location in internal data memory  Max of 256 locations  Initialised to address 07H on power up  Can be changed if needed  Stack grows with increasing memory addresses  The two instructions are PUSH direct  Operates by firstly incrementing the contents of the SP and then storing the value addressed by direct at the address in SP POP direct  Operates by copying the byte pointed to by SP into the location specified by direct. The contents of SP is then decremented by 1

8051 Programmer’s Model Note: The SP reg is shown twice here!

Example  Assume SP contains 07H, A contains 3AH and address 40H contains 00H  What does the stack look like after these instructions? PUSH ACC INC A PUSH ACC POP 39H PUSH 40H ADD A,#05H PUSH ACC POP 40H POP ACC

Example  The A, B and PSW registers are required to perform some calculations, but contain values that are needed later. Use the stack to solve the problem  Solution PUSH ACC; Store on Stack. PUSH B PUSH PSW ….; Do calculations ….; here. POP PSW; Restore from stack. POP B; Note order of instructions, POP ACC; this is important!!

Subroutines

 Subroutines provide a mechanisn to reuse a piece of code  They also simplify the design and coding process, allowing the programmer to partition and abstract subtasks  Subroutines also makes testing and debugging easier

Subroutines: ACALL and LCALL  Just as there was AJMP and LJMP, similarly there is ACALL and LCALL. The assembler will select the approapiate call instruction if the CALL instruction is used (see earlier lectures)  The CALL instruction is  CALL addr  Operates by saving the address of the next instruction on the stack and transferring program control to the subroutine code starting at addr  By saving the address of the next instruction, it is possible to resume execution of the next instruction when the subroutine is finished

Subroutines: Example MOV P1,10H MOV A,90H mainXRL P1,A CALL Delay JMP main Delay: ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1:MOV R6,#250 ; 250*2us=500us dy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay) RET  When the instruction CALL Delay is executed,  The address of the next instruction is pushed onto the stack automatically  2 address bytes  The PC is loaded with the address of the Delay subroutine  The next instruction to be fetched will then be the first instruction in the Delay subroutine

Subroutines: RET MOV P1,10H MOV A,90H Main:XRL P1,A CALL Delay JMP main Delay: ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1:MOV R6,#250 ; 250*2us=500us dy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay) RET  How does the processor know when to return from the subroutine?  When a RET instruction is executed  The instruction is RET  It has no operands  The execution of this instruction causes the PC to be reloaded with two bytes (16 bits) that are popped off the stack

Example: Simulation

Subroutines  Always make sure to include a RET instruction in the subroutine  What are the consequences of not doing so?  Because the stack is used, make sure that it is not corrupted between calling the subroutine and returning  Can still use PUSH and POP instructions  See next example

Subroutines  When subroutines are called, care must be taken not to corrupt resources that are used by the calling code  Calling code refers to code from which subroutine called from  Called code refers to subroutine code itself  A good way to address this issue is to use the stack  Consider the Delay subroutine example.  This uses R6 and R7. What if the called function used these registers

Subroutines MOV P1,10H MOV A,90H Main:XRL P1,A CALL Delay JMP main Delay: PUSH 07 PUSH 06 ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1:MOV R6,#250 ; 250*2us=500us dy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay) POP 06 POP 07 RET  By storing the values of R6 and R7 on the stack at start of subroutine and restoring the original values from the stack at the end of the routine, it is now possible to use the delay routine, even when it is called from a section of code that uses R6 or R7 for a different purpose.  This really makes the Delay routine very usable.  Assumes Bank 0 used

Subroutines  Subroutines often require data to be passed in/out  There are 2 standard approaches  Both are flexible in that data does not have to be located at fixed addresses in memory  The two approaches  Through registers >Limited no. of registers >Fast  Using the stack >More space available >Allows re-entrant operation (recursive calls)

Examples  Write a delay routine that can provide a delay of between 100ms and 10s, in increments of 100ms.  Use both approaches for passing data

Exercises  What is instruction encoding for each of the following? PUSH 41H POP 41H 