Let’s write a simple Kernel (2014)
Bare metal projects are always a fun hobby. I've done some projects like this in the past [0], let me share some things I learned the hard way.If the author is reading, here are a few corrections.> char * vidptr = (char * )0xb8000;This needs "volatile" qualifier or all the writes to video memory will be dropped when optimization is enabled.Memory mapped I/O like this is one of the few use cases where you need "volatile" in C.> gcc -m32 -c kernel.c -o kc.oYou need to build a cross-compiler for bare metal projects. Using the system gcc will not work in the long run. The compiler packaged with your operating system is intended for building binaries for use with your operating system. It may have downstream patches or configured to a target in a way that will cause problems.The worst part is that it may seem to work for a while, until it doesn't. I ran into some hard-to-debug issues with my past projects and learned this the hard way. If I recall correctly, the issues were related to redzones and ABI conventions.You need to build GNU binutils and GCC for the "i686-pc-elf" target. I documented this process for someone else's bare metal project here [1].It really pays off to do this right from the start. Once you have a cross-compiler and a build system that can produce debuggable elf images (you also need to build gdb for the target), things get much easier. Using the built-in debuggers in QEMU or Bochs can only get you so far. Having a proper debugger with symbols and source view will make working much easier.For a build system, plain old Makefiles work best, CMake and other high level build systems are a pain in the ass with bare metal projects that require linker scripts, etc.[0] https://github.com/rikusalminen/danjeros
[1] https://github.com/Overv/MineAssemble/blob/master/README.md