First, the following content is to explore out, would not say necessarily correct, yet there is something wrong, please correct me.
I am in the process of learning has been compiled do not understand how the operating system is segmented and how to assign the address of a segment register (if segment data segment so mov ax, how data is achieved) through to find information and explore basic understanding a principle which, recorded as follows:
First, see how the segment, such as the following codes
data segment
db ‘data’
data ends
My initial understanding is: After obtaining the segment information, will automatically look in memory when the program runs a long unused space 64K to use this segment. I thought it must be a fixed period of 64K, and will not necessarily be assigned to which local memory. I later discovered that understanding is wrong in practice.
The fact is: a space to the maximum segment 64K, and the minimum space allocated is 16 bytes, in multiples of 16 and the total allocated space. (If there is no segment data is not assigned to any defined space)
The space allocated in the segment that you code as shown above:
So if the program has multiple segments it:
data segment
db ‘data’
data ends
data2 segment
db ‘data2’
data2 ends
data3 segment
db ‘data3’
data3 ends
Code in the memory space assigned as shown below:
We see that if there are multiple segments, it is in the order we define the continuous storage. I imagine the system rather than the space available free to find their own store.
Let's look at such as mov ax, how data is achieved.
First, look at the following assembler source
assume cs:code,ds:data,ss:stack
data segment
db 'data'
data ends
stack segment
db 'stack'
stack ends
code segment
db 'code'
first:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov bx,data2
code ends
data2 segment
db 'data2'
data2 ends
data3 segment
db 'data3'
data3 ends
end first
In this code, there are five sections, they are segment name data, stack, code, data2, data3
Written procedures, we do not know this program will eventually be placed where in memory, so when we want to use a segment, you can only use mov ax, data (segment name) to specify that such a code Representative data segment address assigned to the ax register, the operating system needs to be replaced by the actual data in the segment address generating exe off and run. Such as
mov ax, the actual operation of data into the memory as will mov ax, 0aba. This process is relocation.
Relocation of the operating system-related information is stored in the generated exe file:
The first is the header:
Then part of the program:
In the file header portion relocated on the following bytes:
06H-07H relocation entry number.
Obtained from the value 0003 in FIG. 06H-07H, it indicates that the program has three relocation entries
So these three items were relocated position where it? We can be obtained according to the first offset relocation entries
Offset 18H-19H of the first relocation entry
18H-19H obtained from the values in FIG. 001E, i.e. a first relocation entry at the beginning of the header file 1E, each represented by a four byte position in which the upper 2 bytes of relocation entries section where the first row offset with respect to the program, the lower 2 bytes of relocation entry the offset of the first byte of the segment resides.
In addition to re-file 1E start, we can obtain the following three addresses:
00020005 (2 rows offset, byte offset 5)
00 02 00 0A (2 rows offset, byte offset A)
00 02 00 0F (2 rows offset, byte offset F)
With this information we can find the corresponding byte relocation is required, as shown below:
They are
B8 0000 the corresponding assembly code mov ax, data
B8 0001 the corresponding assembly code mov ax, stack
BB 0004 the corresponding assembly code mov bx, data2
We see, data is replaced with 0000, stack is replaced with 0001, data2 is replaced by 0004
So what is this 0000,0001,0004 represent it, they said corresponding segment offset relative to the first line of the program.
0000 is the first line segment data exactly
0001 is a second row, exactly stack section
0004 was the fifth row, just data2 segment
Assuming that segment addresses assigned after the operating system to run the program then the operator 0aba
Data segment address is 0aba + 0000 = 0aba
Segment address stack is 0aba + 0001 = 0abb
Segment address data2 is 0aba + 0004 = 0abe
The operating system will be based on information redirection
mov ax, data replaced mov ax, 0aba
mov ax, stack replaced mov ax, 0abb
mov bx, data2 replaced mov bx, 0abe
Thus completing the process of redirection.
Again I have compiled a novice, these findings if there is wrong with the hope that we corrected, thank you!
Reproduced in: https: //www.cnblogs.com/danqing/archive/2011/12/01/2270429.html