OS——保护模式编程(1)

[section .gdt]    ---->   it is used for define the globla descriptors in the specfic table,

            in the system, there is only one gdt; note that!!!

            do that format the function is for view the code easily;

 

[section .s16]    ---->   as the program begin to run in the real mode, this section will first run;

            it for init the Descriptors in the GDT, then load the gdt to the gdter;

            jmp dword .... in the nasm, it is wonderful; otherwise, you have to write

            binary code to realise this "jump";

 

[section .s32]    ---->   the code is 32 bits, it run in the protected mode, just show the message;

            you should note that, you can not use the int xxh of BIOS or DOS, what you

            can do just write data to video memory, so you can show the message in the

            screen as you want to show there infomation;

%include "pm.inc"

org 0100h

  jmp LABEL_BEGIN

 

[SECTION .gdt]

LABEL_GDT    Descriptor  0,  0,  0

LABEL_DESC_CODE32  Descriptor  0,  SegCodeLen - 1,  DA_C+DA_32

LABEL_DESC_VIDEO   Descriptor  0b8000h,  0ffffh,    DA_DRW

 

GdtLen  equ  $ - LABEL_GDT

GdtPtr   dw GdtLen - 1

      dd 0

 

SelectorCode32  equ  LABEL_DESC_CODE32  - LABEL_GDT

SelectorVideo   equ  LABEL_DESC_VIDEO - LABEL_GDT

 

 

[SECTION .s16]

[BITS 16]

LABEL_BEGIN:

  mov ax, cs

  mov ds, ax

  mov es, ax

  mov ss, ax

  mov sp, 0100h

 

 

  ; Init the SelectorCode32

  xor eax, eax

  mov ax, ds

  shl eax, 4

  add eax, LABEL_SEG_CODE32

  mov word [LABEL_DESC_CODE32 + 2], ax

  shr eax, 16

  mov byte [LABEL_DESC_CODE32+ 4], al

  mov byte [LABEL_DESC_CODE32 + 7], ah

 

  ; init the Gdt

  xor eax, eax

  mov ax, ds

  shl eax, 4

  add eax, LABEL_GDT

  mov dword [GdtPtr + 2], eax

 

  lgdt [GdtPtr]

 

  cli

  

  in al, 92h

  or al, 00000010b

  out 92h, al

 

  mov eax, cr0

  or eax, 1

  mov cr0, eax

 

  jmp dword SelectorCode32:0

 

 

[SECTION .s32]

[BITS 32]

LABEL_SEG_CODE32:

  mov ax, SelectorVideo

  mov gs, ax

  mov edi, (80*10 + 0)*2

  mov ah, 0ch

  mov al, 'P'

  mov [gs:edi], ax

  

  jmp $

  

  SegCode32Len equ $ - LABEL_SEG_CODE32

 

 

 

posted @ 2013-07-06 22:59  Auris  阅读(269)  评论(0编辑  收藏  举报