130 likes | 137 Views
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.
E N D
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. 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.
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.
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/
CALL to PUSH, RET to POP PUSH POP
Stack Right before executing LCALLsub 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
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.