14

Can a whole operating system be written without using even one line of C/C++ code?

EDIT: One more to add to the list - assembly

3
  • I'm reminded about the old joke about /vmunix.el Commented Mar 9, 2009 at 17:07
  • 5
    @Rich B - good job editing the question to make all the replies look like their authors didn't read the question. Commented Mar 9, 2009 at 17:16
  • Wasn't there a machine out there that had an OS written in Modula 3 or Ada or some other Bondage and Discipline language? Commented Mar 9, 2009 at 17:24

11 Answers 11

52

Yes and no.

First of all, it's important to remember that whatever your language of choices, in the end the compiled product is in assembly language (or more accurately, machine code). Even interpreters (such as the cpython interpreter) are ultimately translating your scripts into machine code.

But that's probably being overly technical and missing the heart of your question:

"Can I write an operating system in a higher-level language?"

The answers to this are both personal and technical.

First, the personal side: if don't already know how to write an operating system in a mix of assembly language and C then you have absolutely no business trying your hand at OS design.

Often those new to programming have these sorts of questions because they want to do something as cool as writing a new OS without all the learning required to even attempt such a project. They wonder if higher-level languages can be a way to bypass all that messy study.

So if, in your heart-of-hearts, this is what you're after, stop now. Stop, stop, stop. Becoming good at something is hard work. There are no shortcuts. Be ready to roll up your sleeves and get some carpal tunnel syndrome.

That doesn't preclude following a path to eventual OS design! If that's your passion, then start at the top and work your way down. Get books on networking protocols, memory management, threading, etc, tackle each major subsystem and get to know it well. You can't write a new one if you can't use an old one!

Then read books on operating system design and implementation until you dream about process management methodologies.

Just bear in mind, the amount of knowledge necessary (not just of computer operations but of social constructs like APIs) is immense. This is a long journey and a rewarding one. If you truly love this craft like I do, you'll be glad you took the time even if you never actually write an OS.

Now, the technical answer. You're going to need a bootloader, and that must be written in assembly language. After all, your processor doesn't know C#. Past the bootloader phase, you can write your OS code in any language you want and it'll run, assuming your language can compile to machine code binaries (and not bytecode!)

However, even in our current "glut of cycles" computing environment, an OS must be lean and efficient and that's nearly impossible to achieve in a higher level language, even more so in an interpreted language.

Chances are, you'll need to write your own compiler/interpreter of that given language as a core component of your OS. That core compiler would likely allow only a restricted subset of the language (and you'd bootstrap by writing a more robust compiler in the restricted sub-language). Otherwise the performance will be abysmal.

But all of this is horribly complex and a real discussion of the issues requires a depth of knowledge you probably currently lack. But if you have the drive to do so, you can easily change that, and then I'd happily debate approaches all day!

If it makes you feel any better, I do know enough to write an operating system, and still I sit around daydreaming, trying to figure out how much of an OS could I get away with writing in python. ;)

5
  • 8
    Technically there's little "answer" in there, but for me it's still the best one. Commented Mar 9, 2009 at 17:28
  • 12
    That's a fair comment but sometimes a person's question reveals that the information they need isn't the information they asked for. ;)
    – Jason L
    Commented Mar 11, 2009 at 13:59
  • 2
    I don't want you to become jealous, but I was only 14 years old when I wrote my first "OS". It was written in only assembler as it was virtually impossible for me to get my compiler ( some very old version of visual studio ) to output executables not infected with any windows related crap. The OS was named GOSth ( Graphical OS, note the misspelling of Ghost ;D , English isn't my natural language ). It consisted of a Graphical shell ( 256 color GUI, Wohoo! ), Mousesupport, parts of the DOS-API implemented. Sadly enough my laptop doesn't have a floppy drive. And the sources is on a floppy...
    – Frank
    Commented Sep 11, 2009 at 18:47
  • Interpreters don't translate script into machine code. If they did, they would be called compilers. (JIT compilers often do perform translation of script languages designed for interpretation) Interpreters work by jumping between pre-existing blocks of machine code that implement the primitives available in the scripting language -- they don't create new arrangements of machine code.
    – Ben Voigt
    Commented Sep 18, 2014 at 19:49
  • With processors such as JOP in rise, Isn't it viable to write Operating system in say, JAVA, compile it ito JVM instructions in some other platform, and use those JVM instructions as assembly?
    – cipher
    Commented Sep 19, 2015 at 7:54
17

Yes. Use assembler. I would use Forth. Genera was an os made by symbolics** coded in Lisp. C is mostly used because it is trusted, we know how it works. And there exist c compilers for every CPU in the universe. Compilers for c are simple to build.

** Symbolics registered the first domain on the internet. (interesting fact)

3
  • I know the question shifted on you since your answer. So your answer isn't confusing to people reading this post-mortem: Originally the OP asked "can I write an operating system without using C/C++?" He later amended to "Can I write an operating system without using a low-level language?"
    – Jason L
    Commented Mar 11, 2009 at 14:03
  • And to reply specifically to your comment, I've always had a soft-spot for Forth. If I ever actually have the free time to tackle throwing together an OS, I have a feeling I'd go with Forth as well. :)
    – Jason L
    Commented Mar 11, 2009 at 14:04
  • Yepp, If I were the only one to use my os, then Forth would be the perfect tool.
    – Flinkman
    Commented Mar 12, 2009 at 5:50
15

Sure. You could use pure assembler or choose a different high-level language.

But you usually don't want to. Because the amount of assembler code necessary in a modern operating system is rather low. Most code in an OS doesn't need at that low a level.

C gives a nice intermediate level of abstraction that still allows writing rather low-level code while not having to deal with all the details that assembler developers need to deal with.

Edit: once you remove assembly from the equation it becomes a lot more complicated. Setting up a decent operating system requires executing several machine code instructions that are simply not represented in high-level languages (because they are very, very machine-specific). You could theoretically produce pure machine code in a binary blob and jump to that location from a high-level language, but I consider that cheating, as you'd have to come up with that binary blob in the first place.

1
  • Shit, You won. It's nice for the rep to be on first row.
    – Flinkman
    Commented Mar 9, 2009 at 17:00
11

Yes, MenuetOS is an example of an operating system written using only assembler.

As you don't seem to be interested in assembler see also JNode, an operating system written in Java, and Singularity, an operating system written in C#.

2
  • Thanks, Olafur. I didn't understand the link syntax to begin with. Commented Mar 9, 2009 at 17:16
  • Concerning Singularity: "Code in Singularity is either verified or trusted. Verified code’s type and memory safety is checked by a compiler. Unverifiable code must be trusted by the system and is limited to the hardware abstraction layer (HAL), kernel, and parts of the run-time system. Most of the kernel is verifiably safe, but portions are written in assembler, C++, and unsafe C#."
    – Ben Voigt
    Commented Sep 18, 2014 at 19:57
9

There are numerous attempts to create hardware implementation of JVM. Of course OS for such hardware would be written in Java. See for example: http://www.jopdesign.com/

Also Nokia's S40 seems to be developed principally in Java.

5

Sure. C/C++ code is compiled into ASM code, but there are many other languages you could use in the same way.

You might not be able to use Java or C# since these rely on a Virtual Machine and that could be written in C.

Actually I believe there was a project to create an OS based on LISP, but it failed.

1
4

Sure. As long as there's a compiler available which can produce appropriate machine code, any language will do.

3

Check out http://common-lisp.net/project/movitz/. It's an OS entirely written in Lisp. I think it has it's own Assmebler, e.g. Lisp to machine code translator.

I love the idea of OS without C/C++ and especially a Lisp-based one.

2
  • Ever heard of the Symbolic Lisp Machine? Commented Mar 9, 2009 at 17:23
  • It sounds weird you mention Movitz and admittedly never heard about Lisp machines. Lisp machines are considered the main source of inspiration for Movitz. Lisp machines were a commercial failure because they could not keep the pace with the "non-Lisp" competition, but with the tremendous raw hardware power we have today, why not trying again with an open source project?
    – Halberdier
    Commented Jul 26, 2011 at 13:42
2

Yes. Obviously anything is possible in assembly but often not practical. There have been a few attempts at making operating systems in managed code (.NET, Java) with varying degrees of success.

2

If you really wanted to, then yes, you can write a whole operating system without using Assembly, C or C++, but I recon it would just be a waist of time. You could get a binary editor and program the whole language in Binary or Hexadecimal if you wanted to, it would take ages, but it's completely possible.

1

you could write it in machine code? bu this is just binary. you could write it in assembly but this has a complex structure. you probably should write it in C because it translates directly to machine code when it is compiled, so it is faster than C#/C++ and simpler than assembly

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.