failed to jmp from real mode to protect mode
Posted: Sat Oct 22, 2011 6:02 am
I use nasm to make binaray file, then dd it to a floopy image, but when I run this code in bochs, it failed and I don not kown why.
Poor English ;p If you can understand me, give me a hand please.
org 0x7c00
jmp BEGIN
NULL_DESC:
dd 0
dd 0
CODE_DESC:
dw 0xffff
dw 0
db 0
db 0x98
db 0x40
db 0
VIDEO_DESC:
dw 0xffff
dw 0x8000
db 0x0b
db 0x92
db 0
db 0
; 这个结构会加载到寄存器 gdtr
GDT_LEN equ $-NULL_DESC
GDT_PTR dw GDT_LEN-1
dd 0
; Selector of GDT
Selector_CODE equ CODE_DESC-NULL_DESC
Selector_VIDEO equ VIDEO_DESC-NULL_DESC
[BITS 16]
BEGIN:
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x100
; 设定CODE_DESC的段基址
xor eax, eax
mov ax, cs
shl eax, 4
add eax, CODE_SEG
mov word [CODE_DESC+2], ax
shr eax, 16
mov byte [CODE_DESC+4], al
mov byte [CODE_DESC+7], ah
; 设定GDT_PTR中的GDT基址
xor eax, eax
mov ax, ds
shl eax, 4
add eax, NULL_DESC
mov dword [GDT_PTR+2], eax
; 加载GDTR
lgdt [GDT_PTR]
; 进入保护模式
cli
in al, 0x92
or al, 0x02
out 92h, al
mov eax, cr0
or eax, 1
mov cr0, eax
jmp dword Selector_CODE:0
[BITS 32]
CODE_SEG:
mov ax, Selector_VIDEO
mov gs, ax
mov edi, 1
mov al, 'a'
mov ah, 0x0f
mov [gs:edi], ax
jmp $
CODE_SEG_LEN equ $-CODE_SEG
Poor English ;p If you can understand me, give me a hand please.
org 0x7c00
jmp BEGIN
NULL_DESC:
dd 0
dd 0
CODE_DESC:
dw 0xffff
dw 0
db 0
db 0x98
db 0x40
db 0
VIDEO_DESC:
dw 0xffff
dw 0x8000
db 0x0b
db 0x92
db 0
db 0
; 这个结构会加载到寄存器 gdtr
GDT_LEN equ $-NULL_DESC
GDT_PTR dw GDT_LEN-1
dd 0
; Selector of GDT
Selector_CODE equ CODE_DESC-NULL_DESC
Selector_VIDEO equ VIDEO_DESC-NULL_DESC
[BITS 16]
BEGIN:
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x100
; 设定CODE_DESC的段基址
xor eax, eax
mov ax, cs
shl eax, 4
add eax, CODE_SEG
mov word [CODE_DESC+2], ax
shr eax, 16
mov byte [CODE_DESC+4], al
mov byte [CODE_DESC+7], ah
; 设定GDT_PTR中的GDT基址
xor eax, eax
mov ax, ds
shl eax, 4
add eax, NULL_DESC
mov dword [GDT_PTR+2], eax
; 加载GDTR
lgdt [GDT_PTR]
; 进入保护模式
cli
in al, 0x92
or al, 0x02
out 92h, al
mov eax, cr0
or eax, 1
mov cr0, eax
jmp dword Selector_CODE:0
[BITS 32]
CODE_SEG:
mov ax, Selector_VIDEO
mov gs, ax
mov edi, 1
mov al, 'a'
mov ah, 0x0f
mov [gs:edi], ax
jmp $
CODE_SEG_LEN equ $-CODE_SEG