Subroutine Call and Return Instructions 8051 Micro-controller by Ravinder Nath Rajotiya - April 23, 2019June 10, 20190 Share on Facebook Share Send email Mail Print Print Branch Type Instructions These are of two types: Subroutine CALL Instruction JMP Instruction Unconditional JMP Conditional JMP Subroutine Call Instructions: These instructions are used to calls a subroutine located at the specified address. There are two subroutine Call instructions, theses are ACAL and LCALL instruction Table of Contents Toggle ACALL InstructionLCALLRET ACALL Instruction Size : Two bytes Requirement : Subroutine that is called must be located in the same 2KByte block of program memory as the opcode following the ACALLinstruction. Operation: PC is first incemented twice to obtain the address of the following instruction PC is saved on stack low-byte first and high byte at high address. SP increased by two. Calcultes the subroutine address as: by combining the 5 high-order bits of the incremented PC (for A15-A11), the 3 high-order bits of the ACALLinstruction opcode (for A10-A8), and the second byte of the instruction (for A7-A0) Format Opcode/ Encoding 2nd Byte of Op-Code example / Operation Flags affected ACALL addr11 A10-810001 A7-0 ACALL label PC=PC+2 SP=SP+1 (SP)=PC7-0 SP=SP+1 (SP)=PC15-8 PC10-0=A10-0 P Example: Logical Address Encoding Label ORG 0 0 74 55h MOV A, #55h A=55h 2 F5 80h BACK: MOV P1, A P1=A 4 11 09h ACALL DELAY A10-0=09; so A10-8=000 encoding of ACALL= 00010001 Call location = A7-0=09 6 F4h CPL A Complement A 7 80 02h SJMP Back Jump within 256 location 9 7D FFh Delay: MOV R5, #0FF R5=FFh=256 B D5 0B Again: DJNZ R5, Again R5=R5-1; If R50; PC=PC+offset i.e 0 PC=PC+0=PC Else PC=PC+2 D RET END LCALL The LCALL instruction calls a subroutine located at the specified address. This instruction first adds 3 to the PC to generate the address of the next instruction. This result is pushed onto the stack low-byte first and the stack pointer is incremented by 2. The high-order and low-order bytes of the PC are loaded from the second and third bytes of the instruction respectively. Program execution is transferred to the subroutine at this address. No flags are affected by this instruction. format example Operation Opcode/ Encoding Flags affected Byte Cycles LCALL addr16 LCALL sub1 PC=PC+2 SP=SP+1 (SP)=PC[7-0] SP=SP+1 (SP)=PC[15-8] PC=addr16 00010010, A15-8, A7-0 none 3 2 Example Subroutine Long Call ORG 0 MAIN : – – LCALL subrtn_1 LCALL subrtn_2 LCALL subrtn_3 – – HERE: SJMP HERE ; subrt_1: — RET ; end of sub routine_1 subrtn_2: — RET ; end of sub routine_2 subrtn_3: — RET ; end of sub routine_3 END RET It is used to return from a subroutine POPs the saved address from stack and load in PC. Twice read, each time stack pointer decremented Program execution resumes from the resulting address which is typically the instruction following an ACALL or LCALL instruction. Format Opcode/ Encoding 2nd byte Example/ Operation Flags affected RET 00100010 — RET PC15-8=(SP) SP=SP-1 PC7-0=(SP) SP=SP-1 none Share on Facebook Share Send email Mail Print Print