x86 Bare Metal Examples

Hello world examples of programs without an OS. A.K.A. bare bones.

  1. Examples
    1. printf
    2. min
    3. bios_one_char
    4. bios_hello_world
    5. shutdown_apm
    6. TODO not working
      1. multiboot-c/
      2. hajji
    7. nasm
  2. BIOS
  3. Multiboot
  4. TODO

Getting started

sudo apt-get install build-essential qemu
make run
make run RUN=min
make run RUN=bios_one_char

Tested on Ubuntu 14.04. TODO: get working on real hardware:

sudo dd if=bios_hello_world.img of=/dev/sdX

into an USB did not work.

More assembly info at: https://github.com/cirosantilli/assembly-cheat

Formats

When we create a regular Linux program, we generate an ELF file, which is read by the OS.

Without an OS, we can use the following formats:

Gotchas

  • bytes 511 and 512 of the boot sector must be 0x55aa or else the BIOS will refuse to load

  • BIOS loads the program into memory at the address 0x7C00.

    We must tell that magic number to the linker somehow, either with a linker script, -tText=-Ttext 0x7C00 or NASM org 0x7c00.

    This will only matter when you access a memory address, because of relocation.

    If you don't know what relocation is, first read this: http://stackoverflow.com/questions/12122446/how-does-c-linking-work-in-practice/30507725#30507725

    When we link a normal program with an OS, the linker tells where it wants the OS to place it in virtual memory.

    But for the boot sector, the BIOS puts the program into memory. So we must tell that to the linker somehow. Otherwise it cannot know what addresses to use for instructions.

  • x86 processors start in 16-bit mode.

IO

You cannot use any libraries, so how to do IO? Some ways that this can be done:

Showdown and restart can be managed with either:

See also: http://wiki.osdev.org/Shutdown

Bibliography

The following did not work on my machine out of the box:

Description
No description provided
Readme 1.1 MiB
Languages
Assembly 64.3%
C 27.6%
Makefile 6.8%
Shell 1.2%