views:

1231

answers:

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

+7  A: 

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.

Joachim Sauer
Shit, You won. It's nice for the rep to be on first row.
Flinkman
There, get an upvote for your trouble ;-)
Joachim Sauer
+12  A: 

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 known 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)

Flinkman
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
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
Yepp, If I were the only one to use my os, then Forth would be the perfect tool.
Flinkman
+1  A: 

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

Christoph
A: 

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.

Dinah
+3  A: 

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#.

Daniel Watkins
Those poor soles. lol
eduncan911
(Your hyperlink is broken)
teedyay
Thanks, Olafur. I didn't understand the link syntax to begin with.
Daniel Watkins
A: 

Ummm, how do you think the first OS was written?

How about the first C compiler?

Jimmy J
The first c compiler was written in c++. ;)
Flinkman
According to http://en.wikipedia.org/wiki/History_of_compiler_writing and http://en.wikipedia.org/wiki/History_of_operating_systems the first compiler (1952) actually predated the first operating system (1956) by 4 years.
Joachim Sauer
While your answer does reveal a certain lack of understanding on the OP's part, it would be much more helpful to actually explain your point in a way that doesn't come across as insulting. Of course, that's assuming you're here to educate others, but if you're not... then why are you here?
Jason L
Oh, no! The thought police have found me...!
Jimmy J
+2  A: 

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.

Bogdan Gavril
most compilers generate machine code, not assembler; also, it seems you've never heard of LISP machines: http://en.wikipedia.org/wiki/Lisp_machine
Christoph
A: 

If you are thinking of using C# or alike, remember that C# compiles to managed code by default. Yes, you can compile to unmanaged "native" code, but it still isn't assembler code (I don't think).

eduncan911
A: 

You could use Logo! or even javascript for that matter, but you wouldnt actually want to dedicate much time(years) in that btw.

DFectuoso
+4  A: 

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.

vartec
A: 

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.

siddhadev
Ever heard of the Symbolic Lisp Machine?
Paul Tomblin
Sorry, "Symbolics".
Paul Tomblin
not until now, thanks, sounds interesting
siddhadev
+19  A: 

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. ;)

Jason L
Technically there's little "answer" in there, but for me it's still the best one.
Joachim Sauer
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
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
+1  A: 

Wasn't there a machine out there that had an OS written in Modula 3 or Ada or some other Bondage and Discipline language?

Paul Tomblin
Sweet ;-) Hehehe
ldigas
A: 

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