日期:2014-05-16 浏览次数:20879 次
;-------------------------------------------
;boot.asm
;Usage : nasm boot.asm -o boot.bin
;-------------------------------------------
%include "pm.inc"
org 07c00h
jmp START
SECTION .gdt
; base limit attr
GDT_NULL :Descriptor 0, 0, 0 ;Null descriptor
GDT_DESC_CODE32 :Descriptor 0, SegCode32Len - 1, DA_C + DA_32
GDT_DESC_VIDEO :Descriptor 0B8000h, 0ffffh, DA_DRW ;Graphic Memory descriptor
;End of Gdt
GdtLen equ $-GDT_NULL ;Gdt length
GdtPtr dw GdtLen-1 ;Gdt limit
dd 0
;Selector
Code32Selector equ GDT_DESC_CODE32 - GDT_NULL
VideoSelector equ GDT_DESC_VIDEO - GDT_NULL
;End of selcetor
[SECTION .s16]
[BITS 16]
START:
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0100h
;init the base of code32selector
xor eax, eax
mov ax, cs
shl eax, 4
add eax, SEG_CODE32
mov word [GDT_DESC_CODE32 + 2], ax
shr eax, 16
mov word [GDT_DESC_CODE32 + 4], ax
mov word [GDT_DESC_CODE32 + 7], ax
;init gdtr
xor eax, eax
mov ax, ds
shl eax, 4
add eax, GDT_NULL ;eax <- gdt base
mov dword [GdtPtr + 2], eax ;eax -> GdtPtr
;loda gdtr
lgdt [GdtPtr]
;close interrupt
cli
;open A20
in al, 92h
or al, 00000010b
out 92h, al
;ready to protected mode
mov eax, cr0
or eax, 1
mov cr0, eax
;jmp to protected mode
jmp dword Code32Selector:0 ;Code32Selector32 -> cs, 0->ip
;End of Section .s16
[SECTION .s32]
[BITS 32]
SEG_CODE32:
mov ax, VideoSelector
mov gs, ax ;Graphic Segement Selector
mov edi, (80 * 14 + 77) * 2 ;Column 15, Row 77
mov ah, 0Ch ;Background : Black Font Color : Red
mov al, 'P'
mov [gs:edi], ax
;stop
jmp $
SegCode32Len equ $-SEG_CODE32
;End of Section .s32
times 386-($-$$) db 0 ; 填充剩下的空间,使生成的二进制代码恰好为512字节
dw 0xaa55 ;这个是结束标志符