User: alciro    User
 Original    Translate to:  Deutsch  English  Français  中文  
 

Microcontroladores 8051

5.4. Subroutine or subroutine

A subroutine or subroutine in a section separate program or part of the main program that can be called to perform a specific function. The subroutine may be required by the main program or another subroutine as many times as necessary. When you call a subroutine implementing the current program is stopped, the program counter PC (program counter) 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. The high level languages such as C, Basic subroutines are known under the name functions or procedures.

In the subroutines have to take into account the following considerations:

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

As good advice, it is recommended whenever possible division of subroutines or sub-program and minimize the content of the sentences in the main program. Above all, the subroutines are necessary when part of a program be executed multiple times. We will make the program easier and it takes up less space in the ROM.

If a subroutine is made up of few instructions, it may be advisable not to create it, since the call and return mechanism may make it slower execution instructions to place directly in the main program.

Subroutine or subroutine in sensamblador

CALL Instruction

Calling a subroutine. The program counter is replaced by the address where execution continues. The contents of the PC (next current position) is stored in the Stack.

Dir LCALL

1 º
(PC) = (PC) +3 Read the instruction and the program counter PC points to the next
2 º
(SP) = (SP) +1 Stack saved to the program counter PC is pointing to the next instruction
3 º
((SP)) = (PC 7-0)
4 º
(SP) = (SP) +1
5 º
((SP)) = (PC 15-8)
6 º
(PC) = 15-0 addreses Program control is transferred to the instruction whose address is specified in bytes 2 and 3 of the actual CALL instruction

RET instruction

Subroutine return. Stack is retrieved from the program counter PC and the program continues execution at the instruction following the subroutine call CALL.

RET

1 º
(PC 15-8) = ((SP)) It retrieves the program counter PC Memory Stack
2 º
(SP) = (SP) -1
3 º
(PC 7-0) = ((SP)) Program execution resumes at the next position to the call to the subroutine
4 º
(SP) = (SP) -1

Nesting of subroutines

Nesting Subroutines
Loading
copyright © 2007-2021  www.alciro.org  All rights reserved.         
Share |