1 / 13

Subroutines and the Stack

Learn about the different instruction types (ACALL, LCALL, JMP, and CALL) in the 8051 microcontroller, how they work, and their advantages. Understand the concepts of subroutines and the stack in programming.

alyciat
Download Presentation

Subroutines and the Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Acall & Lcall ACALL allows you to jump to a subroutine within the same 2K page. LCALLallows 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.

  3. JMP and CALL are not 8051 instructions.

  4. Operation during execution of LCALL (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.

  5. 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.

  6. 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/

  7. PUSH during CALL and POP during RET

  8. CALL to PUSH, RET to POP PUSH POP

  9. Stack Right before executing LCALLsub While entering the subroutine

  10. A program example (to show the advantage of the indirect address mode) Example - Getting the Average of a Set of Numbers

  11. Saving the program status before executing the subroutine

  12. 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.

More Related