Microchip Technology SW006021-2 Manuel D’Utilisation
C Language Features
2012 Microchip Technology Inc.
DS52053B-page 187
5.8.7
Function Return Values
Function return values are passed to the calling function using either the W register, or
the function’s parameter memory in its auto-parameter block. Having return values also
located in the same memory as that used by the parameters can reduce the code size
for functions that return a modified copy of their parameter.
the function’s parameter memory in its auto-parameter block. Having return values also
located in the same memory as that used by the parameters can reduce the code size
for functions that return a modified copy of their parameter.
Eight-bit values are returned from a function in the W register. Values larger than a byte
are returned in the function’s parameter memory area, with the least significant word
(lsw) in the lowest memory location.
are returned in the function’s parameter memory area, with the least significant word
(lsw) in the lowest memory location.
For example, the function:
int return_16(void)
{
return 0x1234;
}
will exit with the code similar to:
MOVLW
34h
MOVWF
(?_return_16)
MOVLW
12h
MOVWF
(?_return_16)+1
RETURN
For PIC18 targets returning values greater than 4 bytes in size, the address of the
parameter area is also placed in the FSR0 register.
parameter area is also placed in the FSR0 register.
5.8.8
Calling Functions
All 8-bit devices use a hardware stack for function return addresses. The depth of this
stack varies from device to device.
stack varies from device to device.
Typically, CALL assembly instructions are used to transfer control to a C function when
it is called. Each function calls uses one level of stack. This stack level is freed after the
called routine executes a RETURN instruction. The stack usage grows if a called func-
tion calls another before returning. If the hardware stack overflows, function return
addresses will be destroyed and the code will eventually fail.
it is called. Each function calls uses one level of stack. This stack level is freed after the
called routine executes a RETURN instruction. The stack usage grows if a called func-
tion calls another before returning. If the hardware stack overflows, function return
addresses will be destroyed and the code will eventually fail.
The stackcall suboption to the --RUNTIME option controls how the compiler
behaves when the compiler detects that the hardware stack is about to overflow due to
too many nested calls. See Section 4.8.50 “--RUNTIME: Specify Runtime
Environment” for details on this option. If this suboption is disabled (the default state),
where the depth of the stack will be exceeded by a call, the compiler will issue a
warning to indicate that this is the case. For PIC18 devices, this is the only way in which
calls are made, but for other 8-bit devices, the compiler can swap to an alternate way
of making calls, as detailed below.
behaves when the compiler detects that the hardware stack is about to overflow due to
too many nested calls. See Section 4.8.50 “--RUNTIME: Specify Runtime
Environment” for details on this option. If this suboption is disabled (the default state),
where the depth of the stack will be exceeded by a call, the compiler will issue a
warning to indicate that this is the case. For PIC18 devices, this is the only way in which
calls are made, but for other 8-bit devices, the compiler can swap to an alternate way
of making calls, as detailed below.
If the stackcall suboption is enabled, the compiler will, instead of issuing a warning,
automatically swap to using a method that involves the use of a lookup table and which
does not require use of the hardware stack. This feature is not available for PIC18
devices.
automatically swap to using a method that involves the use of a lookup table and which
does not require use of the hardware stack. This feature is not available for PIC18
devices.
When the lookup method is being employed, a function is reached by a jump (not a call)
directly to its address. Before this is done the address of a special “return” instruction
(implemented as a jump instruction) is stored in a temporary location inside the called
function. This return instruction will be able to return control back to the calling function.
directly to its address. Before this is done the address of a special “return” instruction
(implemented as a jump instruction) is stored in a temporary location inside the called
function. This return instruction will be able to return control back to the calling function.
This means of calling functions allows functions to be nested deeply without overflow-
ing the limited stack available on baseline and mid-range devices; however, it does
come at the expense of memory and program speed.
ing the limited stack available on baseline and mid-range devices; however, it does
come at the expense of memory and program speed.