Segment registers and logical addresses

Register operand: (stored in the CPU)

MOV AX,0FFFFH

AX is the register operand.

The operand itself is stored in a register, and only a few bits of code are given in the instruction to indicate that it is specifically stored in that register.

The data in the memory is read into the CPU through the register and enters the calculation of the data.

Memory operand: (stored in memory)

It is stored in a memory area, so it is called a memory operand.

  1. mydata dw 1234H or mydata db 12H etc.

    Variables like mydata in the above

  2. xx db 0FFH,0

    mov bx, OFFSET xx // Use the offset operator to calculate the offset value of the xx unit

    mov ax,[bx]

    The above [bx] is called the non-variable name directly addressed memory operand

Segment register:

The segment register is set for the segment management of the memory.

Why do you need memory segmentation?

The 8086CPU has 20 address lines, and the maximum addressable memory space is 1MB. The 8086 register is only 16 bits, and the instruction pointer (IP) and index registers (SI, DI) are also 16 bits. It is impossible to address a 1MB space with a 16-bit address. Therefore, the memory must be segmented, that is, the 1MB space is divided into 24, that is, 16 segments, each segment does not exceed 64KB (2 16,16 data lines can be addressed).

Therefore, four 16-bit segment registers are set in 8086 to manage four segments: CS is the code segment, DS is the data segment, SS is the stack segment, and ES is the additional segment. After the memory is segmented, each segment has a segment base address. The segment register holds the upper 16 bits of the segment base address. The upper 16 bits are called the segment base value . The 16-bit address is shifted left by four bits (behind Add 4 zeros) to form a 20-bit segment base address .

However, when describing memory segmentation, the following segment information is required: 1. The size of the segment; 2. The starting address of the segment; 3. The management attributes of the segment (write prohibition / execution prohibition / system specific, etc.). It takes 8 bytes (64 bits) to store this information, but the segment register only has 16 bits.

Therefore, only the segment number , also called the segment selector , can be stored in the segment register , and then the segment number is mapped to the GDT (Global Segment Number Record Table ) stored in the memory to read the segment information.

Virtual space (virtual memory address space) / programming space (program writing space)

The storage management component regards the main memory (physical memory) and the auxiliary memory (disk) as a whole, that is, virtual memory. 486 allows a maximum virtual memory capacity of 246 = 64T, that is, the programmer can program within this address range, and the program can greatly exceed the physical space.

Therefore, the CPU must convert the address in a virtual memory space to a physical address (the running space of the program)

Two steps are required:

1. First, given a logical address, the CPU first uses its segmented memory management unit to convert a logical address into a linear address

2. Reuse its paged memory management unit to convert to the final physical address.

Note:

  1. If there is no paging, the linear address is the physical address;

  2. If paging, then the paging unit will convert the linear address to

    Physical address.

  3. Real mode: the storage space is only segmented, not paging;

    Protection mode: The storage space is segmented first, and then paging.

What is a logical address?

The logical address is the address generated by the CPU and refers to the address used to specify an operand or an instruction in a machine language instruction.

A logical address consists of two parts, the segment identifier: the offset within the segment (offset) . The segment identifier is composed of a 16-bit field, called the segment selector . The segment selector is stored in the segment register. The first 13 digits are an index number. The last three bits contain some hardware details.

The index number, or directly understood as the array index, what is the index of it? This stuff is " segment descriptor (segment descriptor) ", (there are data segment descriptors, code segment descriptors, system segment descriptors). The segment descriptor specific address describes a segment.

In this way, for many segment descriptors, an array is called "segment descriptor table", so that a specific segment descriptor can be directly found in the segment descriptor table through the first 13 bits of the segment identifier The descriptor describes a segment, and each segment descriptor consists of 8 bytes. There is a Base field in the segment descriptor, which describes the linear address of the starting position of a segment. Base + offset is the linear address to be converted.

Note:

Some global segment descriptors are placed in the "Global Segment Descriptor Table (GDT)".

Some locals, such as each process's own, are placed in the "Local Segment Descriptor Table (LDT)".

The T1 field in the segment selector, = 0, means GDT, and = 1 means LDT.

As instructed:

1.直接寻址
格式:段寄存器:[偏移地址]
mov bx, ds:[1234H] //表示从ds数据段偏移地址为1234H的单元取数——>bx
假设(DS)=5000H 可计算出
线性地址为16d×(DS)+1234H=16d×5000H+1234h=50000H+1234H=51234H 。
命令的最后结果就是把线性地址为51234H的存储单元中的操作数据放入BX中

2.间接寻址
格式:段寄存器:[间址寄存器] / [间址寄存器]
注意:可省略段寄存器是因为会到默认约定的段寄存器中取内容数据
例:
mov ds,数据段段基址
mov bx,buf单元的偏移地址
mov al,ds:[bx]
等价于:mov al,[bx]

The logical segment of indirect registers and conventional access is shown in the following figure:

Paging memory management

Core: Divide virtual memory space and physical memory space into pages of the same size, and use pages as the smallest unit of memory space division. Space growth is also easy to achieve: just allocate additional virtual pages and find an idle physical page to store

Why do you need paging memory management?

Because of the following advantages:

  1. There will be no external fragmentation (the root cause of space fragmentation is that the size of each program is different, so there is no consistency when allocating space. The solution is naturally to allocate space to a specified size. Virtual memory It is divided into parts of the same size as physical memory, which we call "pages.") The memory space occupied by a process may not be continuous,
  2. The virtual pages of a process can be stored on the disk when they are not needed, and do not need to be loaded into memory at the same time.
  3. You can share small addresses, that is, page sharing. Just make a corresponding record in the corresponding page table.

Under the mechanism of the paging system:

The virtual address issued by a program consists of two parts: the page number and the offset value within the page

This total_page array has 2 ^ 20 members, each member is an address (32-bit machine, an address is 4 bytes), then to represent such an array, it will take up 4MB of memory space. In order to save space, a secondary management mode machine was introduced to organize the paging unit.

So each 32-bit linear address is divided into three parts, page directory index (10 bits): page table index (10 bits): offset (12 bits)

Follow the steps below to convert:

  1. Remove the page directory address of the process from the cr3 register (the operating system is responsible for loading this address into the corresponding register when scheduling the process);
  2. According to the first ten digits of the linear address, find the corresponding index item in the array. Because the secondary management mode is introduced, the item in the page directory is no longer the address of the page, but the address of a page table. (An additional array was introduced.) The page address was put in the page table.
  3. According to the middle ten bits of the linear address, find the starting address of the page in the page table (also an array);
  4. Add the starting address of the page to the last 12 bits in the linear address to get the physical address we want in the end;

Guess you like

Origin www.cnblogs.com/Hhhighway/p/12684920.html