The compiler triggers the linker to link certain initialization modules from the runtime library called crt<something>. Only the necessary ones are linked, for instance crtxstack.asm (GSINIT1, GSINIT5) is not linked unless the --xstack option is used. These modules are highly entangled by the use of special segments/areas, but a common layout is shown below:
(main.asm)
.area HOME (CODE)
(crtstart.asm)
.area GSINIT0 (CODE)
(crtxstack.asm)
.area GSINIT1 (CODE)
(crtstart.asm)
.area GSINIT2 (CODE)
(crtxinit.asm)
.area GSINIT3 (CODE)
(crtclear.asm)
.area GSINIT4 (CODE)
(crtxclear.asm)
.area GSINIT4 (CODE)
(crtxstack.asm)
.area GSINIT5 (CODE)
(application modules)
.area GSINIT (CODE)
(main.asm)
.area GSFINAL (CODE)
One of these modules (crtstart.asm) contains a call to the C routine _sdcc_external_startup() at the start of the CODE area. This routine is also in the runtime library and returns 0 by default. If this routine returns a non-zero value, the static & global variable initialization will be skipped and the function main will be invoked. Otherwise static & global variables will be initialized before the function main is invoked. You could add an _sdcc_external_startup() routine to your program to override the default if you need to setup hardware or perform some other critical operation prior to static & global variable initialization. On some mcs51 variants xdata memory has to be explicitly enabled before it can be accessed or if the watchdog needs to be disabled, this is the place to do it. The startup code clears all internal data memory, 256 bytes by default, but from 0 to n-1 if --iram-sizen is used. (recommended for Chipcon CC1010).
See also the compiler options --no-xinit-opt,
--main-return and section 4.1
about MCS51-variants.
While these initialization modules are meant as generic startup code there might be the need for customization. Let's assume the return value of _sdcc_external_startup() in crtstart.asm should not be checked (or _sdcc_external_startup() should not be called at all). The recommended way would be to copy crtstart.asm (f.e. from http://sdcc.svn.sourceforge.net/viewvc/*checkout*/sdcc/trunk/sdcc/device/lib/mcs51/crtstart.asm) into the source directory, adapt it there, then assemble it with asx8051 -plosgff3.4 crtstart.asm and when linking your project explicitely specify crtstart.rel. As a bonus a listing of the relocated object file crtstart.rst is generated.
Next: 3.11.2 HC08 Startup Code Up: 3.11 Startup Code Previous: 3.11 Startup Code Contents Index 2008-02-29 |