Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux 0.11First time | the first two lines of code

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >

Share

Shulou(Shulou.com)11/24 Report--

This article comes from Weixin Official Accounts: Low Concurrent Programming (ID: dipingfa), Author: Flash Guest

From the beginning of this article, you will follow me into the dream journey of this operating system!

Don't worry, each chapter will be very small, and you don't have to bear a lot of burden to learn, just like reading a novel, follow me chapter by chapter.

Not much to say, straight to the point. When you press the power button, the firmware program BIOS written in advance on the motherboard will copy the 512 bytes of data in the boot area of the hard disk to the location 0x7c00 in memory and jump to that location for execution.

The definition of the boot area is very simple, as long as the last two bytes of the 512 bytes of sector 0 in disk 0 are 0x55 and 0xaa respectively, then BIOS will consider it a boot area.

So for us to understand the operating system, the BIOS at this time is just a code porter, moving 512 bytes of binary data from the hard disk to memory. Therefore, as an operating system developer, you only need to compile and store the initial code of the operating system in disk 0, track 0 and sector 1 of the hard disk. The BIOS then puts it in memory for us and skips to execute it.

The very first code in Linux-0.11, bootsect.s, is written in assembly language and is located in the boot folder.

By compiling, this bootsect.s will be compiled into a binary file and stored in the first sector of the boot area.

Then, as just mentioned, the BIOS will move to the memory location 0x7c00, and the CPU will start from this location and continue to execute one sentence after another without thinking.

So our dream journey starts with the first line of code in this file!

mov ax, 0x07 c0mov ds,ax Okay, look at two lines in a row.

This code is written in assembly language, meaning to copy the value 0x07 c0 to the ax register, and then copy the value in the ax register to the ds register. Well, actually, the result of this is that the value in the ds register becomes 0x07 c0.

ds is a 16-bit segment register, specifically representing the data segment register, which acts as a segment base address when addressing memory. What does that mean? That is, when we write a memory address later in assembly language, we actually only write an offset address, such as:

mov ax, [0x0001] is actually equivalent to

mov ax, [ds: 0x0001]ds is added by default, indicating that at the base of ds, offset 0x0001 units later, the memory data at this location is copied to the ax register.

Image analogy is, you and friends discuss where to play is better, you say Tiananmen Square, Nanluoguxiang, Summer Palace and so on, in fact are offset addresses, omitting the base address of Beijing City.

Of course, you can say Beijing Tiananmen Square, Beijing Nanluoguxiang such, each time with the prefix Beijing. However, if you agree with your friends in advance, the places I say below are all in Beijing City, ha, then you don't have to bring the word Beijing City every time, is it very convenient?

That ds this data segment register function is such, convenient to describe a memory address, you can omit a base address, nothing magical.

ds : 0x0001

Beijing City: Beijing

See, this ds is assigned to 0x07c 0, because x86 in order to allow itself in the 16-bit real mode to access the 20-bit address line this historical factor (do not understand this first do not tangle why), so the segment base must first shift four bits to the left. If 0x07 c0 is shifted four bits to the left, it is 0x7 c00, which is exactly the same as the memory address 0x7 c00 where this code was loaded by BIOS.

That is to say, the memory address of the data accessed in the code written later is added by default to 0x7c00, and then addressed in memory.

Why add 0x7 c00? This is very easy to explain, BIOS provisions dead to load the operating system code into memory 0x7c00, then all kinds of data inside are naturally offset so much, so set the data segment register ds to this value, convenient later through this base address way to access the data in memory.

OK, quickly digest the previous knowledge, then this article ends here, only two lines of code, knowledge is very small, I did not lie to you.

I hope you can do it, the BIOS will load the operating system code into memory 0x7c00, and we will change the default value of the data segment register ds register to 0x07c0 through the mov instruction. These two things are recognized in the heart, and there is no doubt, which is convenient to continue later.

The world behind is getting more and more exciting. If you want to know what will happen next, listen to the next decomposition.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

IT Information

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report