Main function is called from C startup assembly (_mainCRTStartup) which is called by the operating system during the creation of a new task or process.

A task or process starts from the entry point of the executable binary. Programs written in C/C++ languages are linked with a startup assembly routine and the entry point is startup or start. This startup routine has some responsibilities before it can go to the main function. Startup assembly follows these steps before jumping to the main function.

  • Setup stack segment
  • Setup data segment
  • Setup BSS segment and clean BSS
  • Setup task related to platform
  • Collect environment strings and populate environ liest
  • Parse command line arguments and construct argc and argv
  • call main function with argc and argv

Startup code

Startup assembly is a part of compiler tool chain and it depends on platform. Linker links this statically to the output binary. crt0.obj is the object file for VC++ 32 bit compiler. Turbo C has different memory model and depending on memory model this startup code file varies. File name is like c0t.asm (tiny) or c0s,asm (small) etc.

Linker Map file

Startup code is hidden inside compiler and can be viewed by linker map file. Below map file shows startup routine, setup of argv, setup of environment routine linking locations and application load location.


 Timestamp is 54baa7ad (Sat Jan 17 23:49:25 2015)

 Preferred load address is 00400000

 Start         Length     Name                   Class
 0001:00000000 0000dd42H .text                   CODE
 0001:0000dd42 0001000eH .textbss                CODE
 0002:00000000 0000122bH .rdata                  DATA
 0002:0000122b 00000000H .edata                  DATA
 0003:00000a30 00000ba6H .data                   DATA
 0003:000015d8 00001974H .bss                    DATA
 0004:00000000 00000014H .idata$2                DATA
 0004:00000014 00000014H .idata$3                DATA
 0004:00000028 00000110H .idata$4                DATA
 0004:00000138 00000110H .idata$5                DATA
 0004:00000248 000004afH .idata$6                DATA

  Address         Publics by Value              Rva+Base     Lib:Object

 0001:00000010       _main                      00401010 f   main.obj
 0001:00000040       _mainCRTStartup            00401040 f   LIBCD:crt0.obj
 0001:00000170       __amsg_exit                00401170 f   LIBCD:crt0.obj
 0001:000001d0       __cinit                    004011d0 f   LIBCD:crt0dat.obj
 0001:00000210       _exit                      00401210 f   LIBCD:crt0dat.obj
 0001:00000230       __exit                     00401230 f   LIBCD:crt0dat.obj
 0001:00000250       __cexit                    00401250 f   LIBCD:crt0dat.obj
 0001:00000270       __c_exit                   00401270 f   LIBCD:crt0dat.obj
 0001:000003a0       __XcptFilter               004013a0 f   LIBCD:winxfltr.obj
 0001:000005b0       __setenvp                  004015b0 f   LIBCD:stdenvp.obj
 0001:00000700       __setargv                  00401700 f   LIBCD:stdargv.obj
 0001:00000c10       ___crtGetEnvironmentStringsA 00401c10 f   LIBCD:a_env.obj

 entry point at        0001:00000040

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.