Subroutines and the Stack

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

Week4. Program Branching  From what we have covered so far, our assembly programs execute one instruction after another in sequence  Programs that solve.
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.
Chapter 3 INSTRUCTION SET SUMMARY
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.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Msc. Ivan A. Escobar Broitman Microprocessors 1 1 The 8051 Instruction Set.
Lecture 6 Machine Code: How the CPU is programmed.
8051 ASSEMBLY LANGUAGE PROGRAMMING
Microcontroller Intel 8051
MICROCONTROLLER INSTRUCTION SET
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
CoE3DJ4 Digital Systems Design Chapter 3: instruction set summary.
8051 Programming (Addressing Mode-Instruction Set) Lec note 5
Prof. Cherrice TraverEE/CS-152: Microprocessors and Microcontrollers The 8051 Assembly Language.
CIT 673 Created by Suriyong1 MCS51 ASSEMBLY Language Resources
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
The 8051 Assembly Language Branching & Subroutines
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
8051 Micro controller. Architecture of 8051 Features of 8051.
Prof. Cherrice TraverEE/CS-152: Microprocessors and Microcontrollers The 8051 Assembly Language.
The 8051 Microcontroller and Embedded Systems
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
Lecture Set 4 Programming the 8051.
The 8051 Assembly Language. Overview Data transfer instructions Addressing modes Data processing (arithmetic and logic) Program flow instructions.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
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.
1 Contents: 3.1 Instruction format and Addressing Modes 3.2 Instruction Introduction Chapter 3 Instruction system.
Microprocessors I 8051 Addressing Modes CS Prof. Msc. Ivan A. Escobar
CHAPTER ADDRESSING MODES.
Instruction set Architecture
Figure 8.1 of course package
ECE 3430 – Intro to Microcomputer Systems
Classification of Instruction Set of 8051
Microprocessor T. Y. B. Sc..
The Stack.
3.Instruction Set of 8085 Consists of 74 operation codes, e.g. MOV
Lecture Set 5 The 8051 Instruction Set.
Subroutines and the Stack
ECE,JYOTHI ENGG COLLEGE
8051 Addressing Modes The way, using which the data source or destination addresses are specified in the instruction mnemonic for moving the data, is.
Microcomputer Programming
Data Processing Instructions
Computer Organization and Assembly Language (COAL)
CS 301 Fall 2002 Control Structures
SCHOOL OF ELECTRONICS ENGINEERING Electronics and Communication
8051 Single Board Computer (SBC) Version 1.0
Subroutine Call; Stack
Timer.
Branching Instructions
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
Introduction to Micro Controllers & Embedded System Design Addressing Mode Department of Electrical & Computer Engineering Missouri University of Science.
Chapter 8 Central Processing Unit
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
by Richard P. Paul, 2nd edition, 2000.
DMT 245 Introduction to Microcontroller
Figure 8.1 of course package
Introduction to Micro Controllers & Embedded System Design
Chapter 10 And, Finally... The Stack
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
8051 ASSEMBLY LANGUAGE PROGRAMMING
Subroutines and the Stack
Some Assembly (Part 2) set.html.
Addressing Modes in 8051 MC S. Lourduraj Asst. Prof. of Physics
Computer Operation 6/22/2019.
Computer Organization
Presentation transcript:

Subroutines and the Stack Source: http://www.edsim51.com/8051Notes/8051/subroutines.html

Acall & Lcall ACALL allows you to jump to a subroutine within the same 2K page. LCALL allows you to jump to a subroutine anywhere in the 64K code space. The advantage of ACALL over LCALL is that it is a 2-byte instruction while LCALL is a 3-byte instruction. Calling range Instruction length ACALL (Absolute call) Within the 2 K page. 2 bytes LCALL (Long call) Anywhere in the 64K code space 3 bytes

JMP and CALL are not 8051 instructions. In source program By the assembler CALL JMP CALL is replaced by ACALL. JMP is replaced by either SJMP or AJMP. LCALL LJMP LCALL and LJMP must be programmed explicitly.

Operation during execution of LCALL LCALL add16 (long call to subroutine) machine cod: 00010010 aaaaaaaa aaaaaaaa (3-byte instruction) 00010010 A15 ~ A8 A7 ~ A0 (PC) <- (PC) + 3 ; PC points to the next instruction following the CALL (SP) <- (SP) + 1 ((SP)) <- (PC7-PC0) (SP) <- (SP) + 1 ((SP)) <- (PC15 - PC8) (PC) <- add15 - add0 ; Go to the called subroutine On system reset, the SP is initialized with the value 07H. Therefore, the first item pushed onto the stack will be stored in location 08H.

RET instruction RET (Return from subroutine) Machine code: 0010 0010 (PC15 - PC8)< - ((SP)) (SP) <- (SP) - 1 (PC7 - PC0) <- ((SP)) (SP) <- (SP) - 1 The processor needs some way of knowing where to jump back to once execution of the subroutine is complete.

What is a stack? A stack is a storage mechanism with the first-in-last-out (FILO) or last-in-first-out (LIFO) access scheme. Source: https://www.differencebtw.com/difference-between-queue-and-stack/

PUSH during CALL and POP during RET Addreess Instruction Time slot PC SP Stack 1233H ORG 1233H Main: DIV AB 1 1234H 07H Empty LCALL SUBR 2 1237H 5678H 09H MUL AB 6 1238H 5677H ORG 5677H CLR A SUBR: SETB C 3 5679H CLR C 4 567AH RET 5 567BH CPL A

CALL to PUSH, RET to POP After CALL but before executing the subroutine After starting to execute the subroutine, but before RET After RET Program counter Stack pointer PC15 ~ PC08 PC07 ~ PC00 PC: aaaaaaaa bbbbbbbb PC15 ~ PC08 PC07 ~ PC00 PC: uuuuuuuu vvvvvvvv PC15 ~ PC08 PC07 ~ PC00 PC: aaaaaaaa bbbbbbbb SP  aaaaaaaa bbbbbbbb xxxxxxxx PUSH POP SP  xxxxxxxx SP  xxxxxxxx

Stack Right before executing LCALL sub While entering the subroutine

A program example (to show the advantage of the indirect address mode) Example - Getting the Average of a Set of Numbers Use the direct address mode Use the indirect address mode (to avoid having to add an instruction for each number to be added) average: MOV A, 30H ADD A, 31H ADD A, 32H ADD A, 33H ADD A, 34H MOV B, #05H DIV AB MOV 20H, A MOV 21H, B RET ; The main program MOV R0, #30H ; initialize the subroutine by putting the start address into R0 MOV R1, #05H ; and by putting the size of the set into R1 CALL average   ; The subroutine MOV B, R1 ; copy the size of the set into the B register CLR A ; clear the ACC loop: ADD A, @R0 ; add to the ACC the data in the location pointed to by R0 INC R0 ; increment R0 so that it points to the next memory location DJNZ R1, loop ; decrement R1 and if it is still not zero jump back to loop DIV AB ; once all the numbers have been added together, divide them by the size of the set, which is stored in B RET ; return from subroutine

Saving the program status before executing the subroutine ;the main program using 0 ; assembler directive that indicates to the assembler which register bank is being used (in this case bank 0) MOV R0, #30H; initialize the subroutine by putting the start address into R0 MOV R1, #05H; and by putting the size of the set into R1 CALL average   ;the subroutine average: PUSH PSW PUSH AR0 PUSH AR1 MOV B, R1; copy the size of the set into the B register CLR A ; clear the ACC loop: ADD A, @R0; add to the ACC the data in the location pointed to by R0 INC R0; increment R0 so that it points to the next memory location DJNZ R1, loop; decrement R1 and if it is still not zero jump back to loop DIV AB; once all the numbers have been added together, divide them by the size of the set, which is stored in B POP R1 POP R0 POP PSW RET; return from subroutine

Initial state of the stack On system reset, the SP is initialized with the value 07H. Therefore, the first item pushed onto the stack will be stored in location 08H. Note: SP points to the top of the stack. 0C 0B 0A 09 08 Bottom of the stack SP  07