07.04.2013 Views

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16-Bit C <strong>Compiler</strong> User’s Guide<br />

4.9 THE C STACK USAGE<br />

The C compiler uses the software stack to:<br />

• Allocate automatic variables<br />

• Pass arguments to functions<br />

• Save the processor status in interrupt functions<br />

• Save function return address<br />

• Store temporary results<br />

• Save registers across function calls<br />

The run-time stack grows upward from lower addresses to higher addresses. The<br />

compiler uses two working registers to manage the stack:<br />

• W15 – This is the Stack Pointer (SP). It points to the top of stack which is defined<br />

to be the first unused location on the stack.<br />

• W14 – This is the Frame Pointer (FP). It points to the current function’s frame.<br />

Each function, if required, creates a new frame at the top of the stack from which<br />

automatic <strong>and</strong> temporary variables are allocated. The compiler option<br />

-fomit-frame-pointer can be used to restrict the use of the FP.<br />

FIGURE 4-1: STACK AND FRAME POINTERS<br />

Stack grows<br />

toward<br />

greater<br />

addresses<br />

Function Frame<br />

SP (W15)<br />

FP (W14)<br />

The C run-time startup modules (crt0.o <strong>and</strong> crt1.o in libpic30.a) initialize the<br />

Stack Pointer W15 to point to the bottom of the stack <strong>and</strong> initialize the Stack Pointer<br />

Limit register to point to the top of the stack. The stack grows up <strong>and</strong> if it should grow<br />

beyond the value in the Stack Pointer Limit register, then a stack error trap will be taken.<br />

The user may initialize the Stack Pointer Limit register to further restrict stack growth.<br />

The following diagrams illustrate the steps involved in calling a function. Executing a<br />

CALL or RCALL instruction pushes the return address onto the software stack. See<br />

Figure 4-2.<br />

DS51284H-page 70 © 2008 <strong>Microchip</strong> Technology Inc.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!