Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have read that the executable file contains some information for the operating system to use before running it. rest it is like any other independent application just using the services of the operating system.

my question is that if the compilers generate code dependent on the operating system we are using, then how can we use that compiler to make altogether new operating system from scratch.

Yes we can use assemble language but we just can't write the whole coding in assembly language.

Please tell me if i am wrong somewhere (or even worst 'everywhere'). Help....
Thanks
Posted

When a PC is statered it goes through a boot sequence, and at the end it is able to locate a boot sector on a bootable device, such as a hard disk. Whatever is found there gets loaded into memory by the bios. Then the bios transfers execution to whatever it loaded into memory.

This[^] describes how Windows does it, and here is a description of the linux startup process[^].

What we call programs, are, as Vitaly mentions, files that are written in a specific file format - usually created by a linker - and the boot loader needs to be able to load and execute files written in the choosen format.

Back in the days of 16-bit MS-DOS, compilers and linkers where able to output executable files in something that was called the com format, a simple executable format with no header - the program entry point, that is the first instruction the program was to perform, was located right at the beginning of the file - ideal for writing a simple boot loader.

[Update]
The process is detailed here:
How to develop your own Boot Loader[^] - isn't CP wonderful :-)

Best regards
Espen Harlinn
 
Share this answer
 
v3
Comments
Vitaly Tomilov 24-Jun-12 6:36am    
Correction: COM format wasn't that ideal for a boot loader, because of the starting address in the beginning that it came with. When I tried to use it as boot loader, I found it wasn't easier than EXE, for you just need to specify the exact starting address and size of the block to write into the boot, which made no difference between COM and EXE. Oh, those days with assembler :) COM Format.
P.S. This is just for fun :)
Espen Harlinn 24-Jun-12 6:42am    
It's been a few years, but it appears that we dabbled at OS development at the same time ... :)
suvam8694 24-Jun-12 10:20am    
Thanks a lot sir, it is exactly what i needed.
Espen Harlinn 24-Jun-12 10:24am    
That's great - you are venturing into a strange and wonderful territory, my best wishes for your effort :-D

I didn't know much the first time I tried my hand at this, but I certainly learned a lot that's been pretty useful over the years ...
suvam8694 25-Jun-12 3:28am    
Thank You!!!
EXE files, just like any other binary file, contain a header with information in it specific to the file. Here's the EXE file header on Windows[^]

When a high-level compiler is used, rather than assembler, it sets the starting point to additional initialization logic, like dependencies and standard libraries' initialization. Read this[^] to understand it better.

Some languages, however, like C++, allow overriding that initialization, or at least in part. In VC++, for instance, there are many pragma and __declspec modifiers that allow custom compiler and/or linker behavior, plus many project configuration switches.

We absolutely can, and people do write the whole thing in assembler. In fact, back in 1993 I personally wrote a whole mini OS for 286 just in TASM, as an academical exercise. It was booting off a floppy drive, initialized DPMI, with basic memory management and UI.

However, today this cannot be a recommended approach. Only specific parts these days are written in assembler, while most of OS is written in ANSI C, though considering the progress, one can already find OS kernels written in C++, which is perhaps acceptable in some cases. But hey, who am I to judge them - look at guys writing OS in .NET: Develop Your Own Operating System in C# or VB.NET[^]. I personally think that's just crazy, but that's me.

There are some good tutorials out there on how to get started with your own OS in C, even on this forum: Building your own operating system[^] ;)
 
Share this answer
 
v6
Comments
Espen Harlinn 24-Jun-12 6:22am    
5'ed!
SoMad 24-Jun-12 6:40am    
Good answer, +5.

Soren Madsen
Ashish Tyagi 40 25-Jun-12 3:33am    
Good one, my 5.
BillW33 25-Jun-12 9:47am    
Good answer, +5
Sandeep Mewara 26-Jun-12 14:05pm    
Good answer 5!
That is the reason we have platform independent framework like Java and .NET.

Java solves the problem of platform-independence by using byte code. The Java compiler does not produce native executable code for a particular machine like a C compiler would. Instead it produces a special format called byte code. Java byte code written in hexadecimal, byte by byte, looks like this:
CA FE BA BE 00 03 00 2D 00 3E 08 00 3B 08 00 01 08 00 20 08

This looks a lot like machine language, but unlike machine language Java byte code is exactly the same on every platform. This byte code fragment means the same thing on a Solaris workstation as it does on a Macintosh PowerBook. Java programs that have been compiled into byte code still need an interpreter to execute them on any given platform.

The interpreter reads the byte code and translates it into the native language of the host machine on the fly. The most common such interpreter is Sun's program java. Since the byte code is completely platform independent, only the interpreter and a few native libraries need to be ported to get Java to run on a new computer or operating system. The rest of the runtime environment including the compiler and most of the class libraries are written in Java.

Hope, it clarifies.
 
Share this answer
 
Comments
suvam8694 24-Jun-12 4:50am    
It was helpful but what that basically means is that if i want to make my own Operating System then i would need a pre-installed JVM or .Net framework.
Linux is has most of its kernel code written in C. what about it? what compiled that source code to make it independent of OS and in fact made it an OS itself.
Vitaly Tomilov 24-Jun-12 5:48am    
One only wonders - what does it have to do with writing an OS or EXE header, which was asked?
Espen Harlinn 24-Jun-12 6:21am    
I did not downvote your answer, but please reread the question. This may be an answer to something, but it has no relation to this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900