Electronic – How to prevent MPLABX and XC16 from polluting the assembly program on 16-bit PIC24

microcontrollermplabxpic

I recently started programming PIC microcontrollers after AVR and I wrote a small, do-nothing program just to see what the assembler produces in the HEX file:

;File:  main.s

.include    "p24FJ64GA202.inc"
.global     __reset
.global     __INT1Interrupt
.text

__reset:
    nop
    goto    infinite

__INT1Interrupt:
    nop
    retfie

infinite:
    nop
    nop
    nop
    bra     infinite

.end

After building and disassembling, I found that the assembler puts these unnecessary blocks in my code, that I did not write:

Unnecessary code block #1

and

Unnecessary code block #2

How can I prevent this from happening?

Best Answer

Those routines form part of the crt0, the system initialization routines. They are standard routines. Briefly they:

  • Erase the empty memory to a default of 0 throughout
  • Copy the pre-defined data from Flash into RAM

Without those routines the basic C system won't function. They are required for any program written in C.

I know your program isn't written in C, but the whole environment is C. You're using a C compiler, for instance.

Because of that you get the C routines.

You can try adding the linker option -nostartfiles to prevent the inclusion of the crt0. You can also use -nostdlib and -nodefaultlibs to prevent inclusion of standard library functions.

Note that this will completely break compilation of any C files.

Alternatively, you can keep the start files in there and instead of executing your program starting at the reset vector, define a "main" function in assembly which the crt0 will call for you after the system initialization has taken place.

By the way - XC16 is based on GCC 4.5.1.