# Vault 7: Projects

This publication series is about specific projects related to the Vault 7 main publication.

### Protego

# MPLAB XC16 C-Compiler

- 1
- 2
…
- 201
- 202
- 203
- 204
- 205
…
- 365
- 366

MPLAB
®
XC16 C COMPILER
USERS GUIDE
 2012-2013 Microchip Technology Inc. DS50002071C-page 203
Chapter 15. Main, Runtime Startup and Reset
15.1 INTRODUCTION
When creating C code, there are elements that are required to ensure proper program
operation: a main function must be present; startup code to initialize and clear vari-
ables, to set up registers and the processor; and Reset conditions need to be handled.
The following topics are discussed in this section:
 The main Function
 Runtime Startup and Initialization
15.2 THE main FUNCTION
The identifier main is special. It is must be used as the name of a function that will be
the first function to execute in a program. You must always have one and only one func-
tion called main() in your programs. Code associated with main(), however, is not
the first code to execute after Reset. Additional code provided by the compiler and
known as the runtime startup code is executed first and is responsible for transferring
control to the main() function.
The prototype that should be used for main() is as follows.
int main(void);
15.3 RUNTIME STARTUP AND INITIALIZATION
A C program requires certain objects to be initialized and the processor to be in a
particular state before it can begin execution of its function main(). It is the job of the
runtime startup code to perform these tasks, specifically (and in no particular order):
 Initialization of global variables assigned a value when defined
 Initialization of the stack
 Clearing of non-initialized global variables
 General setup of registers or processor state
Two C run-time startup modules are included in the libpic30-omf.a archive/library.
The entry point for both startup modules is __reset. The linker scripts construct a
GOTO __reset instruction at location 0 in program memory, which transfers control
upon device Reset.
The primary startup module is linked by default and performs the following:
1. The Stack Pointer (W15) and Stack Pointer Limit register (SPLIM) are initialized,
using values provided by the linker or a custom linker script. For more
information, see Section 6.4 Stack.
2. If a .const section is defined, it is mapped into the program space visibility
window by initializing the PSV page and CORCON registers, as appropriate, if
const-in-code memory mode is used or variables have been explicitly
allocated to space(auto_psv).

- 1
- 2
…
- 201
- 202
- 203
- 204
- 205
…
- 365
- 366