views:

660

answers:

10

MenuetOS is an example of an OS written entirely in Assembly. Is there any advantage to writing it in Assembly instead of a low-level programming language such as C?

A: 

At this point, not much, although it naturally depends on what system and what resources you're writing the OS for. Almost all modern computers come bundled with a C compiler that is basically as close to the machine as you can get, so using Assembly is not going to help. On the other hand, if you're writing code for a robot, Assembly would be very useful.

Eli
+5  A: 

Presumably you can write a compact and fast OS. Though you may not be able to write it quickly, and you lose any hope of trivial portability.

dmckee
"can" being the operative word. Its very easy to write slower and larger assembler code than a compiler creates.
Goz
@Goz: no arguments from me. I think Tyler McHenry has the most accurate answer.
dmckee
I should have posted it as an answer and cashed in on the rep points
Tyler McHenry
+2  A: 

Optimized assembly is the fastest you can get. Even well-optimized C is measurably slower that well-crafted assembly. For a general-purpose operating system, though, the gains are not worth the difficulty. I can't imagine an assembly OS scaling at all in that realm. If I recall, Menuet is meant for very small, simple embedded systems where the increased complexity of assembly code is outweighed by the performance benefits.

Thom Smith
I find that most compilers are a lot smarter humans these days. For inner loops in videogames or something, it could be different. But for mundane stuff you would do with while(), for(), if(), etc..., your compiler will always make better code than you.
kmm
Did you ever look at the object code a modern c compiler procduces? You can get grey hairs at all the lost cycles and missed optimizations. Seems like an urban myth to me. "Compilers nowadays are much better than assembler". I think I heard it 20 years ago, and it still is far from true.
drhirsch
Compilers logically cannot be better than "well-crafted" assembly. Any optimization your compiler can make, you can make, so if your assembly is not as good as your compiler's, that just means you aren't a good enough assembly programmer and you aren't crafting well. Hence, we can speculate that there exist "good enough" human programmers who can beat the C compiler. And quite aside from sophistry, it probably is true, at least for reasonably small amounts of code.
Steve Jessop
If you improve a compiler's optimizer, you optimize all the code that compiler can compile. If you optimize assembler by hand, you're only optimizing that one routine. The compiler is better.
Breton
onebyone: In the infinite-time limit, yes. However, aside from "reasonably small amounts of code" (which, realistically, is better described as "tiny"), programs are not written in the infinite-time limit. In the finite-time case, it's an entirely different question.
Brooks Moses
Of course - a compiler is designed to optimise vast quantities of code, every line written by every programmer using the compiler. But an assembly-jockey can look at any particular C program, look at the compiler output, and beat it. So it all depends what you mean by "fastest you can get" - the most performant code possible for a given task, or the most bang per buck from your programmers. I'd consider a minimal embedded OS to come under the heading of "reasonably small amounts of code".
Steve Jessop
compilers are better than the average programmer. And there are some very good compilers, gcc not being one of them, one size fits all usually means it does not fit anyone in particular. Write in C and hand tune the bottlenecks is the best rule for cost and performance.
dwelch
A compiler is PRIMARILY intended to increase the efficiency of the programmer, not the program. When you have a programmer that's "native toung" is assembly he'll be a John Henry to a compiler's Steam Engine. It's the difference between a hand tuned suspention vs. one produced on an assembly line.
NoMoreZealots
A: 

The advantages are pretty much none compared to the disadvantages

BlackTigerX
+6  A: 

Interfacing with an OS tends to be easiest in the language it was written in -- C in UNIX and Win32, C++ in BeOS, etc. MenuetOS was designed specifically to make developing assembly applications easy, so the best choice was to write the OS itself in assembly.

For OSes which aren't intended to be general-purpose, such as embedded systems and microcontrollers, the overhead introduced by mechanical translation (ie compilation) of C into assembly can be unacceptable. Compilers aren't as smart as humans, especially when it comes to the sorts of fiddly little optimizations required on embedded platforms, and writing directly in assembly guarantees there won't be a compiler mucking up carefully-designed algorithms.

John Millikin
"Compilers aren't as smart as humans" that was my guess.
Lawand
Open the pod bay doors, HAL...
h4xxr
@Lawand Just remember to finish the phrase. On a general purpose application, compiler are by far better at optimization. It's the exceptional "corner cases" that it needs help on.
GMan
With properly tweaked C code, one can get the same code as one would have got writing it in ASM. So, it is possible to gain the best of both worlds.
sybreon
Compilers are smarter than most programmers. However, a programmer who can write an entire O/S in assembly language, most likely has the advantage.
Carson Myers
"Compilers are smarter than most programmers." Most programmers don't take the time to understand the machine. Anyone who dedicates the mental focus required to write assembly on regular basis is typically a better programmer for it even when programming in a high level language. It's like a distance runner doing sprints, you might never do it in an actual race but as an exercise it improves your performance.
NoMoreZealots
Why would an OS written in C make developing assembly applications harder? I can't think of any good reasons. Give an example, please.
Pavel Minaev
@Pavel: The OS APIs will be written with C clients in mind, have documented examples written in C, and the behaviors will not be written from an assembly programmer's point of view. It's not impossible, obviously, but having to mentally translate between languages makes development more difficult.
John Millikin
@John - Coming up with an assembly interop standard, forcing API's to conform to that standard, and creating assembly specific samples is a lot less work than writing the entire OS in assembly.
Michael
@Michael: All of those are already present for existing OSes, such as Windows and Linux. Despite this, the percentage of applications written for Linux in assembly is much lower than that of C applications.
John Millikin
I don't think that's because it's harder to interface with the linux OS in assembly. I think it's because it's harder to do anything in assembly.
Steve Jessop
*Interfacing with an OS tends to be easiest in the language it was written in* Caveat: Some OS are written in one language with the API designed for use in another. Example: Mac OSs up to system 7 were written in c and assemble, with the API optimized for pascal.
dmckee
A: 

I guess just performance and size.

George Statis
A: 

Someone could say - speed and small code (by small code I don't mean source code of course, but compiled one), as for the second one - probably yes, as for speed - I don't know how big the differences are, assuming the fact that you have decent c compiler. But I can give you 1 big disadvantage - you can't port such OS to other architectures, writing OS in assembler binds you to one and chosen processor architecture. I won't even mention time that you have to spend to write such piece of software, compared to C.

Ravadre
+2  A: 

I'd imagine the advantage was that it gave them a lot of good PR considering the number of places I've seen that story. Reputation gets you the best jobs...

Stephen Friederichs
+2  A: 

There is still an active community of folks who bang out demos in assembly. There are sites where people are even coding demos for 8-bitters like the C64 and Atari XL machines. It's simply thier "weapon of choice." And they enjoy coding in that format. And provides a higher degree of control, but requires a greater understanding of the machine. Their mastery over the machine and ability to do entire executables in less memory than the overhead standard C libs take up give them intense satifaction.

No matter how good your compiler is, it's still follows a mecanical process in generating the code. Assembly pretty much gives you full control, allowing you to do things that aren't easily representable by even a language like C. For example "Atomic operations" are usually wrapped by either a OS call or function call making them substantially less efficient because there is no standard way to represent them in the language it's self. (Sometimes they will be represented by a compiler intrinsic extension to C.)

Also it allows unconventional things to be done, like using the processor stack in ways that you CAN'T in C. (Thus in C you create a "Stack" datastructure using dynamic memory allocation to replicate some thing the processor can natively do.)

Size wize, I've seen a full screen Text mode, text editor that was required less than 512 bytes for the executable image. That was written by a guy I knew from the bulletin boards. (The pre WWW era.) I've seen C startup code that took more memory than that!

NoMoreZealots
+2  A: 

Main advantage: you don't need a C compiler. Secondary advantage: if you have to make the code size really, really small, then (a) modern compilers are aiming more for fast execution than small code size, and (b) the human penalty for writing assembly code is smaller if the artifact is smaller.

So if you're doing some really tiny OS for some new embedded ISA and it all has to fit in 4K, writing in assembly might be a good choice...

Norman Ramsey
I suppose that's actually valid, writing a compiler is not a trival undertaking in and of it's self.
NoMoreZealots