why is there a main?
I know this is a newbie question but I never asked it when I had the chance, so why not now?
Why exactly does there have to be a main function?
why couldn't you just:
it sort of makes sense if you look at it before it's completely compiled but in Asm.
without the main function you can't really call the memory for the string but...
even in assembly it still doesn't make sense why C can't just write the string to a different address that is called globally like I want.
so if I have to have a main
how can I display something without a main?
There is a main so the program has a starting point. Not every one wants to make every line of code be executed in the order it is typed. This is the reason there is a main. Without it, we'd be stuck with BASIC.
And yes, you have to have a 'main' function. (Java, C++, C, they all have 'main'.)
Well, actually I did it!
I displayed text without main
but not in C or C++
though it is possible
__asm__ = ...
or something, I can't remember the assembly call.
but I did it in asm.
and copied the memory into the top of a temporary stack and displayed it using printf without Main or anyfunction
just "tmp "
No, of course you can write assembly programs
which don't have main. You still need to use a directive
to the assembler such as _start so it knows where to
begin I guess.
You all are just *so cute*.
The _reason_ you have a main() is because when the compiler builds the program and puts the jump tables in, it has to have somewhere to jump to, to get _your code_ going.
All programs start at relative location 0x00000000 to wherever the RAM heap is loaded for their memory segment.
Why? Because zero is the only location the processor knows about-- it's always there.
So let's say your program is double-clicked on, or told to execute in some like manner. The O/S allocates a block of RAM (the "heap") for your program. It loads it into the heapspace and jumps to location zero.
At location zero, the header stub begins to execute. It quickly calculates where your globals are, where your function addresses are (the address of main offset from relative "zero") and stuffs these into the jump table so they don't have to be calculated again. When the jump tables are built, the stackframe is initialized, and a jump is made to main() (now that it knows where it is).
Literally, "main()" is a label. Just like the name of every other function in your program. Even assembly requires the use of labels. Although assembly merely starts executing at the first byte of your program, because there is no jump-table setup in assembly-- such things are resolved by the linker at assemble time.
So let's say your program gets loaded at location 0x0007FE41A, in RAM.
That is your relative "zero".
So when your code gets loaded, the O/S jumps to 0x0007FE41A. Your jumptable and init code starts to run and when its complete it jumps to the main() label to get *your* code going. If that happens to be 754 (or 0x2F2 in hex) bytes into the code segment "from relative zero" then all references to main() become:
0x0007FE41A + 0x000002F2 = address of first byte of main() function's code.
Now, depending on what code snippet you're writing, it may not be called "main()" at all. But there normally is some label you start with.
Hey!! Smile everybody! We're cute!!!!
>>You all are just *so cute*.
Seriously, good post *. I have only a little experience with asm so your post was informative.
Not to be repetative, but I second Mr. Jdinger's comments.
Let me get this straight.
Programming food chain:
C or C++
- then -
- then -
Programming on an understandable level:
C or C++ - Most understand fluently.
ASM - A good number understand fluently.
Machine code - Good luck :D
Am I pretty close?
Which in a nutshell is exactly what I said in the first reply. Like I often say: "God I love simplicity."
I'd just like to point out that the way programs loaded and executed posted upthread is not the only way, and in the modern age there are vast differences.
The entry point of a program does not have to be absolute, in fact, most formats for executable programs I know of are either automatically relocatable or are made relocatable by using various techniques ranging from the use of address translation to data in the executable format which can be used to retarget all non-relative operations.
The reason why main exists is to define the entry point of your code: exactly how this is dealt with is an implementation feature. On one platform it might define a label in an object file which is invoked from within some static library. On another it might mean that the routine has a specific base address and invoked by the system (and the address does not have to be 0 or even known). The important detail is that it is an abstraction for the entry point of the program which is implementable across many architectures and platforms. It also happens to be an easy one to understand and to implement.
you guys are so cute, and so wrong. The sole reason for main is to allow your code to have substance. Some of the programmers out in the "real world" dont have much to put into their programs so main allows them to have an extra line of coding to make them feel better. Man you cute little "coders" need to read some more Aldous Huxley. (i.e. The Doors of Perception)
> Some of the programmers out in the "real world" "blah blah blah" *continuing...*
I'm not trying to be a "real world programmer".
I'm not a real world programmer who comes to messages boards to make others feel like ants either.
A very good portion of this board will probably never be professional programmers, nor have the intention of doing so.
> you guys are so cute