Loading and running of the program (v)--"x86 assembly language: From the actual mode to the protection mode" Reading notes 25
Some of the previous posts ended up parsing the code. This article is about compiling, running, and debugging the code.
1. Compiling the code and writing the image file
We both compile and write on the command line before we enter the command. When the source file is not considered troublesome, when the source file is more, it will be considered particularly troublesome. Is there a simple way?
Of course, it's a make
tool.
1.1. What is the Make tool
make
is a command tool that interprets Makefile
the instructions in. The Makefile
document describes the compilation sequence and compilation rules of all the files in the project.
Note: The make command is not just for compiling programs. Whenever you need to generate an output file from multiple input files, we can use it to complete the task.
1.2. About Makefile
Makefile
have their own writing format, keywords, functions.
Like the C language has its own format, keywords and functions.
and Makefile
you can use the system shell to complete whatever command you want to get done.
Above, but very briefly to make
the Makefile
introduction.
about their use. Be able to search related data to learn.
1.3. Makefile1.3.1 of the source document for chapter 13th. My makefile File
BIN = C13_mbr.bin c13_core.bin c13.bin emptya_dir =/home/cjy/a.imgc_dir =/home/cjy/c.imgall:$(BIN). Phony:all cleanc13_mbr.bin:c13_mbr.asm NASM$<-O[email protected]Ddif=[email protected]of=$(A_dir) C13_core.bin:c13_core.asm NASM$<-O[email protected]Ddif=[email protected]of=$(C_dir) bs= +seek=1Conv=notruncc13.bin:c13.asm NASM$<-O[email protected]Ddif=[email protected]of=$(C_dir) bs= +seek= -Conv=notruncempty:diskdata.txt DDif=$<of=$(C_dir) bs= +seek= -Conv=notrunc Touch[email protected]Clean$(RM)$(BIN)
This is my own writing of the makefile. As for why this is written, there is also the makefile of knowledge. I will write blog introduction later.
1.3.2. Instructions for use
- According to their own Bochs configuration file, a and C drive path changes
A_DIR=
and C_DIR=
the following path;
- Save the changed content as a text file, named
Makefile
, in the folder under Chapter 13th, for example, as seen in:
- At the command line
make
, type, enter, wait for compilation and write finished. For example, as seen in:
Can see that the. bin files that we need are generated, and the write to A and C drives is complete.
2. Running Results
Finally we can see the results, we start Bochs, run the results
3. Modify the dynamic change on the basis of the source code
Only getting the results of the book is not enough, not love tossing the program ape is not a good program ape.
3.1. Writing code is like writing a composition
I think writing code is the same as writing a text. Think about the process that most of us learn to write a composition: it doesn't start. What to do? Copy the chant.
(This is the source code to learn from others.) Run out of the results of others.
) and then what. We're not all copies. But on the basis of others to change into their own. (This is what we're going to do now, add our own ideas to the code and see what happens.) ) finally. We do not need to copy, on the examination room can write their own, the results score is quite high. (This is our ultimate goal.) Bo is the chief of the public. Playing independently )
My code for the 13th chapter. made its own patch pack. A friend who needs to be able to download. Is:
http://download.csdn.net/detail/u013490896/9486717
Or
Https://github.com/LeslieChe/from-real-mode-to-protected-mode
Next. I will explain the changes to the patch pack.
3.2. Let the characters show a different color
Looked at the results of the above operation. Do you think the color is a bit monotonous? Good. We change the source code. Pass the attribute of the character as a parameter to the procedure.
First we define some constants. Represents a different color.
;字符属性(都是黑底) GREEN equ0x02 RED equ0x04 BLUE_LIGHT equ0x09 YELLOW equ0x0e
put_string: ;字符串显演示样例程 ;显示0终止的字符串并移动光标 ;输入:(1) push 属性值 ; (2) DS:EBX=串地址
In addition to passing in the first address of the string DS:EBX
, it is also pressed into the property value.
Differences in Beyond Compare software and before changes, such as
In addition, put_char
there are two areas that need to be changed. The second place is a small bug.
After this change. When we call put_string
, we need to press the stack character attribute first.
For example, with:
The effect of the modified operation is as follows:
3.3. The process
put_hex_dword
Changes 3.3.1. Book Source Code Commentary
The previous blog post did not explain the process, so let's talk about it.
The source code is:
201, assembly language programs are extremely difficult to succeed at once, and debugging is very difficult. This routine can provide help202Put_hex_dword:The current cursor is displayed in 16 binary form.203 ; a double word and push cursor204 ; input: edx= the number to be converted and displayed205 ; output: None30WPushad207 PushDs208 209 movAx,core_data_seg_sel; Switch to core data segment About movDs,ax211 212 movEbx,bin_hex; point to the conversion table in the core data segment213 movEcx8214 . XLT:215 RolEdX4216 movEax,edx217 andeax0x0000000f218Xlat219 - PushEcx221 movCl,al222 PagerPut_char223 PopEcx224 225Loop. XLT226 227 PopDs228Popad229Retf
374 bin_hex ‘0123456789ABCDEF‘
The principle of this code is very easy. EDX
registers are 32-bit, right-to-left, and 4-bit groups, divided into 8 groups.
The value of each group is between 0x0~0xf, and we convert its value to the corresponding character 0
~ F
;
The No. 218 line uses a look-up instruction xlat
, which requires that DS:EBX
a table be stored in advance (32-bit mode) or DS:BX
(16-bit mode). When the instruction is run, AL
a value is used as an offset to retrieve a byte from the corresponding position in the table. Transfer to, for AL
example, if the DS:EBX
table defined in line No. 374 is stored, then when AL
the =0 is in place. xlat
after running. The AL
value in is the ASCII code for character 0.
The No. 215 line uses the circular left shift instruction rol
, the first cycle will be the high 4 bits of edx moved to the far right, and the 0x0000_000f phase, so that the AL
high four bits of the corresponding value, and then look up the table, the corresponding characters are obtained.
No. 221 to No. 222, print this character to the screen (the print position is where the current cursor is located and the cursor is pushed).
3.3.2. My Changes
Before the change, if in the user program. We want to output EAX
the value of the register, then we need to
mov edx,eax call far [fs:put_hex_dword]
Now I want to be able to use this:
push‘eax‘ push eax call far [fs:put_hex_dword]
That is, pass the parameter through the stack, the first parameter is a string ‘eax‘
, the second parameter is EAX
the value of the register.
The effect of the operation is as follows (light blue first line):
Maybe some friends will be surprised. push ‘eax‘
is it possible to do this?
For the NASM compiler, this notation is agreed. ‘eax‘
belongs to the character constant.
A character constant consists of up to four characters that are included in a double-or single-argument. A character constant with multiple characters is serialized into a small order.
mov eax,‘abcd‘
Equivalent
mov eax,0x64636261
So. We can put ‘eax‘
such a character constant into the stack (because in 32-bit mode, so the default is pressed by 4 bytes, the highest bit will fill 0). Passed as a parameter to the procedure. Extracts every character of this parameter in the process. displayed on the screen.
Shows the first change to this process:
From .p_char
the label to .ok
the code between. is to remove the characters we want to display from the stack (0 values are encountered). Output to the screen.
.ok
The following 2 lines are to print the equals sign =
;
The second change to this process is for example:
3.3.3. Local label
In the source code, it is found that the author uses the first label in very many places .
, such that the label belongs to the local label.
The following is an official manual from NASM
http://www.nasm.us/doc/nasmdoc3.html#section-3.9
NASM gives special treatment to symbols beginning with a period. A label beginning with a period are treated as a local label, which means that it's associated with the previous No N-local label. So, for example:
label1 ; some code .loop ; some more code jne .loop ret label2 ; some code .loop ; some more code jne .loop ret
In the above code fragment, each JNE instruction jumps to the line immediately before it, because the both definitions of. Loop is kept separate by virtue of all being associated with the previous Non-local label.
I think this can be convenient for users. Don't bother to name the label.
3.4. Relocation of symbol table
My blog.
Loading and running of the program (iii)--"x86 assembly language: From the actual mode to the protection mode" Reading notes 23
It has been pointed out that there is a small bug when repositioning the symbol table.
I'm going to add debug information to print and prove that this is really a bug, and at the same time it proves that my changes are right.
Line No. 575 to No. 583. I have added some code to print the user symbols and kernel symbols that will be compared.
When you are finished running 573 lines. DS:ESI
points to an entry in the kernel symbol table, ES:EDI
pointing to an entry for the user symbol table. The red code is to print the two entries to the screen, the user symbol on the left, and the kernel symbol on the right.
put_usr_salt
the code for the procedure is as follows:
输入:push 属性 es:ebx 中是符号的起始地址输出:无
-Put_usr_salt:; Print user's symbols $ PushEcx the movEbp,esp the movch,[ebp+3*4] the . Getc:; local label the movCL,[ES:EBX] - orCl,cl inJz. out the PagerPut_char the IncEbx About jmp . Getc the . out: the movCl0x20 the PagerPut_char + PagerPut_char - PagerPut_char the PagerPut_char; print four spacesBayi the PopEcx theRetf4
67: Get attribute values from the stack
68~74: Used to print a string ending with 0.
76~80: Used to print 4 spaces.
put_core_salt
the Code of the procedure is similar. Don't repeat it here.
Take a look at the effect of the operation:
The yellow on the left is the user symbol. Red on the right is the kernel symbol. We can clearly see the comparison process of the symbols:
@TerminateProgram
Compare 2 times after the match up;
@ReadDiskData
Compare 2 times after the match up;
@PrintDwordAsHexString
Compare 3 times to match.
This blog post is here.
In the next blog post, we will talk about the conditional compilation of NASM and some changes of makefile. There are also 13 exercises in the chapter. Please look forward to ...
Loading and running of the program (v)--"x86 assembly language: From the actual mode to the protection mode" Reading notes 25