In the beginning

The startup code in Figure 2 calls the main-function. In this case, as shown in Figure 1, it is a main-function of a small stand-alone program. In a larger system, it may be the main-function of an operating system, for example Linux.

As described above, the startup code is executed by boot code, in the form of BIOS or perhaps in the form of a boot loader such as GRUB, during the startup of the computer.

One might wonder how the very first code that runs, when a computer starts, is invoked. Considering a scenario where a computer is started, by switching on the power, it can be seen in Section 9.1.4 of Volume 3 of the Intel 64 and IA-32 Architectures Developer's Manual, that execution starts at an address given by the hexadecimal number 0xFFFFFFF0. At this address, boot code must be stored. This code has the task of performing necessary initialisations, by itself or in collaboration with boot loader code such as BIOS or GRUB. The initialisations done may involve initialisations of important devices such as interrupt controller and cache controller, and also initialisation of communication devices, such as serial ports and network interfaces, may be necessary. When the initialisations are done, startup code like the one in Figure 2 is executed.

At a later stage, when a program, like the one exemplified in Figure 1 or perhaps in the form of an operating system, is running, additional initialisations of devices and communication ports may be performed.

In an Intel-x86 processor during startup, the BIOS loads a boot sector, often called a Master Boot Record, to an address given by the hexadecimal number 0x7c00. At this address, code is executed. This piece of code performs processor initialisations, and it also needs to perform a switch from 16-bit real mode to 32-bit protected mode. Eventually, and possibly via the use of a boot loader such as GRUB, it loads and executes startup code as specified in Figure 2.

If UEFI is used instead of BIOS, as is the case in newer computers, it is not required to use a Master Boot Record. A new alternative, called GUID Partition Table, can be used instead.

  • Intel-x86
  • ARM