Transferred from the oldlinux Forum
This is what I learned from the bootsect. s file. After reading this version of the bootsect file, I collected it.
Some problems are really obvious in the book, so I didn't extract them.
Some problems are really the same as bootsect.CodeIt does not matter, and I have not excerpted it.
They are all questions you want to ask. I have read it several times and there are still many problems.
In fact, many questions have been asked before. This is indeed the case.
It is very convenient to query. It is based on the row number!
----------------------------------------
Shift instruction in bootsect. s
-------------------------------------------------------
The length of the operand is expressed by the symbol after the instruction in B (byte, 8-bit), w (word, 16-bits), L (long, 32-bits ), for example, "movb % Al, % Bl", "movw % ax, % BX", "movl % eax, % EBX ".
If the length of the operand is not specified, the compiler will set it according to the length of the target operand.
For example, the command "mov % ax, % BX", because the length of the destination operand BX is word, the compiler will equate this command with "movw % ax, % BX ", command "mov $4, % EBX" is equivalent to command "movl $4, % EBX", and "Push % Al" is equivalent to "pushb % Al ".
The compiler reports an error for an instruction that does not specify the length of the operand but cannot be guessed by the compiler, for example, the instruction "Push $4 ".
==============================================
L25. globl begtext, begdata, begbbs, endtext, enddata, endbbs
L26. Text
L27 begintext:
L28. Data
L29 begdata:
L30. BSS
L31 begbbs:
L32. Text
...
.....
.......
L255. Text
L256 endtext:
L257. Data
L258 enddata:
L259. BSS
L260 endbbs:
Q:
We can see that. text. data. BSS overlaps. My question is that when the module is connected ,. text and other modules. text Merge ,. data and other modules. if data is merged, what will happen?
There is also a syntax problem. Why does. text appear three times?
-------------------------------------------------------------------------------
A:
Overlapping segments are generally used only for a single file.
Before
The last two. texts start the text section and define the "label" of the Section. This can be distinguished when debugging with symbols. Both bootsect and setup are separately compiled links and are not associated with other target files.
Merge. When creating the kernel image file, the tools/build. c file will directly remove their respective header structures and combine them in sequence .. Text defines the code behind it in.
Text. If other. xxxx fields are displayed later, the code or data in section XXXXX starts after. xxxx.
######################################## ###################################
Recently, I have studied as assembly and learned about segmentation. However, I have questions when reading the bootsect. s file.
The structure of the entire file is as follows:
. Globl begtext, begdata, begbbs, endtext, enddata, endbbs
. Text
Begintext:
. Data
Begdata:
. BSS
Begbbs:
. Text
...
.....
.......
. Text
Endtext:
. Data
Enddata:
. BSS
Endbbs:
Q: I don't understand this segmentation method. Does Linus compile code segments and data segments in one segment?
Even so, why do we still have endtext and other identifiers?
-----------------------------------------------------------
A: bootsectProgramAll are placed in 512 bytes. In such a small place, there is no need to separate each section.
Begtext and endtext can be used to specify the start and end of the Text Segment.
######################################## ##################################
25. globl begtext, begdata, begbss, endtext, enddata, endbss
26. Text
27 begtext:
28. Data
29 begdata:
30. BSS
31 begbss:
32. Text
Q:
I read the MASM syntax, and then read the help of as86, but I don't know what the above statements actually do. In MASM, there are statements with definition segments, corresponding segment register,
With the help of as86,. Text and. Data are set to the current segment (set current
Segment) I think it is equivalent to some statements in MASM, But what segment registers does. Text and. BSS correspond? In addition, as86 does not seem to seeSource codeSplit
Segment. Does as86 automatically divide the corresponding segment according to the source code during compilation?
A: These pseudo commands are used by the compiler. These labels are used by ld86.
. Text indicates the code segment in the program;. data indicates the data segment;. BSS indicates the uninitialized data zone.
Note that the above "segment" does not mean a segment register. It only specifies the code or data area (Block) in the target file or execution file ). The execution file on the hard disk does not include BSS. Only when the execution file is loaded into the memory will it be allocated a BSS segment (zone) and located behind the data segment.
Linking programs (such as LD) will use these pseudo commands to combine these segments in all link modules separately to form the Combined Code and data parts in the output file. For more information, see:
Http://www.oldlinux.org/cgi-bin/LB5000XP/topic.cgi? Forum = 1 & topic = 1139 & show = 25
######################################## ################################
Q: Can I not use the globl variables defined in the startup file?
I can't see the usage of these globl variables during reading.
----------------------------------------------------------------------
A: Generally, you cannot use global variables that you specify. However, when all your segments overlap and do not use any of the global variables used by the linker, You can omit them. For example, you can remove the. Global definitions in bootsect. s and setup. S.
######################################## ###############################
Q: Is the following code equivalent to the above Code?
. Globl begtext, begdata, begbbs, endtext, enddata, endbbs
. Text
Begintext:
Begdata:
Begbbs:
...
.....
.......
Endtext:
Enddata:
Endbbs:
A: you also need to provide. Data and. BSS after. Text.
========================================================== ==============================================
3.
L43 root_dev = 0x306! The specified file system device is the first partition of 2nd hard disks.
Q:
If the kernel program and the file system are installed in the same hard disk, do you need to set root_dev = 0x301/dev/hda1? Change to the first partition of the first hard disk.
In the program, when loading setup and getting the number of sectors of each track, you also need to change the drive letter to the read hard disk identifier?
A:
1. If you want to install it in the same partition, you need to use a dedicated and independent boot software that can obtain the kernel image file from the file system and load it. For example, shoelace, grub, and Lilo. Someone on this site has already made a special one. Please refer to the 2nd sub-forums (excellent ).
2. You are right. Many (basically all) are useless and can be deleted. However, from this point, we can see that Linus was constantly learning other people's stuff.
L43 root_dev = 0x306! 0x306-/dev/hd6-2nd partitions of Hard Disks
Q:
Why does root_dev point to 2nd partitions of 1st hard disks instead of the first or other partitions? Is there any special reason?
A:
At that time, Linus had two hard disks, and he used 2nd hard disks to develop Linux systems. The minix system was installed on 1st hard disks.
========================================================== ==============================================
L51mov CX #256
Sub si
Sub di
Rep
Movw
Q:
Move the bootsect. S module to 0x90200. How do I know that bootsect. S is compiled with 256 words?
A:
At the end of bootsect. s, there are:
249l. org 508
Indicates that the Code after line 1 starts from the second byte and goes down:
. Word root_dev
. Word 0xaa55
4 bytes in total, so the entire file size is 508 + 4 = 512 bytes.
Linus uses. org here and can also use the fill 0 method. In any case, the last two bytes of 512 bytes must be 55 AA (the word table is aa55)
========================================================== ==============================================
L56 jmpi go, initseg
L57 go: mov ax, CS
Jmpi syntax is jmpi segment value, intra-segment offset
Q:
In the Assembly, it seems that the label refers to the intra-segment offset. Therefore, the label go here should be an intra-segment offset. Is it a problem ??
A:
"Jmpi syntax is jmpi segment value, intra-segment offset" -- yes, this is as86 syntax.
========================================================== ==============================================
L77 J load_setup
Q:
Should it be: JMP load_setup?
A:
It is not a printing error. ld86 has the J command, which is equivalent to JMP.
========================================================== ==============================================
L81 <Note> Read the disk drive parameters using the 8th sub-function of int 0x13. The entry parameter Ah = 0x08, DL = drive letter (if it is a hard disk, set 7 to 1)
Q:
Who assigned the drive letter and how it was allocated. Why should the hard disk be set to 7 to 1?
A:
This is the design of the ibm pc bios.
========================================================== ==============================================
L87 seg CS! Indicates that the operand of the next statement is in the segment specified by the CS segment register.
Q:
In bootsect. s
The sectors, root_dev, sread, Head, track, and other flags are defined in, but the operands contain sectors,
When root_dev is used, seg cs is required. When the operands contain sread, Head, and track, seg cs and why are not used before?
A:
You will find that all the "seg CS" commands are useless. Because this program is connected to a segment, that is, the code and data segments are all together. At that time, the reader program used by Linus during development may be compiled separately, or downloaded from the internet and modified.
L87 seg CS
L88 mov sectors, CX
Q:
The meaning of the statement is well understood, but the DS and CS segments are the same at this time. Is it possible to add 87 lines of statements here?
A:
Seg cs is optional.
========================================================== ==============================================
L94 mov ah, 0x03
L95xor BH, BH
L96int ox10
A:
Are the statements from lines 94th to 96 required before the strings are displayed? I used to add debugging statements to the core code without first reading the cursor position. Is it the first time you read the display?
Q:
94-96 is required. It is used to set the row and column values used by the int 0x10 call function 0x13 (dx, starting position of the string ).
If the minimum bit of Al when the function is called to display strings is 1 (Al = 1 or 3), the cursor is set at the end of the string after the string is displayed. Otherwise, if Al = 0 or 2, the cursor remains unchanged after the display.
========================================================== ==============================================
L99 mov BX, #0x0007! Page 0, attribute 7 (normal)
Q: What is the role of BL = 0x07?
A: This is the display parameter. For more information, see VGA display card.
========================================================== ==============================================
L153 test ax, # 0x0fff
Q:
(1) # 0x0fff why not #0x1000?
(2) Is this step redundant, because ax must be 0x1000. Why should we test it? What error will cause ax and ES not to be equal to 0x1000?
A:
(1) test command: The two operands do not save the result, but only save the feature condition code (affects the flag Flag ).
In this test, the 12 bits represented by all three 0xf should be zero. If all values are 0, the segment value must be 0xn000, and the corresponding segment address must be 0xn0000, that is, it is located on the 64 kB boundary.
(2) because this is a subroutine, it has its own interface requirements. This judgment is a good programming habit.
========================================================== ==============================================
L170xor ax, ax
L171sub ax, BX
L172shr ax, #9
Q:
170
If you have any questions about the line of code, the program jumps to read_it
In the subroutine, the data in the disk must be read into the area where the address starts from 0x10000 in the memory, the entire read process is to first check whether the total number of bytes in the remaining sectors of the current track can be completely
Put it in the current 64 K memory segment. If the current 64 K memory segment cannot be put down, it will be executed to the location of 170 rows, and the sentence of 170 rows will come.
XOR ax, ax
Sub ax, BX
SHR ax, #9
These lines of code are used to obtain the number of sectors that can read the current 64 K memory segment and store them in Al.
I am a bit puzzled about this place. If it is the first read, The BX should be 0, and the ax is 0, and the result is 0 in Al, next we will call INT 13 to stop reading the disk. The meaning of Al in the 13 interrupt is the number of sectors read from the disk. When Al = 0, I don't know how the interrupt was executed ??
I checked the manual, and the Manual did not talk about the call of the subfunction 2 interrupted on INT 13. What is the result of the call when Al = 0, the value of Al is valid between 1 and. I don't know how to deal with INT 13 interruptions?
A:
1st
For the next read, The BX is indeed 0, but the ax is not 0. Because sread is initially 1 + setuplen, that is, 5. Therefore (80-5) x 512 = 38400 =
37.5kb does not exceed 64 KB, so the code starting from line 1 is not executed for 1st times. The maximum 16-bit register size is 64 KB.
-1, so here 0 indicates the maximum 64 KB (carried ).
A floppy disk has 80 sectors on one track, and a hard disk has 63 sectors at most,
In this case, the total number of bytes on a track must be less than 64 KB, that is to say, it will not appear. The first time you read the disk, It will be executed at the 170 rows:
XOR ax, ax
Sub ax, BX
SHR ax, #9
========================================================== ==============================================
L180mov ax, #1
L181sub ax, head
L182jne ok4_read
L183inc track
Q:
When no data is readable on the current track of the 0-Core Head, read the data on the 1-core head. Which kind of soft drive is this suitable? I thought it was a dual-sided floppy drive. If it is not a dual-sided soft drive, it will take an endless loop in read_track.
A:
The floppy drive used by the PC is dual-headed (dual-sided. I have never seen any PC machine with a single side
========================================================== ==============================================
Read_track:
199 PUSH AX
200 push BX
201 push CX
202 push DX
203 mov dx, track
204 mov CX, sread
205 Inc CX
206 mov CH, DL
207 mov dx, head
208 mov DH, DL
209 mov DL, #0
210 and DX, #0x0100
211 mov ah, #2
212 int 0x13
213 JC bad_rt <-- I used bochs to compile JB... Is this code wrong?
214 pop DX
215 pop CX
216 pop BX
217 pop ax
218 RET
Q:
(1) Is it because each segment of linux0.11 has a 64 kB length limit?
(2) there is also the read_it part in bootsect. C, and JC bad_rt <-- I used bochs to compile JB... Is this code wrong?
A:
It is not limited to 64 KB, but a maximum of 64 kB for each read.
In addition, check the manual to see if JB is JC.