An OS in assembly 100%

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
KSK
Posts: 2
Joined: Fri Mar 13, 2026 6:14 pm
Libera.chat IRC: #KSK

An OS in assembly 100%

Post by KSK »

I will build an OS since scratch but all in assembly. I just wanted people that know more than me give me their opinion. Things to clarify: Only will be for my computer, so, all running in ring 0, without security, for use the maximum use of the hardware.
nullplan
Member
Member
Posts: 2034
Joined: Wed Aug 30, 2017 8:24 am

Re: An OS in assembly 100%

Post by nullplan »

I think this is ill-advised. I remember having an assembler phase when I was younger, and I wanted to build everything in it. Then, after I spent more time than is reasonable debugging an issue that a compiler would have found immediately (too few arguments to a function, and two arguments were swapped), I became somewhat disenchanted with the idea.

Use tools to help you find the mistakes you made. Tools do not tire out or mix up similar-sounding functions. Using even just a C compiler to the fullest can help you avoid a lot of issues stemming from that grey matter between your ears.

Plus, the second point is that writing assembler very often is just so completely tedious. Something that is one line in C turns into twenty in assembler. And assembler is very resistant to change. I find that if I need to make a somewhat significant change in assembler, it is easier to just scrap the whole function and start over from scratch.

And the third point is portability. You may not care about it now, but inevitably there will come a time when you want to run your OS on a second computer, possibly because the first one breaks (nothing lasts forever). Assembler may tie you to the hardware too closely, and so the death of a single power supply can mean all of the work you spent scatters in the wind.
Carpe diem!
User avatar
Demindiro
Member
Member
Posts: 165
Joined: Fri Jun 11, 2021 6:02 am
Libera.chat IRC: demindiro
Location: Belgium
Contact:

Re: An OS in assembly 100%

Post by Demindiro »

I'm working on an exokernel which is written in 100% assembly for two reasons:

1. I aim to require the absolute minimum of dependencies, as it is intended for bootstrapping. Currently only fasm 1.73 is strictly required.

2. I will only implemented the bare minimum functionality required to secure hardware and provide debugging facilities. Specifically, the kernel implements a driver for the MMU, (AMD/Intel) IOMMU and serial hardware. I also got myself a cheap PCIe FPGA which should, after programming, be more useful for debugging while requiring less code on the kernel side. I'm still waiting on the pinout diagram from the seller though...

The important thing here: this is a minimal kernel. I will eventually bring over my OS which is written in Rust. Assembly works well for device drivers since it usually involves a very imperative programming style and most of my time is spent interpreting specifications anyway. But for anything more abstract like file systems, processes ... a high-level language is a massive development boost.
nullplan wrote: Mon Mar 16, 2026 11:50 am Then, after I spent more time than is reasonable debugging an issue that a compiler would have found immediately (too few arguments to a function, and two arguments were swapped)
I found that giving each register a unique color helps immensely with avoiding such mistakes.
2026-02-18-213344_429x588_scrot.png
GeneSYS exokernel (Codeberg)
Lemmings! micro-/multikernel (Github, Codeberg)
Waddle container tool (Codeberg)
User avatar
KSK
Posts: 2
Joined: Fri Mar 13, 2026 6:14 pm
Libera.chat IRC: #KSK

Re: An OS in assembly 100%

Post by KSK »

nullplan wrote: Mon Mar 16, 2026 11:50 am And the third point is portability. You may not care about it now, but inevitably there will come a time when you want to run your OS on a second computer, possibly because the first one breaks (nothing lasts forever). Assembler may tie you to the hardware too closely, and so the death of a single power supply can mean all of the work you spent scatters in the wind.
This OS i'll make is only for my computer, and, i love understanding absolutely ALL what's happening under the hood. I'm almost obsessed with the optimization, and as i have a lot of time, that's why i want to do it all in assembly. I love all this world in extreme. (unlikely that i want to run this in another computer) Thanks for your reply, Good night!
User avatar
iansjack
Member
Member
Posts: 4908
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: An OS in assembly 100%

Post by iansjack »

To add to nullplan's points the biggest mistake (IMO) that beginners often make is to think that they can write better, more efficient, code in assembly language than a good C compiler can. In essence they are saying "I can produced better code than all the generations of programmers who have worked on <compiler name of your choice>". The truth is that the collective wisdom of those programmers knows how to optimize code for a particular processor taking account of the vagaries of predictive lookahead, effective use of processor cache, etc., etc. The result is code that is often much less efficient than that created by a good compiler. And this is ignoring the fact that it is so much easier to maintain and optimise code in C, for example, than assembly language.

But it's a phase that many beginners go through. I certainly did.
sandras
Member
Member
Posts: 178
Joined: Thu Nov 03, 2011 9:30 am

Re: An OS in assembly 100%

Post by sandras »

I'm developing two kernels. An experimental one in assembly, and a more traditional one in C.

The assembly kernel is intended to be a minimal, purpose-built one to run a Forth userspace. If and when I develop a Forth assembler, this system can become self-hosting.

I think it's feasible to write a kernel in assembly, if it's intended to stay minimal. But even then, you should have structure in your code. Use some sort of a calling convention. Add comments describing function arguments. Factor out some functions even if you call them just once (this makes thinking easier, but also names things).

You won't end up saving every byte when having this structure, but it will be impossible to go forward without it as your kernel grows.

A theoretical, optimized-to-the-last-bit, non-trivial assembly kernel is a jumbled spaghetti-code mess. You'll waste more development time in real life than your runtime savings ever speed up anything.

For anything more conventional use a higher-level language.

Anecdotally, I have a memory allocation speed test in both of my kernels. The assembly kernel performs better. But that's not real-world use, just one specific test on one specific test machine. And you do need to measure.

Just my two cents.
https://github.com/shimanauskas
Slava Ukraini!
Šalin rankas nuo laisvo žodžio!
User avatar
GoingNuts
Posts: 9
Joined: Fri Jun 17, 2022 7:36 pm
Libera.chat IRC: GoingNuts

Re: An OS in assembly 100%

Post by GoingNuts »

You might want to take a look at BareMetal o/s by ReturnInfinity https://github.com/ReturnInfinity
nightprog
Posts: 1
Joined: Sat May 09, 2026 5:16 am

Re: An OS in assembly 100%

Post by nightprog »

KSK wrote: Mon Mar 16, 2026 8:52 pm
nullplan wrote: Mon Mar 16, 2026 11:50 am And the third point is portability. You may not care about it now, but inevitably there will come a time when you want to run your OS on a second computer, possibly because the first one breaks (nothing lasts forever). Assembler may tie you to the hardware too closely, and so the death of a single power supply can mean all of the work you spent scatters in the wind.
This OS i'll make is only for my computer, and, i love understanding absolutely ALL what's happening under the hood. I'm almost obsessed with the optimization, and as i have a lot of time, that's why i want to do it all in assembly. I love all this world in extreme. (unlikely that i want to run this in another computer) Thanks for your reply, Good night!
The C/C++/Rust Compiler actually optimize C/C++/Rust code better than you could optimize for large codebase. So the argument of "i use asm to get better optimization" doesn't really work here. But I respect the idea of looking whats happenning under the hood. Good luck with your OS.
Post Reply