Programming <3>

zhaozj2021-02-16  48

In the previous section, we told GDT usage. I would like to talk about LDT usage, huh. The following is the source code:

; ------------------------------------------------- -------

EA20 macro; // Open the A20 address line

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

; ------------------------------------------------- ---------------------------

Jump macro selector, offsetv; // jump macro

DB 0eah

DW offsetv

DW selector

ENDM

; ---------------------

Descriptor struc; // Descriptor structure

Limitl DW 0

Basel DW 0

Basem DB 0

Attributes dw 0

Baseh DB 0

Descriptor ENDS

; ---------------------

PDESC STRUC; // Pseudo Description

Limit DW 0

Base DD 0

PDESC ENDS

;

ATDW = 92H

ATCE = 98h

Atcer = 9ah

ATLDT = 82H

TIL = 04h

.386P

; ------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

GDTSEG segment us16

GDT Label Byte

Dummy descriptor <>

Code Descriptor <0FFFFH,, ATCE,>

Code_sel = Code-GDT

; Descriptor of code segment codekseg

Codek Descriptor <0FFFFH,, ATCE,>

Codek_sel = Codek-GDT

General descriptor

Normal Descriptor <0FFFFH,, ATDW,>

NORMAL_SEL = Normal-GDT

; Partial descriptor

LDTable Descriptor <0FFFFH,, ATLDT,>

LDT_SEL = LDTable-GDT

GDTLEN = $ - GDT

VGDTR PDESC

GDTSEG Ends

; ------------------------------------------------- ------------------ Local Description Table

LDTSEG segment use6

LDT Label Byte

Data segment LDTSEG descriptor

Data Descriptor <0FFFFH,, ATDW,>

Data_sel = (Data-LDT) TIL

; Descriptor of the code segment vcode

Vcode Descriptor <0FFFFH,,, ATCE,>

vcode_sel = (vcode-ldt) til

; To display the data

VData Descriptor <0FFFFH,, ATDW,>

VDATA_SEL = (VDATA-LDT) TIL

; Data to display 2

VData1 Descriptor <0FFFFH,, ATDW,>

VDATA1_SEL = (VDATA1-LDT) TIL

;

Vbuf Descriptor <0fffh, 8000H, 0BH, ATDW,>

VBUF_SEL = (VBUF-LDT) TIL

LDTSEG ENDS

; ------------------------------------------------- ----

Code segment

Vdseg segment use6

YANG DB 'How Are you', 0

VDSEG Ends

; ------------------------------------------------- ----

vdseg1 segment us16

Hello DB 'Hello', 0

Hellolen = $ - Hello

VDSEG1 ENDS

; ------------------------------------------------- ----

vcseg segment us16

Assume cs: vcseg

vStart: MOV AX, VDATA_SEL

MOV DS, AX

MOV AX, VBUF_SEL

Mov ES, AX

Lea Si, YANG

XOR BX, BX

MOV CX, 11

Again: MOV Al, [Si]

MOV AH, 87H

MOV ES: [BX], AX

Add bx, 2

Inc Si

Loop again

Hello

MOV AX, VDATA1_SEL

MOV DS, AX

Lea Si, Hello

Mov CX, Hellolen

Again1: MOV Al, [Si]

MOV AH, 0F4H

MOV ES: [BX], AX

Add bx, 2

Inc Si

LOOP AGAIN1

Gojump: Jump ,

Vcseg Ends

; ------------------------------------------------- ----

Codekseg segment us16

Assume CS: Codekseg

Startk: MOV AX, LDT_SEL

LLDT AX

Jump ,

CodekSeg Ends

; ------------------------------------------------- ---- CSEG segment use6

Assume CS: CSEG, DS: GDTSEG

Start: MOV AX, GDTSEG

MOV DS, AX

MOV BX, 16

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, Codekseg

Mul bx

Mov Word PTR Codek.Basel, AX

Mov Byte Ptr Codek.basem, DL

Mov Byte Ptr Codek.Baseh, DH

;

MOV AX, LDTSEG

Mul bx

Mov Word Ptr Ldtable.Basel, AX

MOV BYTE PTR LDTABLE.BASEM, DL

Mov Byte Ptr Ldtable.Baseh, DH

;

Push DS

MOV AX, LDTSEG

MOV DS, AX

Assume DS: ldtseg

MOV AX, VCSEG

Mul bx

Mov Word Ptr vcode.basel, AX

MOV BYTE PTR Vcode.basem, DL

Mov Byte Ptr vcode.baseh, DH

;

MOV AX, VDSEG

Mul bx

Mov Word Ptr vdata.Basel, AX

MOV BYTE PTR VDATA.BASEM, DL

Mov Byte Ptr vdata.baseh, DH

;

MOV AX, VDSEG1

Mul bx

Mov Word Ptr vdata1.basel, AX

MOV BYTE PTR VDATA1.BASEM, DL

Mov Byte Ptr vdata1.baseh, DH

POP DS

;

Assume DS: GDTSEG

LGDT QWORD PTR VGDTR

CLI

EA20; //

MOV EAX, CR0; // Enter protection mode

OR EAX, 1; //

MOV CR0, EAX; //

JUMP , ; // proctect mode

Toreal: Mov AX, Normal_SEL

MOV DS, AX

MOV Eax, Cr0

And Eax, 0FFFFFEHMOV CR0, EAX

Jump ,

REAL: DA20

STI

MOV AH, 4CH

Int 21h

CSEG Ends

End Start

This code, we can use the previous map. To use LDT, we need to use MOV AX, GDTSEG, LLDT AX these two commands are sent to the local description table to the local descriptor table register. Do two After the command, we can use the paragraphs pointed to the table in the local descriptor. Huh,.

转载请注明原文地址:https://www.9cbs.com/read-26966.html

New Post(0)