18

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.

1
  • 1
    It depends on what you actually mean when you say "operating system". I read it's possible for some of the Cortex-M processors (very limited, used for MCUs) in a book related to ARM, but most probably on many other cores you'll be forced to use at least a bit of assembly for some basic configuration.
    – user35443
    Commented Aug 23, 2014 at 9:10

3 Answers 3

20

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.

7

It depends what you mean by C.

If you mean the standard C programming language as defined by ISO, relying only on defined behaviors, and you're thinking of a common variety operating system that runs on hardware, then no, it is not possible to write a practical operating system in C, because C lacks many facilities, in particular to access hardware and to manipulate separate programs.

It is possible to write an operating system that consists of C program and only provides input/output through C facilities, but that isn't an OS that runs on actual hardware. You can write an emulator for a machine and its operating system this way.

If you allow implementation-specific behaviors, then the answer is trivially yes, because implementations are allowed to add whatever extensions they see fit. It's definitely possible to have a C implementation where operating_system(); is the source code of a program that implements an operating system.

If you mean whether it's possible to use a typical C compiler to build an operating system (including a runtime environment) without using any assembly, then the answer is usually no, because C compilers typically lack some primitives that are only of interest to OS writers, such as what it takes to perform a context switch and establish a stack. However this isn't an absolute rule. If you like, it depends on which parts of the runtime environment come from the compiler writer and which parts come from the OS writer.

2
  • It would be possible on a fair number of platforms to write an operating system without writing a single line of assembly language (using only the libraries that ship with the compiler) though it would usually be better to write at least a little bit of machine code. For example, on many platforms one could define (as a static) a union of a jmp_buff and a char[], have the program entry point individually write the characters thereof, and then do a longjmp to it without making any use of any stack [note that memcpy or even a struct assignment might be a subroutine call, thus no good].
    – supercat
    Commented Jun 30, 2015 at 15:53
  • Using a little bit of in-line assembly to set the stack would probably be cleaner (and possibly more portable) than byte-by-byte creation of a jmp_buff, but using techniques like jmp_buff would make it possible, in many implementations, to set up the system without requiring the compilation of anything other than C code.
    – supercat
    Commented Jun 30, 2015 at 15:55
7

Theorically yes, but not everything.

For the bootloader and real mode stuff, you could find some old 16 bit compiler (ex. Turbo C), tell him to output plain binary and write all that stuff with it.
Or, alternatively on GCC you could put asm(".code16gcc") in top of your code, but I don't suggest this, since GCC isn't able to output directly 16 bit code, the result assembly program could take more than 512 bytes.

For other stuff, nullify the assembly usage isn't possible.
Yes you could hide all assembly lines inside inline functions (IN\OUT, CRx registers and stuff), but for other things, like interrupts, you'll be forced to use assembly.

Edit:
Now GCC (4.9.0) supports the -m16 option which enables 16-bits code generation, therefore the assembly usage can be further reduced for things like the bootsector/bootloader (some bits still would require it although).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.