Register Conventions

Although most of the registers in the PIC32MX architecture are designated as General Purpose Registers, there are some recommended uses of the registers for correct software operation with high-level languages such as the Microchip C compiler.

Table 1: Register Conventions

CPU
Register
Symbolic
Register
Usage
r0 zero Always 0 (note 1)
r1 at Assembler Temporary
r2 - r3 v0-v1 Function Return Values
r4 - r7 a0-a3 Function Arguments
r8 - r15 t0-t7 Temporary – Caller does not need to preserve contents
r16 - r23 s0-s7 Saved Temporary – Caller must preserve contents
r24 - r25 t8 - t9 Temporary – Caller does not need to preserve contents
r26 - r27 k0 - k1 Kernel temporary – Used for interrupt and exception handling
r28 gp Global Pointer – Used for fast-access common data
r29 sp Stack Pointer – Software stack
r30 s8 or fp Saved Temporary – Caller must preserve contents OR
Frame Pointer – Pointer to procedure frame on stack
r31 ra Return Address (note 1)

Note 1: Hardware enforced, not just convention

CPU Special Purpose Registers

The CPU contains three special purpose registers:

- During a multiply operation, the HI and LO registers store the product of integer multiply.

- During a multiply-add or multiply-subtract operation, the HI and LO registers store the result of the integer multiply-add or multiply-subtract.

- During a division, the HI and LO registers store the quotient (in LO) and remainder (in HI) of integer divide.

- During a multiply-accumulate, the HI and LO registers store the accumulated result of the operation.

Stack Usage

The MPLAB C32 C compiler dedicates general purpose register 29 as the software Stack Pointer. All processor stack operations, including function call, interrupts and exceptions use the software stack. The stack grows downward from high addresses to low addresses.

By default, the size of the stack is 1024 bytes. The size of the stack may be changed by specifying the size on the linker command line using the --defsym_min_stack_size linker command line option. An example of allocating a stack of 2048 bytes using the command line is:

pic32-gcc foo.c -Wl,--defsym,_min_stack_size=2048

The runtime stack grows downward from higher addresses to lower addresses (see Figure 1). The compiler uses two working registers to manage the stack:

Heap Usage

The C runtime heap is an uninitialized area of data memory that is used for dynamic memory allocation using the standard C library dynamic memory management functions, calloc, malloc and realloc. If you do not use any of these functions, then you do not need to allocate a heap. By default, a heap is not created.

If you do want to use dynamic memory allocation, either directly, by calling one of the memory allocation functions, or indirectly, by using a standard C library function that uses one of these functions, then a heap must be created. A heap is created by specifying its size on the linker command line using the --defsym_min_heap_size linker command line option. An example of allocating a heap of 512 bytes using the command line is:

pic32-gcc foo.c -Wl,--defsym,_min_heap_size=512

The linker allocates the heap immediately before the stack.

Function Calling Convention

The Stack Pointer is always aligned on a 4-byte boundary.



Maintained by John Loomis, last updated 4 August 2008