Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write an Operating System completely in C?

Is it possible to write an Operating System completely in C?

By completely, I include everything up to the bootloader, but not the BIOS/UEFI.

like image 494
resgh Avatar asked Aug 23 '14 04:08

resgh


People also ask

What operating system was totally written in C?

Linux. Linux is also written mostly in C, with some parts in assembly. About 97 percent of the world's 500 most powerful supercomputers run the Linux kernel. It is also used in many personal computers.

Can I code an OS?

Writing an operating system from scratch requires a strong command of computer science, a programming language like C or C++, assembly, and code management practices.

Why are all operating systems written in C?

It's closeness to the hardware, explicit memory management feature, flexible structure, compatibility, and portability makes it the ideal choice for low-level development such as operating systems and embedded software.


1 Answers

The traditional answer to the question "can you write a program completely in C" is "no." The reason is that there is no way for a C program to set up its own stack.

Almost every processor has at least one register that points to the current address in the stack (usually called the frame pointer) and sometimes there's a second register (usually called the stack pointer) that needs to point to the next unused address in the stack. There is no way to use any expression or statement in the language itself to set either of the stack pointer or the frame pointer to an absolute value. (Procedure calls and returns can add and subtract from the stack pointer, but there's no way to initialize it to a known value.)

Posix defines a set of functions, setcontext, getcontext, makecontext and swapcontext (almost always written at least partly in assembler), that will enable you to read and write the stack and frame pointer.

Unfortunately, the setcontext family of functions is not widely implemented.

like image 54
Wandering Logic Avatar answered Oct 17 '22 12:10

Wandering Logic