潜在的错误
我相信您的大部分问题都源于您的某些GDT descriptors 中的标志问题。在 CODE 描述符表中,您有:
db 0x9a ; 1001 1011 - Vaild, ring 0, executable, strict, readable,
db 0x9f ; 1100 1111 - paging, 32-bit
应该是:
db 0x9a ; 1001 1010 - Valid, ring 0, executable, strict, readable,
db 0xcf ; 1100 1111 - paging, 32-bit
特别是1100 1111 是0xcf,而不是0x9f。您还可以使用二进制值定义 DB,例如:
db 10011010b ; Valid, ring 0, executable, strict, readable,
db 11001111b ; paging, 32-bit
通过以这种方式使用二进制值,它更不容易出错并且更具可读性。
在您拥有的 DATA 描述符中:
db 0x92 ; 1001 0011 - vaild, ring 0, data, forward, writable, accessed
db 0x9F ; 1100 1111 - 4KB, 32-bits
我认为应该是:
db 10010010b ; valid, ring 0, data, forward, writable, accessed
db 11001111b ; 4KB, 32-bits
您的 0x9F 应该是 0xCF (11001111)
其他注意事项
进入保护模式后设置SS:ESP 是个好主意。这样的代码可以工作:
mov es, ax ; Set ES as well
mov ss, ax
mov esp,0x90000 ; We should specify a stack pointer
cld ; Set direction flag forward
0x90000 可以是任何有效的线性地址。由于像MOVSB 这样的一些指令需要设置ES,所以最好设置ES=DS。如果您使用任何需要方向标志的指令,最好明确设置它。大多数代码假定可以使用CLD 指令设置向前移动。
作为一个实验,您可以添加此代码以显示内存中的字符串。这将测试为 DS 生成的偏移量是否按预期工作;并测试 GS 段是否指向 0xb8000 处的彩色文本视频内存的底部。
; print "Hello, World!"
xor ax, ax
mov edi, 0
mov ecx, 0
mov ah, 0x07
; Display a nul terminated string from a variable to video memory
; using a character attribute in AH
strloop:
mov al, [mystr+ecx]
test al,al
je .endstrloop
mov [gs:edi+ecx*2], ax
inc ecx
jmp strloop
.endstrloop:
修改后的代码
修改后的 stage2 代码可能如下所示:
; stage2
; still -f bin
[org 0xd200] ; calculated by first stage loader
; GDTs are defined here
jmp entry
GDT_DESC_START:
GDT0:
dw 0 ; Limit 0~15
dw 0 ; Base 0~15
db 0 ; Base 16~23
db 0 ; Access Byte
db 0 ; Limit 16~19 + Flags
db 0 ; Base 24~31
DESCRIPTOR_CODE:
dw 0xffff ; Limit 0~15
CODE_BASE_AX dw 0 ; Base 0~15
CODE_BASE_EAHL db 0 ; Base 16~23
db 10011010b ; Valid, ring 0, executable, strict, readable,
; accessed
db 11001111b ; paging, 32-bit
CODE_BASE_EAHH db 0 ; Base 24~31
DESCRIPTOR_DATA:
dw 0xffff ; Limit 0~15
dw 0x0000 ; Base 0~15
db 0x00 ; Base 16~23
db 10010010b ; valid, ring 0, data, forward, writable, accessed
db 11001111b ; 4KB, 32-bits
db 0x0 ; Base 24~31
; Note that in further development, VRAM descriptor should be
; created dynamically
; Base = 0x000b8000
DESCRIPTOR_VIDEO:
dw 0xffff
dw 0x8000
db 0x0b
db 0x92
db 0x4F
db 0x00
GDT_DESC_END:
GDTR0:
dw GDT_DESC_END - GDT_DESC_START - 1
dd GDT_DESC_START
SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
SELECTOR_DATA equ DESCRIPTOR_DATA - GDT0
SELECTOR_VIDEO equ DESCRIPTOR_VIDEO - GDT0
; --- code ------------
[bits 16]
entry:
cli
; open a20 gate
in al, 0x92
or al, 0x02
and al, ~1
out 0x92, al
; goto protected mode
; rewrite code descriptor
xor eax, eax
mov eax, entry_32
mov [CODE_BASE_AX], ax
shr eax, 16
mov [CODE_BASE_EAHH], ah
mov [CODE_BASE_EAHL], al
; load the gdt
lgdt [GDTR0]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp dword SELECTOR_CODE:0x0 ; -> protected mode!!!
[bits 32]
entry_32:
mov ax, SELECTOR_DATA
mov ds, ax
mov es, ax ; Set ES as well
mov ss, ax
mov esp,0x90000 ; We should specify a stack pointer
mov ax, SELECTOR_VIDEO
mov gs, ax
cld ; Set direction flag forward
; print "Hello, World!"
xor ax, ax
mov edi, 0
mov ecx, 0
mov ah, 0x07
; Display a nul terminated string from a variable to video memory
; using a character attribute in AH
strloop:
mov al, [mystr+ecx]
test al,al
je .endstrloop
mov [gs:edi+ecx*2], ax
inc ecx
jmp strloop
.endstrloop:
.sleep:
hlt
jmp .sleep
mystr: db "Hello, World!",0
虽然您的问题没有第一阶段,但我为可能希望测试的读者提供了一个简单的问题:
[BITS 16]
org 0x7C00
STAGE2OFFSET equ 0xd200
start:
; This section of code is added based on Michael Petch's bootloader tips
; See http://stackoverflow.com/a/32705076/3857942
xor ax,ax ; We want a segment of 0 for DS for this question
mov ds,ax ; Set AX to appropriate segment value for your situation
mov es,ax ; In this case we'll default to ES=DS
mov bx,0x7c00 ; Stack segment can be any usable memory
cli ; Disable interrupts to circumvent bug on early 8088 CPUs
mov ss,ax
mov sp,bx ; This places top of the stack @ 0x0000:0x7c00
; just below the bootsector.
sti ; Re-enable interrupts
cld ; Set the direction flag to be positive direction
mov ah, 0x02 ; Read sectors from drive
mov al, 4 ; Read 4 sectors
mov ch, 0 ; Cylinder 0
mov cl, 2 ; Sector 2
mov dh, 0 ; Head 0
xor bx, bx
mov es, bx
mov bx, STAGE2OFFSET ; ES:BX = 0x0000:0xd200 (start of memory to read into)
int 0x13
jmp 0x0000:STAGE2OFFSET ; Far Jump to second stage. Sets CS=0
times 510-($-$$) db 0 ; Create padding to fill out to 510 bytes
dw 0xaa55 ; Magic number in the trailer of a boot sector
可以使用以下命令将这些文件组装并构建到磁盘映像中:
dd if=/dev/zero of=floppy.img bs=1024 count=1440
nasm -f bin bootload.asm -o bootload.bin
nasm -f bin stage2.asm -o stage2.bin
dd if=bootload.bin of=floppy.img conv=notrunc
dd if=stage2.bin of=floppy.img bs=512 seek=1 conv=notrunc
代码段推荐
虽然您设置 CODE 描述符的方式可行,但另一种选择是使用零基数。这可以通过以下方式完成:
; rewrite code descriptor
xor eax, eax
mov eax, entry_32
mov [CODE_BASE_AX], ax
shr eax, 16
mov [CODE_BASE_EAHH], ah
mov [CODE_BASE_EAHL], al
- 修改ptr16:32 FAR JMP以使用
entry_32的偏移量:
jmp dword SELECTOR_CODE:entry_32 ; -> protected mode!!!
我们简单地使用 0 的描述符基数,而不是 0 偏移量和非零描述符基数。然后我们可以将标签 entry_32 的偏移量用于ptr16:32 FAR JMP。通过保持 CS=DS=ES=SS,您可以使用 4gb 线性地址空间来简化代码0. 保持一致可以避免将来出现潜在的编码错误。