2.4.1 Source Files
The assembler accepts, as input, a source file that consists of either MIPS- or ARM-based instructions, assembler directives and comments. A sample source file is shown below.
Note: Microchip Technology strongly suggests an
.S
extension for assembly source files. This will enable you
to easily use the C compiler driver without having to specify the option to tell the driver
that the file should be treated as an assembly file. The capitalized 'S' also indicates
that the source file should be preprocessed by the C preprocessor before being passed to
the assembler. See the relevant MPLAB XC32 compiler user's guide for more details on the C
compiler driver.Sample Assembler Code
Updated example code:
#include <xc.h>
#define IOPORT_BIT_7 (1 << 7)
.global main /* define all global symbols here */
.text
/* define which section (for example "text")
* does this portion of code resides in. Typically,
* all your code will reside in .text section as
* shown below.
*/
.set noreorder
/* This is important for an assembly programmer. This
* directive tells the assembler not to optimize
* the order of the instructions as well as not to insert
* 'nop' instructions after jumps and branches.
*/
/*********************************************************************
* main()
* This is where the PIC32 start-up code will jump to after initial
* set-up.
********************************************************************/
.ent main /* directive that marks symbol 'main' as function in the ELF
* output
*/
main:
/* Call function to clear bit relevant to pin 7 of port A.
* The 'jal' instruction places the return address in the $ra
* register.
*/
ori a0, $0, IOPORT_BIT_7
jal mPORTAClearBits
nop
/* endless loop */
endless:
j endless
nop
.end main /* directive that marks end of 'main' function and its
* size in the ELF output
*/
/*********************************************************************
* mPORTAClearBits(int bits)
* This function clears the specified bits of IOPORT A.
*
* pre-condition: $ra contains return address
* Input: Bit mask in $a0
* Output: none
* Side effect: clears bits in IOPORT A
********************************************************************/
.ent mPORTAClearBits
mPORTAClearBits:
/* function prologue - save registers used in this function
* on stack and adjust stack-pointer
*/
addiu sp, sp, -4
sw s0, 0(sp)
la s0, LATACLR
sw a0, 0(s0) /* clear specified bits */
/* function epilogue - restore registers used in this function
* from stack and adjust stack-pointer
*/
lw s0, 0(sp)
addiu sp, sp, 4
/* return to caller */
jr ra
nop
.end mPORTAClearBits