Search code examples
cassemblyprogramming-languagesentry-point

Entry point of different languages


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,

  1. What's the program's entry point written in MASM, NASM, and other languages?

  2. 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.


Solution

  • 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.

    • With NASM, you use the .start directive to place the entrypoint.
    • With MASM it seems more complicated, but the 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_entry

    This 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.