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 while 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 and 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) (PC) <- (PC) + 3 (SP) <- (SP) + 1 ((SP)) <- (PC7-PC0) (SP) <- (SP) + 1 ((SP)) <- (PC15 - PC8) (PC) <- add15 - add0 On system reset the SP is initialised 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 Right before executing LCALL sub
While entering the subroutine

7 A program example: average
Example - Getting the Average of a Set of Numbers 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; initialise 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: 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 RET; return from subroutine

8 Saving the Controller Status
;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; initialise 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 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

9

10

11

12


Download ppt "Subroutines and the Stack"

Similar presentations


Ads by Google