XC16 assembly -- extra instructions in hex file

Thread Starter

geekoftheweek

Joined Oct 6, 2013
1,429
It's been over eight years since my only PIC24 project and things have changed a bit since then.

I am using MPlabX to create an assembly program for a 24K32KA302. It took a bit of reading and digging to come up with a basic program as it is a bit different from MPASM which I thought I used last time. So far it compiles and the hex file looks about right. What I did find is it looks like XC16 will add certain "standard" code which I am curious if anyone knows what it is. I plan to program a disassembler down the road some time to figure it out, but now that I see the general idea it really is not that important. Unfortunately the list file MPlabX generates in the IDE does not show the "standard" code.

The program so far (the config is in a separate c file as suggested by XC16)...
Code:
.include "inc/global-defines.inc"

  
    .text
    .global _main
  

_main:
    goto _main
Turns into...
Code:
---  /home/jason/usr/local/src/pic24f/24f32ka302-template/asm/main.s  -----------------------------------
                                                  1:     .include "inc/global-defines.inc"
                                                  2:   
                                                  3:     ;--------------------------------------------------------------------------------------------
                                                  4:       
                                                  5:         .text
                                                  6:         .global _main
                                                  7:       
                                                  8:     ;--------------------------------------------------------------------------------------------
                                                  9:   
                                                  10:    _main:
0002CE  0402CE     GOTO 0x2CE                     11:        goto _main
                                                  12:  
                                                  13:    ;--------------------------------------------------------------------------------------------
                                                  14:  
                                                  15:  
                                                  16:      
                                                  17:    ;--------------------------------------------------------------------------------------------
_main: should start at 200, but there is some filler until 2ce and that is what I am curious about. The generated map file shows four additional sections between 200 and 2ce. I understand the default interrupt, but the rest...

Generated map file
Code:
"program" Memory  [Origin = 0x200, Length = 0x5600]

section                    address   length (PC units)   length (bytes) (dec)
-------                    -------   -----------------   --------------------
.text                        0x200                0xce           0x135  (309)
.text                        0x2ce                 0x4             0x6  (6)

                 Total "program" memory used (bytes):          0x13b  (315) <1%


"data" Memory  [Origin = 0x800, Length = 0x800]

section                    address      alignment gaps    total length  (dec)
-------                    -------      --------------    -------------------

                 Total "data" memory used (bytes):              0  (0)


Dynamic Memory Usage

region                     address                      maximum length  (dec)
------                     -------                      ---------------------
heap                             0                                   0  (0)
stack                        0x800                               0x800  (2048)

                 Maximum dynamic memory (bytes):          0x800  (2048)

Info: Project is using a large data memory model when small data memory model is sufficient.


External Symbols in Program Memory (by address):

                  0x000200                  __resetPRI
                  0x00023c                  __psv_init
                  0x000252                  __data_init
                  0x000252                  __data_init_standard
                  0x0002c8                  __DefaultInterrupt
                  0x0002ce                  _main
                  0xf80000                  __FBS.sec
                  0xf80004                  __FGS.sec
and the resulting HEX (not the actual hex file, but a filtered view)...
the left column is the address in each HEX line added to the segment address stored from an earlier line. The address needs shifted right one bit to be the correct program address. 400 = 200 and 59c = 2ce
The rest of each line is the corresponding data from each data line in the HEX file
Code:
00000000 ::  00  02  04  00  00  00  00  00
........  there was some other data here that would map to interrupt vectors .....

beginning of program
00000400 ::  0f  80  20  00  0e  ff  20  00  0e  01  88  00  00  00  00  00
00000410 ::  00  00  20  00  00  00  e0  00  02  00  32  00  00  01  20  00
00000420 ::  20  02  88  00  14  00  07  00  5b  00  07  00  00  00  e0  00
00000430 ::  03  00  3a  00  00  00  20  00  01  00  20  00  02  00  37  00
00000440 ::  00  00  20  00  01  00  20  00  81  0f  70  00  01  00  32  00
00000450 ::  14  00  07  00  00  00  20  00  00  00  e0  00  02  00  32  00
00000460 ::  00  00  02  00  00  00  00  00  ce  02  02  00  00  00  00  00
00000470 ::  00  40  da  00  00  00  fe  00  44  40  a9  00  00  00  20  00
00000480 ::  00  00  e0  00  03  00  32  00  00  00  20  00  a0  01  88  00
00000490 ::  44  40  a8  00  00  00  06  00  8a  1f  78  00  8b  1f  78  00
000004a0 ::  00  00  06  00  91  01  88  00  80  04  78  00  00  00  eb  00
000004b0 ::  1a  00  37  00  e2  84  44  00  32  a0  b4  00  99  05  ba  00
000004c0 ::  e2  84  44  00  32  a0  b4  00  99  06  ba  00  e2  84  44  00
000004d0 ::  32  a0  b4  00  00  06  eb  00  47  6f  de  00  fd  07  b2  00
000004e0 ::  63  6c  e1  00  03  00  3a  00  eb  ff  07  00  00  00  eb  00
000004f0 ::  0a  00  37  00  60  6c  e1  00  04  00  3a  00  00  5d  eb  00
00000500 ::  8b  05  e9  00  fd  ff  3e  00  04  00  37  00  61  68  e1  00
00000510 ::  01  00  32  00  00  86  eb  00  0b  00  07  00  19  c7  ba  00
00000520 ::  19  05  ba  00  0e  04  e0  00  03  00  3a  00  0a  00  e0  00
00000530 ::  e0  ff  3a  00  00  00  06  00  fe  07  b2  00  9e  01  88  00
00000540 ::  8a  04  78  00  f5  ff  37  00  39  5d  ba  00  8b  05  e9  00
00000550 ::  0c  00  32  00  29  5d  ba  00  8b  05  e9  00  08  00  32  00
00000560 ::  0c  00  e0  00  03  00  3a  00  e2  84  44  00  32  a0  b4  00
00000570 ::  f5  ff  37  00  19  dd  ba  00  8b  05  e9  00  fa  ff  3a  00
00000580 ::  89  04  e8  00  e1  84  44  00  32  a0  b4  00  00  00  06  00
00000590 ::  00  40  da  00  00  00  fe  00  00  00  05  00
0000059c ::  ce  02  04  00  00  00  00  00
01f00000 ::  ff  ff  ff  00
01f00008 ::  ff  ff  ff  00
01f0000c ::  7f  ff  00  00
01f00010 ::  bb  ff  00  00
01f00014 ::  5f  ff  00  00
01f00018 ::  fb  ff  00  00
01f0001c ::  ff  ff  00  00
01f00020 ::  ff  ff  00  00
I know I am missing the stack setup in my code which it looks like that is probably a few instructions of the "standard" code since it is defined in the map file, but other than that I don't know.

At any rate thanks for looking.
 
Last edited:

Papabravo

Joined Feb 24, 2006
22,082
For most compilers and IDEs (Integrated Development environments) you can find a file which contains the standard code that is used to setup the hardware, call your main() function and handle a return from main() if that should ever happen. In most cases for an embedded system main() contains an infinite loop because there is no useful purpose to exiting the main function. To find this file you may have to dig into the workings of the linkage editor or whatever compiler pass is actually generating the ".hex" file. This is doable and it is seldom hidden aggressively by compiler or IDE creators.
 

Thread Starter

geekoftheweek

Joined Oct 6, 2013
1,429
You were right papabravo. I ended up finding it. The XC16 "assembly" manual didn't have much to say, but the XC16 "C" manual described what was happening and where to find the code.

Now to figure out how to disable the standard stuff...
 

Papabravo

Joined Feb 24, 2006
22,082
You were right papabravo. I ended up finding it. The XC16 "assembly" manual didn't have much to say, but the XC16 "C" manual described what was happening and where to find the code.

Now to figure out how to disable the standard stuff...
Oh. OK, try looking for some kind of process that assembles a list of files to build. You can either change the name of the file that process is looking for or create an alternate file of the same name, to be used in it's place, that does the minimum to call main(). Typically this could be limited to initializing the stack pointer. The initialization of RAM and registers is also useful, but technically not necessary.

Good luck with your rockets.
 

Thread Starter

geekoftheweek

Joined Oct 6, 2013
1,429
Oh. OK, try looking for some kind of process that assembles a list of files to build.
Honestly now that I have been over the library sources a bit, and the assembler and c compiler manuals enough to make sense of what is going on I may just do my own makefile or script to build it and see what happens. There are options to explore... there are probably ways to disable things in MPlabX also to look for.

I seem to finally get tired of searching, ask my question, then find the answer shortly after by looking in that last place before taking a break out of frustration. Thanks
 

Papabravo

Joined Feb 24, 2006
22,082
The movie "The Shawshank Redemption" has for me always been a metaphor for software development. Nothing is what it seems and there is no obstacle that cannot be overcome with time and pressure.
 

Thread Starter

geekoftheweek

Joined Oct 6, 2013
1,429
The issue ended up being XC is a C compiler by nature that can do pure assembly also. It was adding the standard routines to set up a C program which would make perfect sense if it was a C program. Just had to disable all the standard libraries and such and it's all gone.
 
Top