0

When I compile Fortran or FreeBASIC code into assembly, the instruction

call ___main

is found in the assembly code generated by the compiler.

However, when I use cl.exe to compile C++ code, this instruction is not generated by the compiler.

Both Fortran and FreeBASIC does not contain a function called ___main, how is this function automatically generated?

1 Answers1

0

Since we usually have done a good job of initialization in the BOOTLOADER, including code handling, we had better not call the library function __main(), because __main(), as a library function integrated with ADS, will initialize the system, which may conflict with our initialization. So it's best not to call __main() after we've initialized it. When simulation, if __main() is adjusted and entry is not set, a warning will be given. The __main() library function code is not well understood. It is estimated that the library function __main() should be used carefully.

When all the system initialization is complete, you need to transfer the program flow to the main application, that is, call the main application. The simplest case is:

IMPORT main

B main

Jump directly from the startup code to the main function entry of the application, though the main function name can be arbitrarily defined by the user.

In the ARM ADS environment, there is an additional set of system-level calling mechanisms.

IMPORT __main

B __main

__main() is a function provided by the build system that initializes library functions and initializes the application execution environment, and then automatically jumps to main(). So, the first is a library function, and the second is our own main() function;

So we use B __main to actually execute a library function, which then calls our main() function, so when you step through it you'll see that you have to run a section of the program (which is actually a library function), and then step through to our own main function (this also means that if you have B __main If we use B main to enter our main function, we will see our own main function directly in the step, no other program in between;

So what's the difference between using B __main and using B main what's the difference between entering our main function?

If the former is used, the compiler will add a "segment copy" program, that is, we say from the load domain to the execution domain conversion program; You don't have this with the latter, so if you want to do a segment copy, you have to write your own program to do it, and then you can go into our main function, and it doesn't have to be called main(), it can be called something nice, unlike B __main; No MATTER WHICH WAY WE USE TO ENTER OUR PROGRAM, there MUST BE A "SECTION COPY" program, RUN THE SECTION COPY AFTER CAN ENTER OUR MAIN PROGRAM! (By the way, startup.s does not have a "segment copy" function, so it's no good looking!)

For programs that contain a launcher," execute at the same address as load "is not easy to implement:

If the execution address is the same as the load address, of course, you do not need to do a "segment copy ", but I understand that the compiler will also add a" segment copy "program (if B __main is used), just because the condition is not met and not executed; However, IT IS NOT EASY TO "EXECUTE AT THE SAME ADDRESS AS LOAD" FOR A PROGRAM CONTAINING A LAUNCHER. Because the startup program is to burn into the non-volatile memory, used to execute on the power, and this program will certainly have RW segment, if the RW in the non-volatile memory, such as FLASH, it is not easy to achieve RW function, so RW to move to the memory can achieve RW function, such as SRAM. Therefore," execution address and load address are the same "is not easy to implement for programs that contain startup; The entry point to the program is at __main in the C library, where the library code does the following:

  1. Copy the non-zero (read and write) run region from its load address to the run address.

  2. Clear the ZI area.

  3. Go to __rt_entry.

  • 2
    I don't understand what you are saying here, but you seem to have lots of mentions if `__main`, several of `main` but not a single one of the `___main` of the question. – francescalus Aug 07 '22 at 09:10