Control register:
The control register has CR0 CR1 CR2 CR3, where CR1 is reserved, where I have focused on CR0, which should have important contacts with segments and paging, CR2 and CR3 do not introduce.
The first bit of the CR0 of the control register is represented by PE. He is used to control segments. When PE = 0, the processor runs in real mode, when PE = 1, the processor runs in the protection mode. Under our door In the code, I will set the CR0 PE bit, enter the protection mode. CR0, 31 bits of CR0, is used to control paging. Use PG. When PG = 0 is disabled, the paging mechanism is enabled when PG = 1.
Let's take a few examples below.
Switching of real mode and protection mode
Output a B in real mode, output a A in the protection mode
EA20 Macro
Push AX
IN Al, 92H
OR Al, 00000010B
OUT 92H, Al
POP AX
ENDM
; ------------------------------------------------- ---------------------------
Close the A20 address line
; ------------------------------------------------- ---------------------------
DA20 Macro
Push AX
IN Al, 92H
And Al, 11111101B
OUT 92H, Al
POP AX
ENDM; A20 See http://www.xemean.net/data/Article.asp?id=8
Jump Macro Selector, Offsetv
DB 0eah
DW offsetv
DW selector
ENDM
; ---------------------
Descriptor struc; structure of a descriptor
Limitl DW 0
Basel DW 0
Basem DB 0
Attributes dw 0
Baseh DB 0
Descriptor ENDS
; ---------------------
PDESC STRUC
Limit DW 0
Base DD 0
PDESC ENDS
;
ATDW = 92h; there is a readable write data segment type
ATCE = 98h; there is only a code segment type
Atcer = 9ah; there is a type of code segment capable of executable readable writing
.386P
DSEG segment use6
; ----------------------------------------
GDT Label Byte; Global Description Table
Dummy descriptor <>
Code Descriptor <0FFFFH,,, ATCE,>; Code Segment Descriptor
Code_sel = code-gdt; selector
Data Descriptor <0FFFH,,, ATDW,>; Data Section Descriptor Data_Sel = Data-GDT; Corresponding Selection
Note: The properties of the code segment are also the code segment, the properties of the data segment and the data segment. Don't confuse otherwise, you can't run properly.
Vcode Descriptor <0FFFH,, ATCE,>; Display Chart Segment Descriptor of Character A
Vcode_sel = vcode-gdt; correspondence
Vbuf Descriptor <0fffh, 8000H, 0BH, ATDW,>
VBUF_SEL = VBUF-GDT
Normal Descriptor <0FFFFH,,, ATDW,>; Descriptor
NORMAL_SEL = NORMAL-GDT; selector
; -----------------------------------------
GDTLEN = $ - GDT
;
VGDTR PDESC
DSEG Ends
; ----------------------------------------
Code segment
vcseg segment us16 'vcode'
Assume cs: vcseg
vStart: MOV AX, 0B800H; Direct written screen, output A
MOV DS, AX
MOV BX, 0
MOV Al, 'A'
MOV AH, 07H
MOV [BX], AX
Jump
Vcseg Ends
END VSTART
CSEG segment use16 'code'; real mode
Assume CS: CSEG, DS: DSEG
Start:
MOV AX, DSEG
MOV DS, AX
MOV BX, 16; * 16 transform into 32-bit
Mul bx
Add Ax, Offset GDT
ADC DX, 0
Mov Word Ptr vgdtr.base, AX
Mov Word Ptr Vgdtr.Base 2, DX
;
MOV AX, CSEG
Mul bx
Mov Word Ptr Code.Basel, AX
MOV BYTE PTR Code.basem, DL
Mov Byte Ptr Code.baseh, DH
;
MOV AX, DSEG
Mul bx
Mov Word PTR Data.Basel, AX
MOV BYTE PTR DATA.BASEM, DL
Mov Byte Ptr Data.Baseh, DH
;
MOV AX, VCSEG
Mul bx
Mov Word Ptr vcode.basel, AX
Mov Byte Ptr vcode.basem, DLmov Byte Ptr Vcode.Baseh, DH
;
LGDT QWORD PTR VGDTR; introducing a pseudo descriptor to GDTR, a very important step
CLI
EA20; Open A20 is a space capable of addressing 4G.
MOV EAX, CR0; Sets CR0, enters protection mode
OR EAX, 1; set CR0 to 1 into protection mode
MOV CR0, EAX
Jump
Toreal:
Mov Ax, Normal_Sel
MOV DS, AX
MOV EAX, CR0; Sets CR0, enters the protection real mode
And Eax, 0Fffffeh; set CR0 to 0, return to real mode
MOV CR0, EAX
Jump
REAL:
DA20
STI
MOV AX, DSEG; Display Character B
MOV DS, AX
MOV DL, 'B'
MOV AH, 2
Int 21h
MOV AH, 4CH
Int 21h
CSEG Ends
End Start
The above is a very simple real model and the protected mode to transition to each other, but I have also made a long time to understand. These are some of my experience, if there is incorrect hope, I hope everyone ax is. Finally thanks to 9CBS assembly sector CSDSJKK () pointing.
Reference: Yang Jiwen edited << 80x86 assembly language programming textbook >>