Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subroutines and the Stack

Similar presentations


Presentation on theme: "Subroutines and the Stack"— Presentation transcript:

1 Subroutines and the Stack
Source:

2 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

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

4 Operation during execution of LCALL
LCALL add16 (long call to subroutine) machine cod: aaaaaaaa aaaaaaaa (3-byte instruction) A15 ~ A8 A7 ~ A0 (PC) <- (PC) ; 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:

7 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

8 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

9 Stack Right before executing LCALL sub 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 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, R ; copy the size of the set into the B register CLR A ; clear the ACC loop: ADD ; add to the ACC the data in the location pointed to by R0 INC R ; 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

11 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 AR MOV B, R1; copy the size of the set into the B register CLR A ; clear the ACC loop: ADD 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

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. 0C 0B 0A 09 08 Bottom of the stack SP  07

13


Download ppt "Subroutines and the Stack"

Similar presentations


Ads by Google