Protected mode addressing

In assembly language, or if you have studied courses such as computer theories or principles of computer composition, etc., then you probably have heard of the concept of real-mode and protected mode. What are they in the end is, what is the difference, and how to address?

Finally, the three operating modes about Intel microprocessors in a king cool "assembly language."

Following the launch of Intel 8086, Intel has launched a landmark 80386 microprocessor, which can work in real mode, protected mode and virtual 8086 mode, and since then the microprocessor provides three operating modes, until now . Three modes of the Intel family of microprocessors are as follows:

  • Real mode: working mode is equivalent to a 8086
  • Protected Mode: Provides support for multi-tasking work environment, protection mechanisms
  • Virtual 8086 Mode: may be switched from protected mode to the mode of operation 8086 in which one kind, there is provided in this manner is convenient for users to run one or more programs in protected mode 8086

When our system boot, cpu first completed some work in real mode, and then jumped into protected mode, provides support for multi-tasking environment for our system. And when (for example, when learning assembly used in DOS systems) we need to run the program in real mode on a system protected mode, we need to get a "fake" real mode in the current protected mode, which is the virtual 8086 mode.

GDT and descriptors

In the real mode (as will be appreciated when working on 8086), we are 16-bit CPU, providing 16-bit registers, 16-bit data bus, an address bus 20, the addressable range bits 1M. Physical address follows the following formula:

\ [Physical segment address address = offset address + 16 * \]

Wherein the segment address and offset address is 16 bits.

80386 from the beginning, Intel CPU into the family of 32-bit era, this time the CPU has 32-bit address bus, addressable range of 4G. CPU also has a 32-bit register, one register to 4GB of addressable space.

In real mode, we use the segment address: offset address addressing because we only have 16 registers, a single register addressing range not reach 1MB, but now we have a 32-bit register, a single register addressable range has up to 4GB, then you do not need is not a segment register? the answer is negative. In protected mode, the address is still a "segment address: offset" way to represent, but the concept segment has undergone a fundamental change .

In real mode, the segment value (the value of the segment address) or part of the address. In protected mode, the segment value while still original 16 is cs, ds denotes a register, etc., but this time they are only an index, the index points to a data structure of the entry , the entry section defined in detail a starting address, boundary, property and so on, this data structure, called a GDT (in fact probably LDT, we first discuss the most part), each entry in the GDT, called descriptor

Addressing process

We look at the process of addressing the protected mode in view. Prior to this, there are several points to note:

  • GDT is a data structure that is stored in memory, so it should have a starting address, which is set a series of descriptors
  • GDT start address by a dedicated register to store - gdtr, gdtr register is 48, and we discuss later in this register
  • GDT Each descriptor describes a segment, the segment including the start address (base address) and the like properties
  • It is the same as the real mode offset and the protected mode, only 32-bit

Well, here there is a map, we can look at this chart over again how protected mode is addressed.

  1. When addressing, first find gdtr register, derive GDT base address
  2. With GDT base address, there are stored in the segment register index can be obtained that entry segment register "refer", and referred to both the descriptor
  3. Descriptor obtained, can be obtained starting address of the segment descriptor is the descriptor from
  4. With segment start address, the offset address adding thereto take over, will be able to give the final linear address
  5. With the linear address (virtual address), the converted, to obtain the corresponding physical address

I believe that here, you already have a rough idea of ​​addressing procedure, and then we look at what we have not mentioned in detail in the above

gdtr register

gdtr is a 48-bit register holding the base address and limit the GDT (GDT or the length), the upper 32 bits of the base address of the GDT, the limit of the lower 16 bits. Remember protected mode segment registers are 16-bit do, they gdtr boundaries are the corresponding ah.

Descriptor

Each descriptor in the GDT 8 bytes with the following structure

We can not control these properties, only to see the segment base address and segment boundaries. Is not above addressing contacted yet.

You may ask, ask what segment base address and segment boundaries are separated, but not together? This is mainly historical issues, we will not explored.

Code

Just look at the theory after all the water, we see a simple piece of code actually taste.

[SECTION .gdt]
; GDT
;                              段基址,       段界限     , 属性
LABEL_GDT:     Descriptor       0,                0, 0           ; 空描述符
LABEL_DESC_CODE32: Descriptor       0, SegCode32Len - 1, DA_C + DA_32; 非一致代码段
LABEL_DESC_VIDEO:  Descriptor 0B8000h,           0ffffh, DA_DRW      ; 显存首地址
; GDT 结束

GdtLen      equ $ - LABEL_GDT   ; GDT长度
GdtPtr      dw  GdtLen - 1  ; GDT界限
        dd  0       ; GDT基地址

; GDT 选择子
SelectorCode32      equ LABEL_DESC_CODE32   - LABEL_GDT
SelectorVideo       equ LABEL_DESC_VIDEO    - LABEL_GDT

The above code, we define a corner .gdt segment, wherein the first three LABLE_xxx we use is called a macro Descriptor defines three selectors, wherein the values are not necessarily correct, because we just defined, further It did not initialize . Descriptor role is to segment base address, a segment limit and the sub-attribute in the corresponding selection position, which is defined in the end of the article, interested can look.

GdtPtrGdtr and is not put in the content the same? Yes, before we enter protected mode in real, we need to GdtPtr value loaded into register gdtr: using the commandlgdt [GdtPtr]

What was the last two GDT selectors is it? Seems to be offset with respect GDT descriptor base address, is not all right, which is slightly more complicated, as shown in FIG.

Where TI is the RPL and selection sub-attributes, the remaining high 13 indicates the descriptor in the descriptor table position, i.e., the GDT of several descriptors

Finally, we look at how to use above them

[SECTION .s32]; 32 位代码段. 
[BITS   32]

LABEL_SEG_CODE32:
    mov ax, SelectorVideo
    mov gs, ax          ; 视频段选择子(目的)

    mov edi, (80 * 11 + 79) * 2 ; 屏幕第 11 行, 第 79 列。
    mov ah, 0Ch         ; 0000: 黑底    1100: 红字
    mov al, 'P'
    mov [gs:edi], ax

    ; 到此停止
    jmp $

SegCode32Len    equ $ - LABEL_SEG_CODE32

上述代码将一个字母P显示在屏幕上。gs中保存的是显存的选择子,edi为偏移地址,然后使用mov [gs:edi], ax将ax的内容写入到地址为gs所指的描述符中的段基址+edi的内存处,由于这里写入的是显存,所以将会将一个字母P显示在屏幕上。

Descriptor宏的定义如下

; usage: Descriptor Base, Limit, Attr
;        Base:  dd
;        Limit: dd (low 20 bits available)
;        Attr:  dw (lower 4 bits of higher byte are always 0)
%macro Descriptor 3
    dw  %2 & 0FFFFh             ; 段界限1
    dw  %1 & 0FFFFh             ; 段基址1
    db  (%1 >> 16) & 0FFh           ; 段基址2
    dw  ((%2 >> 8) & 0F00h) | (%3 & 0F0FFh) ; 属性1 + 段界限2 + 属性2
    db  (%1 >> 24) & 0FFh           ; 段基址3
%endmacro ; 共 8 字节

参考:

  • 《汇编语言》 王爽
  • 《一个操作系统的实现》 于渊

Guess you like

Origin www.cnblogs.com/tcctw/p/11300720.html