Page 1 of 2
Is it worth to write my system (small) in Assembly entirely?
Posted: Wed Aug 01, 2012 11:20 am
by Congdm
Hi and I am new to OS Development and OSDev.org. Thank you for many documents and guide that help me to learn about OS Development.
I usually program in high-level languages like Pascal or Visual Basic but when come to OS Development, I prefer Assembly to high-level languages. Somehow, I find that write a small OS in Assembly if far more easy and simple than in C or Pascal (am I weird?)
Assembly has a weakness at maintainablity, so I keep my code well-structured (avoid spaghetti code as much as possible), using well-defined procedure interface to negate that weakness.
But recently, I have learned a sad truth that with modern superscalar, pipelined cpu, it is very difficult to average hand-optimised Assembly code to make it run as fast as good compiler-generated code. Therefore my Assembly code will run slower than most optimised compiler.
Should I continue to code my system in Assembly language?
Re: Is it worth to write my system (small) in Assembly entir
Posted: Wed Aug 01, 2012 12:30 pm
by iansjack
It's your OS, so do whatever you want to. Although how you can find assembler easier than C for anything but the most trivial programs is beyond me.
Bear in mind that assembler, unlike C, is locked to the processor you have chosen.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Wed Aug 01, 2012 4:17 pm
by Congdm
Thanks, that's right, it is up to me to decided since no one care about a hobby os anyway.
About portability, I am fully aware of that drawback. So, before wrting the code, I design and write down the algorithm on the paper first and translate it to assembly code later. So even when I lost the code, I still have the design in paper and can reimplement it, even for another language (this method also reduces time for debugging considerably). Beside, buying an another machine with different architecture than Intel x86 is very hard for me.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 12:35 am
by iansjack
Congdm wrote:Beside, buying an another machine with different architecture than Intel x86 is very hard for me.
I don't know anything about your circumstances, but computers based on ARM processors are very common nowadays, and are cheaper than x86 computers. With emulaters such as qemu you don't even need other hardware to play with alternative processors.
I wouldn't completely rule out the possibility of other architectures.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 3:24 am
by Love4Boobies
Note that maintainability and portability aren't the only issues. With a HLL, you have the option of asking the compiler to micro-optimize for a specific microarchitecture. With assembly, you have two choices:
- To not micro-optimize but instead write the code for your chosen architecture as generally as possible. (But then what is the point of using assembly?)
- To profile your code and provide micro-optimized routines for all the microarchitectures of your chosen architecture that you wish to support. (This means lots of duplicated code.)
I suspect that the reason why you find it easier to write it in assembly rather than C or some other HLL is that you don't know how to set up a proper build environment and are too lazy to research it (we have articles on that, on the wiki).
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 4:21 am
by Antti
Congdm wrote:it is very difficult to average hand-optimised Assembly code to make it run as fast as good compiler-generated code
As an offtopic, my assembly code is usually quite unoptimized. I prefer the code being easy to read even it makes it slower. It executes extremely fast on modern hardware anyway. Of course, some critical parts are more optimized.
My kernel is much slower and inefficient when compared to highly optimized ones (or "normal" ones). The kernel itself uses memory quite inefficiently because modern computers have it and when looking the system as a whole it does not matter. It goes without saying that the efficiency and elegancy are not mutually exclusive but I am not capable of getting both. I personally find my kernel being elegant which is the most important thing because it is, well,
my kernel. Luckily, I work alone so I do not have to explain my ideas that may seem to be very unefficient (but have the rationale behind them). Well, the elegancy could also mean being highly efficient... well... maybe I stop talking. EOO (End of Offtopic).
Congdm wrote:Somehow, I find that write a small OS in Assembly if far more easy and simple than in C or Pascal (am I weird?)
No, you are not weird. Low-level things are easier when using an assembly language.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 5:04 am
by Congdm
iansjack wrote:I don't know anything about your circumstances, but computers based on ARM processors are very common nowadays
I knows that ARM architecture is popular nowadays but in developing countries (like my country), it isn't popular yet
. I really want to programming in other architectures like ARM, Power, MIPS, Sparc, monstrous Itanium, or even old architectures like System/360, Motorola 68000.
Emulation may help but I didn't like it, I like the feeling of real machine
Love4Boobies wrote:I suspect that the reason why you find it easier to write it in assembly rather than C or some other HLL is that you don't know how to set up a proper build environment and are too lazy to research it
You are quite right, but I am not as lazy as you say, setting up build environment isn't that hard
, but I don't like it.
Antti wrote:As an offtopic, my assembly code is usually quite unoptimized. I prefer the code being easy to read even it makes it slower. It executes extremely fast on modern hardware anyway. Of course, some critical parts are more optimized.My kernel is much slower and inefficient when compared to highly optimized ones (or "normal" ones). The kernel itself uses memory quite inefficiently because modern computers have it and when looking the system as a whole it does not matter.
I agree with you too, I focus on clear design, choosing good algorithms, using abstraction to make problem easier and managable. Maybe if I choose Assembly, my code won't run fast, but if it is clear and managable, what is the problem? It can't compare with big OS anyway. In my system, there isn't kernel, everything is module and can be replaceable. It can run only with basic Interrupt, Video, Keyboard module and a basic module loader, nothing else.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 9:49 am
by Love4Boobies
Congdm wrote:Love4Boobies wrote:I suspect that the reason why you find it easier to write it in assembly rather than C or some other HLL is that you don't know how to set up a proper build environment and are too lazy to research it
You are quite right, but I am not as lazy as you say, setting up build environment isn't that hard
, but I don't like it.
You seem to be okay with making a big mistake before even starting. What chance does your project stand then?
You are probably under the impression that it doesn't really matter, that the project is the actual code, that from now on you will make good choices, that in fact it will be manageable, etc. These problems are extremely common with inexperienced people. I used to think these things myself. Fortunately,
books have been written for those who wish to avoid these mistakes.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 10:37 am
by Antti
Love4Boobies wrote:You seem to be okay with making a big mistake before even starting.
I agree that using just the assembly is not a good idea in a long run. Meanwhile, I would say that it does not matter as far as the
first operating system related project is concerned. It may actually be a good idea to use just the assembly there.
After gaining some experience, you can start your project from scratch. With a proper build environment.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 11:44 am
by Congdm
Love4Boobies wrote:You seem to be okay with making a big mistake before even starting. What chance does your project stand then?
I am just joking about that lazy part. Of course I can not stick with Assembly forever, even if I can, almost all sane people don't like coding in Assembly, so if I choose only Assembly, most people will never involve with my project. (A bit off-topic: my question is with small project, is using all assembly languages practical, not with big project)
Unprove_ranting:
Instead, I designed my system with structured modules and clear interfaces, forbiding cross-dependence so modules can be replaceable individually. Anyone can use any languages to write/change a module with requirements: conform to the binary interface and design in a structured way. With that design, when I code in high-level languages, I can still reuse my old Assembly codebase easily without any problem. That is Replaceable part.
The main reason that I design my project in that way is: There is no perfect design, and never will. We can make it better by changing it but can not make it perfect. So no design is static, it always evolve to something better. The only stable thing is changing, therefore I design it with ability to change but can inherit its ancestor when need.
End_ranting
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 1:17 pm
by sandras
Congdm wrote:
The main reason that I design my project in that way is: There is no perfect design, and never will. We can make it better by changing it but can not make it perfect. So no design is static, it always evolve to something better. The only stable thing is changing, therefore I design it with ability to change but can inherit its ancestor when need.
I think this is a pretty good reasoning. I find it very important to keep in your head that nothing is perfect. To answer your "Is it worth to write my system (small) in Assembly entirely?" question:
I say use C, like most do. And they do for a reason. It's portable AND fast. Even if it ends up not as fast as assembly, I think it is the language with the best speed/portability and low/high level of abstraction ratio. I somewhere read that ISA's should be made for compilers, not for people. I think that is very true.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 1:33 pm
by Griwes
That "C is not as fast as assembly" statement is getting obsolete, as modern (and even not-so-modern) compilers are mainly optimizers...
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 1:39 pm
by Nable
And in most cases highly optimized asm code is unreadable, so when you use pure asm you have trade-off between speed and ability for other people to study and support your code.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 7:17 pm
by Love4Boobies
Antti wrote:Love4Boobies wrote:You seem to be okay with making a big mistake before even starting.
I agree that using just the assembly is not a good idea in a long run. Meanwhile, I would say that it does not matter as far as the
first operating system related project is concerned. It may actually be a good idea to use just the assembly there.
After gaining some experience, you can start your project from scratch. With a proper build environment.
I've heard this argument before but it doesn't really make sense to me. Here's how I read it: He's learning and should thus be encouraged to do wrong things. The whole point of learning is to find out how to avoid the wrong things.
Re: Is it worth to write my system (small) in Assembly entir
Posted: Thu Aug 02, 2012 11:01 pm
by Congdm
From all responses, I concluded that writing in Assembly entirely is a bad idea. But for now, I decided to continue with Assembly. I consider that as a challenge to test my designing and programming skills. Thanks for response.
Also:
Nable wrote:The whole point of learning is to find out how to avoid the wrong things.
In my opinion, the main purpose of learning is to
correct wrong things.