Mentioned above
Made"
Difficult"
Decide. This book is in 2009
Year 11
Month 22
It has been more than a year since I bought it every day, and most of the time it is sleeping. After all, it is "x86
Assembly"
And"
Operating System"
It sounds quite old and mysterious! Previously 80x86
In the assembly language lesson, I have experienced it in the fog. To learn it well, I must be prepared to fight for a long time and fight hard.
Now I have come to Huaqing vision embedded long-term class training. It is rare to study the operating system in a leisurely manner. (There are a lot of courses and you are busy with it)
Although the operating system in this chapter is not even the embryo, I believe that in the author's"
It is our tactics to step by step, and a sense of accomplishment is our purpose"
Under the guidance of the sparrow, a day will grow into a big sculpture.
Let's just remove the bottom of the bottle, replace it with the harbo telescope, and look at the OS together.
Those things! (
Recently, I gave up my glasses ^)
Write the smallest"
Operating System"
Although everything is difficult at the beginning, sometimes it is not. For example, writing a practical operating system is a daunting task, but a minimal operating system may be easy to implement. Now let's implement a smaller one.
Operating System "......
1.1
Preparations
For program writing, preparation is nothing more than hardware and software:
1.
Hardware
One computer (Linux
Operating system or Windows
Operating system )(
My environment is Window XP + vmware6 + ubuntu8.10
)
A blank floppy disk (
It's hard to find this stuff now!
)
2.
Software
Assembly compiler NASM (
Why choose it?
)
Floppy Disk absolute sector read/write tool, Linux
Available dd
Command
1.2
Operating System completed in 10 minutes
Org
07c00h
;
Tell the compiler program to load to 7c00
Location
MoV ax, CS
MoV ds, ax
MoV es, ax
Call
Dispstr
;
Call the display string routine
JMP
$
;
Infinite Loop
Dispstr:
MoV ax, bootmessage
MoV bp,
Ax
; Es: BP =
String address
MoV CX,
16
; Cx =
String Length
MoV ax,
01301 H
; Ah = 13, Al = 01 H
MoV BX,
000ch
;
The page number is
0 (bH = 0)
Black Background red letter
(BL = 0ch,
Highlight
)
MoV DL, 0
Int
10 h
; 10 h
Number interrupted
RET
Bootmessage:
DB
"Hello, OS world! "
Times 510-($-$)
DB
0
;
Fill in the remaining space to make the generated binary code exactly 512
Bytes
DW
0xaa55
;
End mark
Save as boot. ASM
.
Compile: NASM boot. ASM-o
Boot. Bin
Don't worry, NASM
Not Installed yet!
1.3
Boot Sector
If you choose to start from a floppy disk, the computer will check the disk's 0
Surface 0
Track 1
Slice
If it is found to be 0xaa55
End
, Then BIOS
Think of it as a boot sector
. Once BIOS
If the Boot Sector is found, the 512
Bytes of content loaded to memory 0000: 7c00
,
Jump to 0000: 7c00
To this guide code.
1.4
Code explanation
1.
Square brackets []
In NASM
Any
Not square brackets []
The enclosed tags or variable names are considered addresses.
.
2. $
And $
$
Indicates the address after the current row is compiled; $
Represents a Section
,
In this program $
Actually 0x7c00
. Times 510-($-$) db 0
Indicates to repeat this byte by 510-
($-$
) Times, and the remaining space is constantly filled with 0
Until 510
Byte until the end sign 0xaa55
Occupied 2
Bytes, exactly 512
Bytes.
3. The netizen Hector explained in detail and directly quoted it as follows:
Table. msonormaltable {font-size: 10pt; font-family: "Times New Roman ";}
This code segment is a System Bootstrap program stored in the disk Boot Sector. If it is stored in the first sector of the hard disk, this code is called: Hard Disk Master Boot Record; if it is stored in the first sector of a partition of the hard disk, it is called: Partition Boot Record; if it is stored in the first sector of a floppy disk, it is called the Boot Record of a floppy disk. An important identifier of a disk Boot Record is:
DW 0xaa55
One sector of the disk is 512
Byte, Mark 0xaa55
The last word stored in this sector (two bytes, offset address: 1feh
), The rest of the space is used to store command code and some parameters,
Prompt information. Disk Boot Record by ROM BIOS
Int 19 h (
The boot loader is equivalent to a hot-start system. The shortcut key is CTRL + ALT + DEL)
, Fixed load memory 0000: 7c00h
And then give the control to the disk for boot.
Program, which is equivalent to starting to execute the following program segment. The functions of this Program Section are as follows:
Org 07c00h;
Adjust the offset pseudo-command org
, Specify the following command from 7c00h
Because the BIOS
Once the Boot Sector is found, the 512
Bytes loaded to memory 0000
: 7c00
Location
MoV ax, Cs;
Data transfer command, the code segment register CS
To the General Register ax.
MoV ds, ax; ax → DS
So that the data segment and the code segment are in the same segment
MoV es, ax; ax → es
In the same segment as the code segment.
Call dispstr;
Call the subroutine dispstr
, Displays the string information: Hello, OS world!
JMP $; $
Indicates the current address, implementing an endless loop
Dispstr :;
Subroutine: Display string
MoV ax, bootmessage; bootmessage
To ax
MoV bp, ax; bootmessage
First address to stack pointer BP
MoV CX, 16;
Number of characters to display
MoV ax, 01301 h; Ah = 13 H
, Int 10 h
That is, the video is interrupted for 13 H.
Function: Write a string; Al = 01 H
Indicates that the cursor position is updated after the string is written.
MoV BX, 000ch; bH = 0
, Page number (the video buffer is paginated), so beginners do not need to handle it temporarily; BL = 0ch
, Character display attribute, highlighted in black
MoV DL, 0; DH
, DL =
Cursor Position of the write string, DH =
Row number, DL =
Column number
Int 10 h;
Call video interruption
RET;
Subroutine return command, return caller
Bootmessage: DB "Hello, OS world !" ;
String information to be displayed
Times 510-($-$) db 0; $
Is the current address, $
It is the first address, which means from here till 510
Use 0
Fill
DW 0xaa55;
Disk Boot Record important marker, last 2
Bytes indicate that this is the Boot Sector.
1.5
Iceberg under the water
Every problem is a lock. You have to believe that there must be a key in the world that can be opened. You can also find the key.