[A83] Re: C(++) programming
[Prev][Next][Index][Thread]
[A83] Re: C(++) programming
> Actually, the function main isn't any more special then progstart or
> progstart-2(depending on the calc).
Yes, it is. Symbols for a symbolic assembler are completely different than
functions in C. Further more, main is the only function in C where it is
legal to declare the implementation with a varying number of parameters.
> What the compiler does, is it converts your program to asm, the inital
part
Not necessarily. A compiler backend can generate different things.
> is probably an initisilasation code(for example if you had a class that
was
> global that needed to be set up(c++ stuff))
Correct, all global classes are constructed before main is executed.
> after that it calls the function _main(most of the c/c++ functions are the
> name you gave them plus a '_' infront of them)
This is implementation specific.
> but before that it pushes in and pops the data you passed to the
function(why
> i belive recursion isn't as good as some say it is)
Not exactly. The "C" calling convention has the caller push the parameters
in reverse order (right to left)and then call the function. The caller must
remove the functions from the stack after the function returns. Parameters
being pushed right to left allows for variable length argument lists, such
as for printf. The function does not usually literally pop the parameters
off the stack. The code generates a stack frame. Values on the stack are
accessed via pointer.
An example of this can be seen from the following program (hex values are
used to make the disassembly easier to read):
void foo(int i)
{
i = 0xCD;
}
int main(int argc, char *argv[])
{
foo(0xAB);
return 0;
}
Compiling this code without optimizations using gcc for Linux on i386 will
generate the following code, as disassembled by gdb using the Intel
disassembly flavor (the default is AT&T):
0x8048415 <main+9>: push 0xab
0x804841a <main+14>: call 0x8048400 <foo>
0x804841f <main+19>: add esp,0x10
0x8048400 <foo>: push ebp
0x8048401 <foo+1>: mov ebp,esp
0x8048403 <foo+3>: mov DWORD PTR [ebp+8],0xcd
0x804840a <foo+10>: leave
0x804840b <foo+11>: ret
The calling code in main pushes the parameter onto the stack, calls the
function, and clears the parameter off the stack. The called function
accesses the parameter via a register.
> in C if a varaible is declared, but no type is given, it should default to
> int, but in c++ they dropped this 'feature'
This is because C++ has stronger typing than C.
> If you declare the main function as anything other then int, some
compilers
> will stop on you, while others will say 'ok' and continue on.
Anything other than int is illegal in ANSI C and C++.
> As far as i can tell you can call the main() function just like any other
> function(though i would be carefull with this because that can be very
> dangerous)
It is legal in ANSI C, and illegal in ANSI C++.
> Also there is a destruction bit of code that is called when the main
function
> returns a value.
What on earth are you talking about?
References: