Intravanic in the process of procedure - guiding sector
what? Is it a sauce chicken wings? ! Your eyebrow wrinkle - How does the company's work dining can not be a new pattern? The sky that blows air conditioners will also sweat, but you still have to endure this greasy. At this moment, you obviously need celery! This is like you deeply obsessed this job. In fact, how much you miss the scene after knocking out a line of ordered a command, how proudly uses the colored pattern used to be drawn on the screen Nowadays, virtual machines, APIs, etc. Fresh gadgets make you always feel that I want to kiss my girlfriend, but I have no intimate feeling. Maybe, you still need Java, .Net high-rise buildings, you also need Fresh idyllic, such as how you can use the half solution every day - how to guide the computer. So, now let us walk into this pastoral and see how the computer is guided.
Computer startup process
If you buy a new computer, the hard disk is still blank, you can press the POWER key, still see how many characters and patterns, which is obviously not part of the operating system, but the BIOS program is working. Of course, in the end you can see a line, prompt you to insert the boot disk. Yes, the BIOS is looking for a bootable disk. After finding, the boot module on the disk will be loaded, and the control is handed over, and the relay bar is transmitted to the operating system. We consider the easiest to learn, that is, floppy disk. If you put a non-boot disk into the floppy drive, the BIOS will still report an error, prompting you to insert a system disk, this shows that the BIOS is not all trying to load execution, then what is its choice? In fact, it is very simple, it will check the 0-faced zero of the floppy disk 1 (size is 512 bytes), if it is found to end with 55AA, BIOS think it is a boot sector, that is, the boot sector of us. . Of course, a correct boot sector is only 55AA this end flag, it should also contain a less than 512-byte execution code so that it can be placed in a sector and run correctly. Once the BIOS discovers the boot sector, it will load the 512-byte of the entire sector content to the 0: 7C00 of the memory, then jump to 0: 7C00 to handle the control to this boot code. As soon as this, the computer is no longer controlled in the BIOS to control, and it becomes controlled by the operating system.
Master - a smallest guiding sector
Preparation: Hardware a computer (Windows operating system) a blank floppy software assembly compiler NASM. The latest version can be obtained at this link: http://sourceforge.net/projects/nasm. (At this moment, you may have questions, why is NASM, not MASM or TASM, I will explain this later) The floppy disk absolute sector read and write tools. In fact, you can write one by yourself, it is very easy to use CreateFile and Writefile. I am doing this, saving time in online looking for tools. It is best to have a virtual machine such as VirtualPC, you can do not need to restart your own computer during the test.
Code Let's see this code: ORG 07C00H; Tell the compiler program to load to 7C00 JMP $; unlimited cycle Times 510 - ($ - $$) DB 0; fill the remaining space, make the generated binary code just 512 bytes DW 0AA55H; boot sector needs to end with 55AA
Before explanation of the program, in order to see the effect as soon as possible, please follow me: First compile with NASM: NASM boot.asm -o boot.bin
We got a 512-byte size boot.bin, using the floppy disk absolute sector reading and writing tools to write this file to a blank floppy disk, well, this floppy disk is already a boot disk. . Then put it in your floppy drive to restart your computer, or use the VirtualPC to simulate the startup process, boot from the floppy disk, what did you see? The answer is no surprising results, which is easy to understand, because our first statement is a dead cycle. In addition to let the program stop in there, we don't do anything. This is obviously not satisfactory, we have to see some effects, let us change the code:
ORG 07C00H; Tell the compiler program to load to 7C00 MOV AX, 0B800HMOV ES, AX; set ES to directly write memory MOV BYTE [ES: 0], 'A'; in the first byte write characters 'a' MOV BYTE [ES: 1], 0CH; write hexadecimal value C in the second byte of the memory, indicating the black bottom red word JMP $; unlimited cycle Times 510 - ($ - $$) DB 0; fill the rest Space, make the generated binary code exactly 512 bytes DW 0AA55H; the boot sector needs to end with 55AA
We insert four lines before the procedure infinite loop, the purpose is to let our boot program display a red character 'a'. These four lines are more readily understood, and the effect is written in B800: 0000: 'A' and 0CH. We know that B800: 0000 happens to be the first address of the memory. Compile, write to disk and restart, what did you see? You see red characters a! How exciting, this shows that our procedure is running correctly, further, this shows that our own guidance sector test is successful! If you use VirtualPC, it should be such a scene (local):
This is simply too wonderful, because with this beginning, you can make any extension on this, even write your operating system! This step is really not difficult, but it has historical significance!
Why is NASM you might feel very strange, why do someone act like NASM, not the MASM or TASM you used before, actually this a bit involved in personal preferences, but the truth is that I have inadvertently contact NASM. I decided to abandon MASM since then. Because it has the following main features: 1, the code is clear, avoiding the syntax that is easily confused in Masm. This feature is reflected in multiple details of NASM, here I only give two cases. First, in NASM, any label or variable name that is not enclosed in square brackets [] will be considered an address, and the content in the access tag must be used []. So, MOV AX, Message will pass the Message to the first address of the string to the register AX. For example, if there is: Foo DW 1 MOV AX, FOO will pass the address of the FOO to AX, and MOV BX, [FOO] will assign BX's value to 1. In fact, in NASM, variables and labels are the same, that is,
Foo DW 1 ≡ foo: DW 1
And you will find that Offset is not needed in NASM. Because it is expressed when not adding squirrels, it is OFFSET. I personally think this is a big advantage of NASM. If the address does not add square brackets, it doesn't have to add all OFFSET. If you want to access the address, you must add square brackets, code rules are very distinct, and a glimpse. Second, since all the labels are the address, the NASM has another feature, that is, the number of variables is not remembering, so when assigning the variable, it is necessary to add the type of assignment, such as: Mov Byte [var1], 'A'
2, can be used in different platforms If you want to learn, you can use it under different platforms. NASM is almost the only choice. If you want to perform a full code transplant, NASM is a perfect tool. Because regardless of DOS, Windows or Linux, NASM is available, and the usage is identical. 3, free probably this feature is not enough to attract your eye, but it is indeed a cute feature. This article is not a special NASM introduction article, but I don't think it is a recommended tool, especially if you don't want to just understand the way of guiding sectors, but on this basis, study the research, I Ensure that you will get more and more advantages of NASM tools.
Key code explanation
The annotations of the above two codes have been written, and several problems have emphasized here. 1. The role of ORG's use of ORG is to tell the compiler, which is loaded into the memory in the future. We use the constant in the later example, the compiler is based on the address specified by org to determine the address of the constant. 2. About $ and $$$ Representing the address after the current row is compiled. It seems that it is not very good to understand, don't matter, we have disassembled the second-party code file that has just been generated:
Ndisasmw -o 0x7c00 boot.bin >> A.ASM
Open A.ASM, you will find such a line:
00007c09 Ebfe JMP Short 0x7c09
$ Here means 0x7c09 (after loading to memory). So what is $$? It represents the beginning of a section being assembled after the beginning of the section. Here, our program has only one section, so $$ actually means that the program is compiled, that is, 0x7c00. During the process of writing, "$ - $$" may be used frequently, which represents the relative distance from the start of the Bank. Now, what do you mean about 510 - ($ - $$)? Times 510 - ($ - $$) DB 0 indicates that 0 this byte repeat 510 - ($ - $$), that is, the left is not populated in the remaining space until the program has 510 bytes, In this way, coupled with the two bytes of the end sign 55aa, exactly 512 bytes. 3, 55AA or AA55 beginner is often very headache by this issue, always unclear who is in front of who is in front of it, in fact, it is still not understanding essentials. The principle of IBMPC is "high in high byte". For example, if there is a DWORD type 0x12345678 in memory, it looks like this:
L ---> h78 56 34 12
Because 78 is in the low position of the number, it will also be placed in the low position of memory. Here is something to think about it, that is, the computer only knows the numbers. I don't know the type, so I take a number from a number of addresses, you must make sense to indicate the type, such as such a memory image. :
L ---> H78 56 34 12 If you want to take a BYTE, you will get 0x78; if you want to take a word, you will get 0x5678; if you want to take a DWORD, you will get 0x12345678. Looking back at our code:
DW 0AA55H
We specify that the 0AA55H Word type number is placed on the end of the boot sector, and the AA is in the number of numbers, which will be placed in memory, so its image in memory should be:
L ---> h55 aa
Very simple, is it very clear?
Recourse - a variety of lines
Only one character is obviously not enough, we want a further sense of accomplishment, such as displaying a string. However, if each character is to be implemented two lines of code, it is inevitable that it is awkward and inefficient. Yes, you must think of it, we can use the BIOS interrupt. Please see the code:
ORG 07C00H; the program will be loaded to 7C00, so you need this MOV AX, CS MOV DS, AX MOV ES, AX Call Dispstr; call display string routine JMP $; unlimited loop DISPSTR: MOV AX, BootMessage MOV BP, AX; es: bp = string address MOV CX, 16; CX = string length MOV AX, 01301H; AH = 13, Al = 01H MOV BX, 000CH; page number 0 (bh = 0) Black bottom red word (BL = 0ch , Highlights) MOV DL, 0 INT 10h; INT 10h RetbootMessage: DB "Hello, OS World!" Times 510 - ($ - $$) DB 0; fill the remaining space, make the generated binary code exactly 512 bytes DW 0AA55H; boot sector needs to end with 55AA
This code looks much longer, but in fact the body frame is only 5 lines (from Chapter 2 to Chapter 6), which calls a subroutine that displays strings. The second, 3rd, 4 lines of the program are three MOV instructions that cause the DS and ES segment registers to the same segment as the CS to be positioned to the correct position when data operation is performed later. The 5th line adjustment subroutine shows the string, then JMP $ is unlimited. Let's test, compile, write disk, start:
success! Come come, let a cup of coffee below, then enjoy your results quietly on the back of the chair, let your screen temporarily stop at this moment. What is interesting work! Although our code is very short, there are so many technical details, we even use the BIOS interruption, with the help of interrupt routines, we are almost omnipotent, imagine it, the most exciting thing is You can perform disk operations, load more of the program into memory and execute, which means that you can really expand it on this small thing, even build the building of the operating system!
The reason why this is idyllic, because this is the soil
You may have not had such a thing for a long time, as if you see a green plant growing in the dirt. This is a feeling of returning to nature. So, please enjoy this moment of refreshing feelings, forget Java, .NET, and the nasty sauce chicken wings.