C programming for OS development

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
exobition
Posts: 1
Joined: Wed Jul 09, 2025 4:47 pm
Libera.chat IRC: Exobition

C programming for OS development

Post by exobition »

I am working on an operating system project and i really need some recourses on programming in C for OSdev.
And because of the lack of sources I am stuck.
Any documents where I could learn more about bare metal programming that isn't for arm devices/aurdino/raspberry pie (most of them are only for blinky and hello world).
Anything helps anyways keep deving ur OS!
Octocontrabass
Member
Member
Posts: 6245
Joined: Mon Mar 25, 2013 7:01 pm

Re: C programming for OS development

Post by Octocontrabass »

exobition wrote: Fri Jul 11, 2025 4:43 pmAnd because of the lack of sources I am stuck.
Where are you stuck? The wiki probably has something that will help, if you tell us more about what you're trying to do.
nullplan
Member
Member
Posts: 2034
Joined: Wed Aug 30, 2017 8:24 am

Re: C programming for OS development

Post by nullplan »

exobition wrote: Fri Jul 11, 2025 4:43 pm And because of the lack of sources I am stuck.
The projects page has no lack of projects, most of which are open-source, so you can look at them. The wiki has the meaty skeleton you can use for the same thing. And of course the internet is absolutely crawling with bad tutorials about osdev. So what in particular do you need help with?
Carpe diem!
User avatar
iansjack
Member
Member
Posts: 4908
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: C programming for OS development

Post by iansjack »

As you exclude Arm, let's assume that you are working with an x86 processor. (If you were using, say, a PowerPC or a RiscV processor I would expect you to know the answer already - if not then poor choice of processor. Great though they are, there is far more information available for x86 processors and systems than anything else.)

The answer to your question as posed is easy. Bare-metal programming means controlling hardware devices by writing/reading registers in those devices. These are accessed via a memory location or an input/output port. In C this means using a pointer for memory access or encapsulating the x86's port input/output instructions in an assembly function or using inline assembly. Problem solved.

But I suspect that's not your real question. Perhaps you want to know how to use those registers for a particular device. How do you read keystrokes from a keyboard, display characters on a screen, read/write data from a hard disk? That's all dependant on the device that you want to control; details for most popular devices are readily available via a Google search (or searching the Wiki). Some are fairly easy to control (keyboard, IDE disk, screen), some are rather more difficult (floppy disk, network card, usb controller) and best avoided at first.

It will also be essential to understand how the x86 processor works. The Intel Programmer's Manuals are the definitive reference here. Essential reading, though you don't have to read all of them initially, and you certainly won't understand all of them without further research.

But I suspect that's not your real question. I don't think it's anything to do with the processor or language you are using. I suspect that you are asking "now that I have written a 'hello world' program, what next to produce an operating system?". To answer that you need to understand what an operating system does. It allows programs to be loaded and run (usually several at the same time) and it controls access to resources by those programs. (That's a simplification, but good enough at first.) Assuming that you are not writing a brain-dead, single-tasking OS such as MS-DOS. You don't want user programs deciding what memory they can use, or writing directly to the screen, for example. Or imagine two programs trying to write data to the same address on a hard disk at the same time - who wins? That's what the operating system arbitrates.

To answer that question you need to do a little background reading about OS design. There are several excellent general books on the subject and a lot giving more detailed explanations of the design and working of particular OSs. (There are also several web tutorials, but all they will teach you is how to reproduce someone else's work along with their inevitable errors.) If that's your problem then let us know and people can make recommendations.

Your question to me is a bit like the woman asking a New York policeman "Excuse me officer, can you tell me how to get to Carnegie Hall?." "You've got to practise, lady, you've got to practise.".
Post Reply