日期:2014-05-16  浏览次数:20659 次

运行《自己动手写操作系统》中例子时的问题
代码如下:
Assembly code


;-------------------------------------------
;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      ;这个是结束标志符




最后边的那段填充空间是我自己在例子上添加的,如果不添加,生成的二进制文件不够512字节,而且最后两位不是以55AA结尾,运行bochs会报no bootable device found的错误,加上这段就没有这个错误了,但是会有新的问题,log如下:
00000000000i[ ] ACPI support: no
00000000000i[ ] NE2000 support: no
00000000000i[ ] PCI support: no, enabled=no
00000000000i[ ] SB16 support: no
00000000000i[ ] USB support: no
00000000000i[ ] VGA extension support: vbe 
00000000000i[MEM0 ] allocated memory at 0xb4f78008. after alignment, vector=0xb4f79000
00000000000i[MEM0 ] 32.00MB
00000000000i[MEM0 ] mem block size = 0x00100000, blocks=32
00000000000i[MEM0 ] rom at 0xfffe0000/131072 ('/usr/share/bochs/BIOS-bochs-latest')
00000000000i[MEM0 ] rom at 0xc0000/40448 ('/usr/share/vgabios/vgabios.bin')
00000000000e[DEV ] Bochs is not compiled with SB16 support
00000000000i[CMOS ] Using local time for initial clock
00000000000i[CMOS ] Setting initial clock to: Thu Dec 1 12:27:40 2011 (time0=1322713660)
00000000000i[DMA ] channel 4 used by cascade
00000000000i[DMA ] channel 2 used by Floppy Drive
00000000000i[FDD ] fd0: 'Image' ro=0, h=2,t=80,spt=18
00000000000i[VGA ] interval=300000
00000000000i[MEM0 ] Register memory a