Subroutine in 8051 microcontroller

21
Subroutine in 8051 Microcontroller By, Bhadresh Savani (140110120046) Ronak Sejani (140110120047) Aashiv Shah (140110120048) Guided by, B. R. Hindocha

Transcript of Subroutine in 8051 microcontroller

Subroutine in 8051 Microcontroller

By, Bhadresh Savani(140110120046)

Ronak Sejani (140110120047)

Aashiv Shah (140110120048)

Guided by, B. R. Hindocha

What is Subroutine?

Subroutine is a group of instructions Written separately from the main program to perform a function that occurs repeatedly in the main program.

When you call a subroutine implementation of the current program is stopped, the program counter PC is loaded with the memory location of the subroutine, running up to the RET instruction (end of subroutine), where produce a return to the main program resumes running.

Subroutines...

• Perform specific functions and are not operating on their own.• Are always linked to a major program or other subroutines.• Can be called many times as necessary as it reduces the code the

program to have the effect of code reuse.• Allow the division of program blocks as perform the function of

structure. Providing greater visibility and understanding of it.

INSTRUCTIONS of Subroutine in 8051 Microcontroller

ACALL

LCALL

RET

RETI

ACALL INSTUCTION

Function: Absolute Call Within 2K Block

Syntax: ACALL code address

Description: ACALL unconditionally calls a subroutine at the indicated code address. ACALL pushes the address of the instruction that follows ACALL onto the stack, least-significant-byte first, most-significant-byte second. The Program Counter is then updated so that program execution continues at the indicated address.

The new value for the Program Counter is calculated by replacing the least-significant-byte of the Program Counter with the second byte of the ACALL instruction, and replacing bits 0-2 of the most-significant-byte of the Program Counter with 3 bits that indicate the page. Bits 3-7 of the most-significant-byte of the Program Counter remain unchanged.

Since only 11 bits of the Program Counter are affected by ACALL, calls may only be made to routines located within the same 2k block as the first byte that follows ACALL.

Instructions OpCode Bytes Cycles Flags

ACALL page0 0x11 2 2 None

ACALL page1 0x31 2 2 None

ACALL page2 0x51 2 2 None

ACALL page3 0x71 2 2 None

ACALL page4 0x91 2 2 None

ACALL page5 0xB1 2 2 None

ACALL page6 0xD1 2 2 None

ACALL page7 0xF1 2 2 None

LCALL INSTRUCTION

Function: Long Call

Syntax: LCALL code addr

Description: LCALL calls a program subroutine. LCALL increments the program counter by 3 (to point to the instruction following LCALL) and pushes that value onto the stack (low byte first, high byte second). The Program Counter is then set to the 16-bit value which follows the LCALL opcode, causing program execution to continue at that address.

Instructions OpCode Bytes Cycles Flags

LCALL 0x12 3 2 None

Difference B/W ACALL and LCALL

ACALL stands for absolute call while LCALL stands for long call.

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.

RET INSTRUCTION

Function: Return From Subroutine

Syntax: RET

Description: RET is used to return from a subroutine previously called by LCALL or ACALL. Program execution continues at the address that is calculated by popping the topmost 2 bytes off the stack. The most-significant-byte is popped off the stack first, followed by the least-significant-byte.

Instructions OpCode Bytes Cycles Flags

RET 0x22 1 2 None

RETI instruction

Function: Return from Interrupt

Syntax: RETI

Description:The RETI instruction is used to end an interrupt service routine. This instruction pops the high-order and low-order bytes of the PC (and decrements the stack pointer by 2) and restores the interrupt logic to accept additional interrupts. No other registers are affected by this instruction.

The RETI instruction does not restore the PSW to its value before the interrupt. The interrupt service routine must save and restore the PSW.

Execution returns to the instruction immediately after the point at which the interrupt was detected. If another interrupt was pending when the RETI instruction is executed, one instruction at the return address is executed before the pending interrupt is processed.

Instructions OpCode Bytes Cycles Flags

RETI 0x32 1 2 None

Difference B/W RET and RETI

RETI releases the interrupts of the same level of priority than the interrupt that has caused the branch, and RET doesn't.

RET stands for Return from Stack.

RETI stands for Return from Stack of Interrupt.

ADVANTAGE OF SUBROUTINE

Minimize the content of the sentences in the main program.

Dividing a large programming task among various programmers, or various stages of a project.

Improving Traceability.

Make the program easier and it takes up less space in the ROM.

Disadvantages of SUBROUTINE

Invoking a subroutine (versus using inline code) imposes some computational overhead in the call mechanism.

The subroutine typically requires standard housekeeping code – both at entry to, and exit from, the function.

Resources

1. http://www.alciro.org/alciro/microcontroladores-8051_24/subrutina-subprograma_357_en.htm

2. https://en.wikipedia.org/wiki/Subroutine

3. http://www.edsim51.com/8051Notes/8051/subroutines.html

4. http://www.hobbyprojects.com/8051_tutorial/acall_absolute_call.html

Thank you!!