Implementation of an operating system: protection mode

Source: Internet
Author: User

Today, I learned the protection mode and finally completed my own protection mode based on the content in the book and the video from the big gray wolf teacher.

[BITS 16]; indicates the following 16 BITS

Org 07c00h; this is because the system loads the code at 7C00.

Jmp start

Gdt_table_start:

Gdt_null:

Dd 0 h

Dd 0 h; Intel requires that the first descriptor in the descriptor table must be an empty descriptor.

Gdt_data_addr equ $-gdt_table_start

Gdt_data:

Dw 07FFh; the value of the data segment. The size of a Data Segment and code segment must be 8 Mb.

Dw 0 h; Segment Base Address 0 ~ 18-digit

Db 0 h; 19 ~ of the base address of the segment ~ 23-bit

Db 10010010b; the first 1 indicates P, which indicates whether the descriptor is valid for address conversion. The valid value is 1.

; The second 1 indicates DT, indicating the system segment or storage segment, and 1 indicates the system segment

; The third 1, together with 0010, indicates the type, that is, the readable and writable data segment or the readable and executable code segment.

; The sixth byte of the segment attribute

Db 111000000b; the first 1 indicates that the data segment is 4 GB. If it is 0, it indicates 64 K; the Code segment is 32 bits; if it is 0, it indicates 16 bits.

The second 1 indicates that the segment boundary granularity is byte, and 0 indicates 4 K.

; The seventh byte of the segment attribute

Db 0 h; base address

Gdt_code_addr equ $-gdt_table_start

Gdt_code:

Dw 07FFh; the value of the code segment. The size of a Data Segment and code segment must be 8 Mb.

Dw 1 h; Segment Base Address 0 ~ 18-digit

Db 80 h; 19 ~ of the base address of the segment ~ 23-bit

Db 10011010b; the first 1 indicates P, which indicates whether the descriptor is valid for address conversion. The valid value is 1.

; The second 1 indicates DT, indicating the system segment or storage segment, and 1 indicates the system segment

; The third 1, together with 1010, indicates the type, that is, the readable and writable data segment or the readable and executable code segment.

; The sixth byte of the segment attribute

Db 111000000b; the first 1 indicates that the data segment is 4 GB. If it is 0, it indicates 64 K.

; The second 1 indicates that the code segment is 32 bits, and 0 indicates 16 bits

; The seventh byte of the segment attribute

Db 0 h; base address

Gdt_video_addr equ $-gdt_table_start

Gdt_video:

Dw 01FFh; the Code segment's boundary value, which can be any value

Dw 8000 h; 0 ~ of the base address of the segment ~ 18-digit

Db 0Bh; 19 ~ of the base address of the segment ~ 23-bit

Db 10010010b; the first 1 indicates P, which indicates whether the descriptor is valid for address conversion. The valid value is 1.

; The second 1 indicates DT, indicating the system segment or storage segment, and 1 indicates the system segment

; The third 1, together with 1010, indicates the type, that is, the readable and writable data segment or the readable and executable code segment.

; The sixth byte of the segment attribute

Db 111000000b; the first 1 indicates that the data segment is 4 GB. If it is 0, it indicates 64 K.

; The second 1 indicates that the code segment is 32 bits, and 0 indicates 16 bits

; The seventh byte of the segment attribute

Db 0 h; base address

Gdt_table_end:

 

Gdtr_addr:

Dw gdt_table_end-gdt_table_start-1; segment descriptor table length, GDTR register is 48 bits

Dd gdt_table_start; base address of the segment descriptor table

 

Start:

; Initialize the data segment descriptor

Xor eax, eax

Mov eax, data_32

Mov word [gdt_data + 2], ax

Shr eax, 16

Mov byte [gdt_data + 4], al

Mov byte [gdt_data + 7], ah

; Initialize the code segment descriptor

Xor eax, eax

Mov eax, code_32

Mov word [gdt_code + 2], ax

Shr eax, 16

Mov byte [gdt_code + 4], al

Mov byte [gdt_code + 7], ah

 

Closing the interrupt vector table is equivalent to deleting the previous 16-bit interrupt vector table to prepare for loading the 32-bit interrupt vector table.

Cli

 

Load the size and base address of the GDT description table into the gdtr register.

Lgdt [gdtr_addr]

 

Open the A20 address line to access the address memory that exceeds 1 MB

In al, 92 h

Or al, 00000010b

Out 92 h, al

 

; Enable the protection mode to allow

Mov eax, cr0

Or eax, 1

Mov cr0, eax

 

Jump to the 32-bit address in Protected Mode

Jmp gdt_code_addr: 0

 

[BITS 32]

Data_32:

Dd "my OS! "

Code_32:

Mov ax, gdt_data_addr

Mov ds, ax

Mov ax, gdt_video_addr

Mov gs, ax

Mov edi, (80*20 + 30) * 2; display data in column 20th of Row 3 on the screen my OS!

Mov ah, 0ch; black background red letter

Mov bx, 0

Mov cx, 6

S:

Mov al, [ds: bx]

Mov [gs: edi], al

Mov [gs: edi + 1], ah

Inc bx

Add EDI, 2

Loop s

JMP $

Times 510-($-$) db 0

DW 0aa55h

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.