I know that in C/C++ and Java, the entry point of a program is the function main(),
now I have the following two questions,
What's the program's entry point written in MASM, NASM, and other languages?
What's the convention from which CPU knows where to find entry point of a program?
==-EDIT-==
Question 2 is not a meaningful question since it is wrong that CPU takes responsibility of finding the entry point. There is no such convention. See Eric Lippert's clarification.
In assembly (both MASM and NASM are merely assemblers, i.e. programs that convert assembly source code to machine code) there is no default entrypoint. You typically specify it with an assembler directive.
.start directive to place the entrypoint.end directive is important.The address referenced then ends up in the binary (executable) file's header, so that the operating system can figure out where to jump.
For ELF binaries (used on many operating systems) see the e_entry header field:
e_entryThis member gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero.
This happens with C too, except there of course the compiler sits between your source and the executable file, so it inserts the required reference to main() (or, actually, typically to an init routine that runs before main().
Java does not work with raw binaries, its programs on a JVM, so it doesn't really compare.
The entry point is not usually defined by the compiler, but by the linker. See for example ld --entry for GCC or link /ENTRY: for VC++.
In C and C++, the default entry point is usually in the standard library (_start in GCC IIRC) and that function eventually calls the user main().
Java, having the JVM, the program entry point is hard coded in the JVM.
If you program in assembly, it depens on whether you use some language's standard library:
main() function to your program, and all will just work as in C._start).For example, compare the error messages of these two commands, when compiling and linking an empty C file:
$ gcc empty.c
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
$ gcc -nostdlib empty.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080480b8
You can change the entry name with:
$ gcc -nostdlib -Wl,-entry,begin empty.c
/usr/bin/ld: warning: cannot find entry symbol begin; defaulting to 00000000080480b8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With