How to make a simple operating system

Source: Internet
Author: User
Keywords Program Ape know
Tags compilers course cpu display display device example function helpful

Actually, the operating system is not a clean problem, I personally think that writing compilers are more helpful to study and work (big, bigger, bigger), and more clean (better than clean) and better written (than good writing), of course, the operating system seems to be very inscrutable. (and quite useless ...) I think the main reason is that many people do not learn the CPU principle, feel that the bottom of everything is so magical, and infinite rendering amplification "bottom Master theory" ... Most of the students who learn EE are not still in the pit.

If you insist on writing the operating system ...

My suggestions are as follows:

Learn and understand the CPU, design a set of instructions yourself, this is mainly to let you understand how the computer works, in the absence of OS how to use.

You'd better be able to write an emulator for this instruction set, because you don't have enough theories and principles.

If you want to be simple, you can use the stack-based model, but in fact the register-based model is more convenient and familiar.

Learn OS should not use x86 this kind of industrial product, too dirty too miscellaneous, even if use also can use only a subset. So if you write the CPU simulator for the custom instruction set above, you can throw away the x86.

If you do not throw, use x86 also do not have to look at what protection mode and other things, dirty their hands.

——————————

A simple OS should consist of three parts:

Process Management

Memory management

Driver

--Process Management--

Theoretical knowledge does not form, in practice, you should implement these two functions: _execute and _exit

And if you implement a single process OS, that's easier, and many processes are more menial jobs.

_execute is used to open a process by saving the current register state, setting the new process properties, and then letting the new process start executing.

_exit is used to end a process by ending the current process and restoring it to the state of the previous process.

The state is stored in a particular memory area, which, according to x86, manages the memory in a stack of registers called SP

--Memory management--

Memory management is also two functions: _alloc and _free

Memory management is actually very simple, _alloc, write down the size and address, put in a linked list, _free, traversing the linked list, the corresponding record marked, this theoretical knowledge also said, it is best to use a packing problem that a set of solutions, but in fact you will be able to bare. Anyway, it's a simple operating system, and the principle is much more important than the algorithm.

So the management of memory enough, and then advanced point of view, now the OS with paging function, this theory also wrote, you see it is, say a few days ago also dream of the page replacement algorithm again ... This algorithm in my dream image is a radish slice ...

--Driver--

Display device:

Now the display device is raster (raster) technology, grating is actually pixel point. The screen is made up of countless pixel points.

Use a section of memory as the source of the display device, such as the display is 400*300, regardless of color, with a byte to represent a point, it is necessary to open a 400*300 of memory when video memory, display devices each frame through the memory of the scan to achieve the display function.

Your display device driver needs to implement the function is also very simple, roughly is _drawpixel, _drawline, _drawrectangle, _drawcharacter and so on.

Here are a few things you can look at I wrote an example of an answer inside

Can the address be able to design an ultimate fool programming software that allows ordinary people to program perfectly? -Know the answer from the user

Of course, that example is too simple, because it is a pseudo raster, in fact, draw line algorithm to trouble a little, this graphics book will have to write, but you find a blog to look at the good, very simple, nothing more than a set of formula calculation, no need to think. If this is written in Chinese, it is difficult to debug the correct ... Not as good as compilations. So my example uses a pseudo grating ...

The way to display characters is this, for each character, you have to draw a picture of the character (the simplest is a m*n pixel), and then the implementation of the _drawcharacter function is to copy the image of this character to the video memory (you imagine, realize what I said dirty means ...) )

Input device:

Keyboard, a section of memory space mapped to the key, through the operation of this memory to implement input.

Of course, there are file system ah what the mouse ah, but in fact no file system is nothing, the original hard disk is not the standard computer, the mouse is not necessary, and the mouse to cooperate with the GUI or character GUI to use, it is more dirty, so it. Simple operating system

You might think what I said was strange, and the book is not the same, but in fact, the operating system is so, the book is too deep too detailed, I am not saying that writing is not good, the book very well, very theory, but you should learn to read the meaning behind it.

The book will tell you what it is, what it is, and why it is, but you have to learn to think "what if it doesn't?" 」

OK, now you can implement a Tetris on this operating system (in fact, the greedy snake is better, let me write Tetris instead of Killing me ...) To fool the teachers.

Or they'll ask you, "Is this dark?" Write something! 」

But here is the sincere proposal landlord, do not write operating system, pay much more than in return, write compiler.

You're young, you want to write, you can't write later

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.