3 ms·
I've also recently discovered the joy of system level programming. I've gone through a few iterations of writing a (incomplete) kernel from scratch using differ
by khaledh 5y ago
I've also recently discovered the joy of system level programming. I've gone through a few iterations of writing a (incomplete) kernel from scratch using different languages. I started with a 32-bit kernel in C and got as far as preemptive multitasking.
Then I decided to try Nim, I found it very ergonomic and lets you write compact code that expresses the main logic without too much syntax noise. But I got blocked by some issues making it work for a UEFI target.
Then I tried Zig and got as far as parsing ACPI tables and writing an AML bytecode parser. Zig is nice and I like its optionals support and error handling approach. But I was put off by its noisy syntax (e.g. !?*[*]u8 to represent an error union of an optional pointer to a many-pointer of uint8). Also having to prepare and weave allocoators throughout most of the code that needs to dynamically allocate (which is most of the code) gets in the way of the main logic. Even little things like string concatenation or formatting becomes a chore. In the end I realized that Zig is not for me.
So I got back to Nim and resolved the issues that were blocking me. I'm now on my journey to write a complete kernel in Nim (so far I have access to UEFI services and ACPI table parsing and on my way to get interrups working). The code is pleasant to write and read. Also the language and the stdlib documentation is great (Zig's stdlib documentation is almost non-existent.)
Overall, I found that systems programming is very satisfying, and you can depend on specs that rarely change (some are over 20 and 30 years old and still work the same way today) and is quite a welcome change from web dev where it depends on tech de jour.
Edit: Here are the github repo links:
- C kernel: https://github.com/khaledh/bitflow https://github.com/khaledh/bitflow
- Zig "kernel": https://github.com/khaledh/axiom-zig https://github.com/khaledh/axiom-zig
- Nim "kernel": https://github.com/khaledh/axiom https://github.com/khaledh/axiom
- co_dh 5y agogithub? I like the idea of using Nim to write a kernel, and is curious on what the code looks like. Especially, how do you avoid malloc?
- khaledh 5y agoI wasn't ready to share it yet, but here it goes[1]. It's at a very early stage, but should give you an idea of how to get things up and running under Nim. I didn't avoid malloc. I provided a simple bump pointer based heap to get things going. Later I'll have to separate things into a UEFI bootloader and a proper kernel image, each with its own allocator (the bootloader will use UEFI memory allocation services, and the kernel will have its own heap). [1] https://github.com/khaledh/axiom https://github.com/khaledh/axiom