Should OS be written in C , C++ or Assembler?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
dany_gates_universal

Should OS be written in C , C++ or Assembler?

Post by dany_gates_universal »

hi, i would like to know which one of the following is good for OS development from all points of view : assembly? C? or C++?
Actually i want to do my OS in C++, but i have to question to ask,
can a whole OS be done from start(very start , leaving to boot loader) to the end(till the last bit i code) in C++ without using assembly or C and still using all the features any x86 microprocessor has to offer?

my prob is that i just wanna stick to ONE lang throughout OS dev!
So which lang do u think i should use?
Moose

RE:Should OS be written in C , C++ or Assembler?

Post by Moose »

"can a whole OS be done from start(very start , leaving to boot loader) to the end(till the last bit i code) in C++ without using assembly or C and still using all the features any x86 microprocessor has to offer?"

Simple answer, no.

Your OS WILL require some assembly in there even if you use C or C++ or a combination of both.

This is because assembly is required to perform certain actions that C and C++ simple can not do. Such as specific machine level instructions.

As for C and C++. Both can be used to make an OS. The only different is some featurs of C++ called virtual functions can not be used because they require the OS. And in C you can't use all the libraries since most are provided by the host OS in some way (and you are the OS!).

Also, not too sure if you know this but your OS kernel can not be one whole file from start to finish with the bootloader included. The bootloader must be 512kb in size, a kernel obviously will not fit in this space. On top of this, an OS is not one file either. Rather when we reference a file we're talking about the kernel, the 'engine' to the OS, but other files exist too, such as drivers, systems software and programs that must exist to make up the OS.

Personally, my OS is written in all assembly, and i can say its tough, not to program, but to find the time to sit down and write it all. Assembly can not be written up in a couple of hours.
Bugs and errors are a pain to uncover. The amount of hours i've spent looking for them in my code looking for reasons why it won't work, only to find out the answer is i did not clear the upper word of a register that would require only a single instruction :s.

Moose.
dany_gates_universal

RE:Should OS be written in C , C++ or Assembler?

Post by dany_gates_universal »

Thank u for ur answer! first of all i know full OS is not a one file or something . Anyways lastly tell me can a whole GUI like windows be developed using Assembly?
How much percentage of speed gain would i gain if i use assembly instead of C or C++? To start coding my OS in assembly do i really have to be a VERY GOOD assembly programmer (so that i can really get edge over C/C++ in terms of speed)?
Moose

RE:Should OS be written in C , C++ or Assembler?

Post by Moose »

"can a whole GUI like windows be developed using Assembly?"
Absolutely. Anything can be written in assembly (as far as i am aware).

"How much percentage of speed gain would i gain if i use assembly instead of C or C++?"
I think assembly is roughly 10% faster than C but dont quote me on that, im not sure. You should check thats right from another source.

"To start coding my OS in assembly do i really have to be a VERY GOOD assembly programmer"
Not really. When i started developing my OS. i didn't even know assembly, i just started learning. I've read books on assembly and they recommend you understand C and C++ first to be able to learn assembly, i still do not know C/C++.
People say its really difficult to do assembly, personally i find it easy. Just a bunch of instructions, the difficult parts are debugging (but a good print routine can ->> REALLY <<- help you here) planning the steps you're taking to get from problem to solution - problem solving.

Moose
dany_gates_universal

RE:Should OS be written in C , C++ or Assembler?

Post by dany_gates_universal »

Finally Which assembler do u think i should use(stick to) while developing my ?Since ur such a expert in assembly ur opinion is very imp for me!
I have both Linux RH9 and Windows ! Which is better nasm or MASM32?
Personally i think MASM32 has good IDE and documentation, but nasm i think will make me take some extra efforts(i mean type i a editor, then assemble in console)!
Technically which is better ?
Suggest me some good OS Developement Book,which will get my concepts clear from ground.

Finally if i wish to write a OS for 64bit micrprocessor , what things will i need?
Will my 32bit code can be reused for 64bit OS , if atall in any way?
Moose

RE:Should OS be written in C , C++ or Assembler?

Post by Moose »

"Which assembler do u think i should use?"
I have been developing with nasm but i do want to switch to another assembler called yasm. Its nasm compatible and can produce 64bit code.
Masm i can't say i've tried so i dont know about that one.
Technically speaking i dont know which is better. nasm does produce good small outout of files. I've used yasm once to try it out and when assembling the same code my files and subsiquent kernel is larger than that made when using nasm.

"I have both Linux RH9 and Windows"
Same here :). Soon to switch to fedora since Redhat professional series is being discontinued.

I use linux to program under for two reasons. My windows PC's monitor is only 14" which i dont like programming on, having to scroll around the screen and so on, and because my os is assembled/linked using the elf file format and djgpp port of ld does not support elf unless its recompiled, and it's a lot of hassle to recompile. I just wanna write my OS!

As for books. I have The indispensable Pc hardware book (not really THAT indispensable to be honest), Operation Systems by John O'Gorman - A Grassroots series books aimed at students. Is mostly theory of how os's work in general. Not too bad, has chapters on processo managements, concurrency, memory management, i/o management and security.
I've been reading a book called Mmurtl v1.0. It now has a different name but doing a search for that should bring up some results. It descibes an OS called Mmurtl and how the developer made it.
I would say visit osdever.net for a list of useful books but i've tried and it seems to be down. They say on their site, it should be back up by the end of April?!?!

I can say the most important books you'll ever need are the intel specs from intel.com. I think some links are the in Development Links at the top of this page.
They are called
Intel IA-32 Architecture Software Developers Manual. Three of them to collect.
Volume 1 - basic architecture.
volume 2 - instruction set reference.
volume 3 - system programming guide.

Concepts are difficult to know from the ground unless you know what you expect your OS to do. The Megatokyo OS faq should help you here.
http://mega-tokyo.com/osfaq2

Two programs you might be interested are Umbrello and Visual Paradigm for UML CE, two UML editors that could be useful for you in designed your os.
umbrello is released with kde3.2 available from sourceforge for 3.1 users.
VP for UML CE can be found at http://www.visual-paradigm.com/

Moose
dany_gates_universal

RE:want to get some basic concepts clear?

Post by dany_gates_universal »

most of my questions have been really been satisfactorily answered, but few haven't beign answered  :(a huge list of question follows...be prepared!sorry!)
1)once i make my os in assembly , can i be sure it will run on all IA-x86 machines?
2)can i my use source code written for 32bitOS for new 64bit OS? with few minor changes here and there? all will i have to write entire code again from scratch?
3)can i make my OS hardware independent(for use on any IBM compatible PC), if i only use BIOS functions for my OS, without directly using the hardware?
4)I have heard abt Hardware Abstraction Layer in case of Linux,what does it do exactly?
5)If i have to make my OS run on every x86 machine i will have to use only those instructions which are supported by i386,so how will i be able to use other Pentium instructions which make them faster or superior to i386? What can i do so that i can use all the instructions available in latest PIV or AMD Athlon and  still be able to run my os on any microprocessor?
Moose

RE:want to get some basic concepts clear?

Post by Moose »

1)once i make my os in assembly , can i be sure it will run on all IA-x86 machines?
Yes. Providing it is the IA32 standard. i.e. All Intel/AMD 32bits standard(386, 486, P1,2,3,4,celeron, AMD tbird, XP, Duron and so on).

2)can i my use source code written for 32bitOS for new 64bit OS? with few minor changes here and there? all will i have to write entire code again from scratch?
The code you write in 32bit will work on AMD64's. the AMD64 cpu's are fully backwards compatible to 32bit software (just like 32bit cpu's are backwards compatible to 16bit cpu's). You won't however be able to use the same assembled code within 64bit mode. Firstly, because its assembled to 32bit instructions. If you use yasm to assemble it as 64bit code then your 32bit instructions should work. And second, 64bit has a different way of working, just like 32bit protected mode compared to 16bit real mode. Some 32bit instructions will work, but not all, mainly the special system instructions that are expected to be run by the kernel.

3)can i make my OS hardware independent(for use on any IBM compatible PC), if i only use BIOS functions for my OS, without directly using the hardware?
You could, ish. BIOS functions are only available in Real mode (and another mode called unreal mode i think). They aren't available in protected mode. This means, when (assuming you are) you jump to the 32bit protected mode you wont have the bios to hold your hand. You have to write your own set of 'bios routines' and use the hardware by controlling them directly, commonly through drivers you write.
Also, not all bios's are the same. Some provide this set of functions, others provide another set. Goto Ralf browns interrupt list and check out the bios section.
http://www.ctyme.com/rbrown.htm

4)I have heard abt Hardware Abstraction Layer in case of Linux,what does it do exactly?

quote from: http://whatis.techtarget.com/definition ... 24,00.html

"In computers, a hardware abstraction layer (HAL) is a layer of programming that allows a computer operating system to interact with a hardware device at a general or abstract level rather than at a detailed hardware level. Windows 2000 is one of several operating systems that include a hardware abstraction layer. The hardware abstraction layer can be called from either the operating system's kernel or from a device driver. In either case, the calling program can interact with the device in a more general way than it would otherwise."

5)If i have to make my OS run on every x86 machine i will have to use only those instructions which are supported by i386,so how will i be able to use other Pentium instructions which make them faster or superior to i386? What can i do so that i can use all the instructions available in latest PIV or AMD Athlon and  still be able to run my os on any microprocessor?
If you were writing C/C++ code, you could tell the compiler to produce output designed to run on a particular machine. e.g. 386, 486, 586, 686, athlon. And the appropriate instructions (including performance instructions) to that target cpu architecture would be used.
In assembly however, you would have to either write your code to a specific target of hardware or make your code 'detect' what cpu it was running on before it does anything special, then run the appropriate code that can be used on the cpu. The cpu can be detected using alignment check bits and the cpuid function. By reading the AMD/Intel manuals, you can write the code that will perform this.

Moose
dany_gates_universal

RE:want to get some basic concepts clear?

Post by dany_gates_universal »

as in previous discussion u said :
"If you were writing C/C++ code, you could tell the compiler to produce output designed to run on a particular machine. e.g. 386, 486, 586, 686, athlon. And the appropriate instructions (including performance instructions) to that target cpu architecture would be used"

but when u are developing a OS how the hell would u know on which platform(i.e 386 ,486,,,etc) will ur OS be running. i mean how can u tell the end-user will be using which microprocessor, u cannot expect the enduser to compile the source everytime he changes his processor:)
How does Windows overcome this problem, coz i haven't heard anyone compiling windows source anytime, so how are they able to use all the instructions and still  make sure their os is usable on all architechtures? do they  check everytime for CPU type when using some special instruction.Thats so cumbersome to do!

and abt 64bit thing , i dont have AMD Athlon64 with me , but want to develope a OS for the same, so can i use bosch for emulating Athlon64 machine and then write my code in yasm for AMD64 and test it in emulation mode? can a full OS be developed in this way?

is there a c/c++ compiler available for AMD64?

as u said previously we cannot use bios functions in pmode, and that we have to write drivers ourselves,does that mean we have to know specifications of each and every hardware that is already there and is coming out there once everyday? can we work around this problem by making our OS use microsoft device drivers in ur OS, by this we will get a hell lot of drivers,right:)

abt some extra info , i have heard intel is going to bring their new 64bit processor on similar architechture that AMD64 uses, is that correct? if thats so then i should be writing my OS for AMD64 rightaway, what do u think?
Moose

RE:want to get some basic concepts clear?

Post by Moose »

I can't vouch for windows, i dont know how they handle architecture versions. Linux on the other hand, when you run the installer to any modern distribution, the ditribution installer will detect what os you're running and install the necessary pre compiled rpm. Usually you get a 386, 586 and 686 and athlon rpm package.

I beliece you can use Bochs to emulate AMD64 yes. You can also use another emulator called Simics. For more information on AMD64, goto www.x86-64.org. Simics can be found at www.simics.com

Don't know about C/C++ compilers for AMD64. Check what the gcc compiler collection package can handle.

Having to know all about hardware specifications? Yes you would.
Most hardware operates in the same manor and a serious amount of todays pc's internal system hardware (e.g. timers, floppy controllers, ATA controllers, PIC, APIC etc) all operate as they did 10 year ago so that should not create a problem.
Other items of hardware, for example my Creative SoundBlaster 5.1 SoundCard, would need specialist drivers as one sound card is generally speaking not the same as the next.
Using Windows drivers? I suppose yeah, but does Windows put patents on their drivers code designs? Do you think they would mind you stealing their drivers for yourself?

Intel are releasing another processor yes, it is designed to follow AMD's 64bit cpu. They do already have a 64bit cpu, called Itanium i think, but its not 32bit backwards compatible but rather a brand new cpu design. This obviously does not go down too well for the average home user who now can't run their usual apps the way they did if they had it with WindowsXP.

Moose
dany_gates_universal

RE:want to get some basic concepts clear?

Post by dany_gates_universal »

abt the windows driver thing, do u mean there isn't anyway we can use windows drivers? What if we have a built-in program in our kernel which converts a windows driver to MyOs driver dynamically or when request by user, so does this free us for using win drivers. Why i am stressing so much on win drivers is coz, main problem of any os is in its driver avaibility! Look at linux, though its such a good OS but still most of the sound cards dont work for it, same is the case with winmodems, i am still unable to get the right drivers for my modem , so cannot access net from linux, have to come to win everytime for net access!that so cheap:)

next abt the bios functions,are bios functions really worth ,so that we need to use them by swithcing to Real mode everytime  we want to use BIOS functions and then switch back to PMode for normal operations?
isn't there any way to using bios functions becoz anyways they are gonna be there in ROM sleeping ! without any work , so why not use them!right:)
so do i really need to use bios functions? if yes -:how can i use them?

i am still not clear abt Hardware Abstractions Layer, where can i get more info abt it? Google is not showing good results! wanna suggest any site?

Just to get it Confirmed abt AMD64 thing! Are u sure u said Intel is planning to follow AMD64 design for their new Intel 64 CPU? i mean have u heard abt it on any site or just heard it from ur friends. becoz i want to design my os for AMD64 only if intels gonna follow it.
Moose

RE:want to get some basic concepts clear?

Post by Moose »

Windows Drivers. Im saying, Microsoft might not like you stealing their drivers. By all means, steal them, but if your OS gets too big im sure they could quite easily complain. I mean, look at Lindows, and all they did was support windows tasks.

Bios functions can ONLY be accessed from real mode. If switching between real mode and protected mode then that's your decision. But not all bios's have support for all the bios ints you might want to use, and using bios ints aren't as efficient as just using the hardware directly with your own device drivers. If you want to use them look at this site about known bios ints
http://www.ctyme.com/rbrown.htm - Look through the Interrupt section.

HAL. From what i understand is its a simple way of connecting applications to devices without all the complications of ports and so on. Take linux for example, devices are referenced through the file system in the /dev/ folder. e.g. the first serial port is /dev/ttyS0. If im a program, i can pipe messages to whatever device, say my old 56k modem, by writing to that file. No ports or complicated bit arrangements to handle because the OS hal layer handles it for the me.

AMD64 and intel. I heard it on this board i think, but here's an annoucement on pcworld site (not the superstore but a review site).
http://www.pcworld.com/news/article/0,aid,114820,00.asp

Moose
dany_gates_universal

RE:want to get some basic concepts clear?

Post by dany_gates_universal »

Now that other things are done, lets think abt File Systems.

Can i use NTFS file system in my OS, or will Microsoft have problem with this also? Was NTFS developed by MS?
Is NTFS good filesystem? Do do u think there is other good file system out there?
If i choose to use NTFS is there enough documentation available out there on NET for this FS?

Another question i have is after i complete my os and want to get all its rights(i.e rights of use and copying and etc)for myself, what do i need to do?
Do i have to Copyright it? or what? Is it possible to copyright a OS against a single individual? or some kind of company(business) is needed?

Since i have decided(don't know how much successful i will be!:)) that my OS will be base on AMD64 architechture , will AMD64 Architechture Manuals,alongwith some other manuals like "Manual for Kernel and BIOS developers" provide enough information for understanding AMD64 architechture,and for programming for AMD64 or anything else is needed?

Lastly some personal question : What do u think is the future of 64bit computing? And more specifically future of AMD64?
Moose

RE:want to get some basic concepts clear?

Post by Moose »

NTFS was developed by MS yes.
They have not released the documentation for it but you can get some reversed engineered docs from the net that were used within the linux kernel driver for ntfs.
NTFS is not the best fs around. The best fs is probably xfs.

You can copyright material in the UK and US (not sure about other countries) simply by putting your name and a copyright tag on it providing its your original work. You can go one step further and register your software as copyright to yourself but that isnt required to get copyright but is better as record to your work and you for reference by others.

The AMD64 manuals can be found on amds site or possibly www.x86-64.org.

64bit computing? Its good for servers. Workstations and desktops is another matter. The question is what can 64bit cpu's provide to a home user or company workstation what a 32bit cpu can't? Yes its faster but with it being more expensive to buy the cpu than 32bit cpu's and requirements for a 64bit os to accompany it, getting a 64bit cpu over 32bit when the tasks the user will be performing can be done on 32bit means the 64bit cpu isn't going to get into machines.
Whats really needed is package pc vendors uptake on 64bit. If package pc vendors started selling amd64's then the general population of end users would have them, meaning more software would be written for them, and further uptake of the hardware.

Moose
dany_gates_universal

RE:want to get some basic concepts clear?

Post by dany_gates_universal »

hi,
    what do u think how much time it takes to build a professional OS like Windows? For example with help of two or four guys how much time it will take for me to build a good OS?:) Though i cannot give my full time to OS developement, i have college to attend , and can give around 5-10hrs a day for OS developement.
What do u think of target of 4yrs,.............ya well i know linux took 10yrs for completion! But u know "NOTHINGS IMPOSSIBLE" :)

     previously u told me u were installing Fedora. So have u installed the same?
How is it? is it cool? mean does it have anything extra than RH9? does it use kernel 2.6? My friends got the fedora , so i am giving thought to install fedora over RH9. Is it worth replacing RH9? does it have nasm and other programming stuff, so i can do my OS dev work on it?
       i know by this time i must have got in ur nerves:) i have asked so many questions!
        we have been talking for so long, tell me something abt ur self( if u dont mind). well i am doing Electronics and TeleCommunication Engineering Diploma in
MIT!(GOT SCARED?) take a deep breath NOT USA MIT , its a well know indian institute for education.though i am thinking of changing my branch to Computers , after i take admission for Engineering Course.
Post Reply