Link to home
Start Free TrialLog in
Avatar of arvindhp
arvindhp

asked on

who is in charge of calling main() function?

In c code can be divided into number of funcions. For invocation of the funcions it is required to give the name or address of that function explicitely by the programmer. Similarly who is calling the main() function?
It is definitely NOT by compiler--it just compiles.
Avatar of arvindhp
arvindhp

ASKER

NOthing
ASKER CERTIFIED SOLUTION
Avatar of murrayc
murrayc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe main is actually address 0 in your .exe?
Why dont you dissasemble a 'hello world' type program to see how your compiler does it.

I remember reading somewhere that an .exe needs to meet a few criteria to be accepted by windows, such as the first 2 bytes to be something like 'MZ', which decompile to a relative jump or something.
MZ is the file signature of an executable.

Check out

http://www.chesworth.com/pv/vault/file_format/dosexecutables.txt

for an overview of the file format.
The one calling main() is the "Runtime Startup Code" - This is a piece of code (mostly written entirely in assember) - some compiler supply the source (In my Watcom the source is located in \watcom\src\startup) ... The startup code setup stacks and other stuff.. (Getting the parameters to supply to main(...)
the jump thingy is for COM files, and it was used mostly in the good ol' days ( i wasn't there at the time ;-)) there is nothing that calls main... main is simply the program itself in assembler... the program instructions get loaded into memory and then are being executed one at a time... etc, etc.... here is an assembler Hello World program....

a COM program...

.model tiny
.code
      org 100h
main:
  mov dx, offset msg
  mov ah, 9
  int 21h

  mov ax, 4c00h
  int 21h
.data
  msg db "hello world$"
end main

an EXE program...

.model small
.stack 100h
.data
  Message db "Hello World$"
.code
main:
  mov ax, @data
  mov ds, ax

  mov dx, offset Message
  mov ah, 9
  int 21h

  mov ax, 4c00h
  int 21h
end main

the same thing would looklike this in C

#include <stdio.h>

main()
{
  printf("Hello World");
}

Hope this helps...

..-=ViKtOr=-..
Very much.
>>Very much.

what the hell do you mean?!? :)
IMHO, Hougaard has given the most correct answer.

It is the run-time code that is supplied with your C compiler.

A program will be compiled and then linked to the run-time library. The compiler initially sets the entry point of the final executable to some run-time library function. That function will take care of all the setting up that needs to be performed.

When that function is finished, control will be passed over to main() by that function (just a call to main()).

For the sake of linking the run-time library correctly and making it possible for the compiler and linker to make the executable load and run, there has to be at least one function with a known name. That function is your supplied main().

When main() is finished (i.e. program stopped running), control is given back to the function which called your main(). That function (in the run-time library) is also responsible for calling various sorts of clean-up routines as atexit() (also with predefined names).

When all cleaning-up has been performed, control is given back to the operating system.
Amen!
sorry for the delay...
hougaard
Thanks for giving me the answer.
I am very much interested to see the code that calls main. I am using Turbo C++, Borlan international version 3.0. Please try to send the code as i doubt of having that part of the code in my compiler. Hope you will reply soon.
Well I'm not using Borland so the Watcom code would not help you.
can any expert help me regarding this this.