跳到保护模式不起作用或没有任何效果



我有一个用MASM编写的引导加载程序,它由JWASM编译。到目前为止,它工作得很完美,然而,当它应该跳到保护模式时,什么也没发生。它应该在屏幕左上角打印一个黄色的P,但它没有出现。

boot.asm:

.386p
option segment:use16, casemap:none
.model TINY, C, NEARSTACK
;------------------------------
; CODE
;------------------------------
.code
org 07C00h
Error PROTO lpStr:WORD
Print PROTO lpStr:WORD
ReadDisk PROTO nSectorsRead:BYTE, nCylinder:BYTE, nSector:BYTE, nHead:BYTE, nDrive:BYTE, lpBuffer:WORD
NewLine PROTO
PrintLn PROTO lpStr:WORD
PrintWord PROTO num:WORD
ReadKey PROTO
_start:
mov [bootDisk], dl

xor ax, ax
mov ds, ax
mov ss, ax
mov es, ax
mov bp, 07BFFh
mov sp, bp
INVOKE PrintLn, OFFSET bootInfo         ;Onyl works because we are using JWASM as our compiler!
INVOKE PrintLn, OFFSET loadingInfo
INVOKE ReadDisk, 1, 0, 2, 0, [bootDisk], 07E00h
INVOKE Print, OFFSET pageInfo
INVOKE PrintWord, MEM_PAGE_COUNT
INVOKE NewLine
INVOKE PrintLn, OFFSET protectedInfo

lgdt fword ptr [GDT_desc]
mov eax, cr0
or eax, 1
mov cr0, eax
db 66h                                  ;manually encoded far jump = CODE_SEG:start_protected_mode
db 0EAh
dd OFFSET start_protected_mode
dw CODE_SEG
Error PROC lpStr:WORD
INVOKE Print, lpStr
mov ah, 0
int 16h
db 0EAh                         ;manually encoded reboot
dw 0000h
dw 0FFFFh
Error ENDP
Print PROC USES ax si lpStr:WORD
mov si, lpStr
mov ah, 0Eh
@@loop:
lodsb
cmp al, 0
je @@done
int 10h
jmp @@loop
@@done:
ret
Print ENDP
NewLine PROC USES ax
mov ah, 0Eh
mov al, 13
int 10h
mov al, 10
int 10h
ret
NewLine ENDP
PrintLn PROC lpStr:WORD
INVOKE Print, lpStr
INVOKE NewLine
ret
PrintLn ENDP
ReadDisk PROC uses ax cx dx bx nSectorsRead:BYTE, nCylinder:BYTE, nSector:BYTE, nHead:BYTE, nDrive:BYTE, lpBuffer:WORD
mov ah, 02h
mov al, nSectorsRead
mov ch, nCylinder
mov cl, nSector
mov dh, nHead
mov dl, nDrive
mov bx, lpBuffer
int 13h
jnc @@end
INVOKE Error, OFFSET diskReadError
@@end:
ret
ReadDisk ENDP
;------------------------------
; DATA
;------------------------------
bootDisk byte 1
bootInfo db "Booting CompatOS...", 0
loadingInfo db "Loading extra sector...", 0
diskReadError db "Disk read failed!", 0
pageInfo db "Page count: ", 0
GDT_null    dq 0
GDT_code    dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0
GDT_data    dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0
GDT_desc    dw OFFSET GDT_desc - OFFSET GDT_null - 1
GDT_addr    dd OFFSET GDT_null
CODE_SEG equ GDT_code - GDT_null
DATA_SEG equ GDT_data - GDT_null
;------------------------------
; PADDING
;------------------------------
byte 510-($-_start) dup (0)
dw 0AA55h
org 07E00h
MEM_BEG equ 08000h
MEM_END equ 0FFFFh
MEM_PAGE_COUNT equ (MEM_END - MEM_BEG) / 0200h
;------------------------------
; CODE
;------------------------------
PrintWord PROC USES ax bx dx num:WORD
LOCAL digit:BYTE
.if num >= 10
mov ax, num
mov dx, 0
mov bx, 10
div bx
INVOKE PrintWord, ax
.endif
mov ax, num
mov dx, 0
mov bx, 10
div bx
mov digit, dl
mov ah, 0Eh
mov al, '0'
add al, digit
int 10h
ret
PrintWord ENDP
ReadKey PROC
mov ah, 0
int 16h
xor ah, ah
ret
ReadKey ENDP
option segment:use32
start_protected_mode:
mov ax, DATA_SEG
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ebp, 90000h
mov esp, ebp
mov eax, 0B8000h
mov [eax], 'P'
mov [eax+1], 14
cli
hlt
;------------------------------
; DATA
;------------------------------
protectedInfo db "Entering protected mode...", 0
;------------------------------
; PADDING
;------------------------------
byte 1024-($-_start) dup (0)
END _start

反汇编:

00000000  8816E47C          mov [0x7ce4],dl
00000004  33C0              xor ax,ax
00000006  8ED8              mov ds,ax
00000008  8ED0              mov ss,ax
0000000A  8EC0              mov es,ax
0000000C  BDFF7B            mov bp,0x7bff
0000000F  8BE5              mov sp,bp
00000011  68E57C            push word 0x7ce5
00000014  E88E00            call 0xa5
00000017  83C402            add sp,byte +0x2
0000001A  68F97C            push word 0x7cf9
0000001D  E88500            call 0xa5
00000020  83C402            add sp,byte +0x2
00000023  68007E            push word 0x7e00
00000026  A0E47C            mov al,[0x7ce4]
00000029  50                push ax
0000002A  6A00              push byte +0x0
0000002C  6A02              push byte +0x2
0000002E  6A00              push byte +0x0
00000030  6A01              push byte +0x1
00000032  E88100            call 0xb6
00000035  83C40C            add sp,byte +0xc
00000038  68237D            push word 0x7d23
0000003B  E84300            call 0x81
0000003E  83C402            add sp,byte +0x2
00000041  6A3F              push byte +0x3f
00000043  E8BA01            call 0x200
00000046  83C402            add sp,byte +0x2
00000049  E84C00            call 0x98
0000004C  686B7E            push word 0x7e6b
0000004F  E85300            call 0xa5
00000052  83C402            add sp,byte +0x2
00000055  0F0116487D        lgdt [0x7d48]
0000005A  0F20C0            mov eax,cr0
0000005D  6683C801          or eax,byte +0x1
00000061  0F22C0            mov cr0,eax
00000064  66EA447E00000800  jmp dword 0x8:0x7e44
0000006C  55                push bp
0000006D  8BEC              mov bp,sp
0000006F  FF7604            push word [bp+0x4]
00000072  E80C00            call 0x81
00000075  83C402            add sp,byte +0x2
00000078  B400              mov ah,0x0
0000007A  CD16              int 0x16
0000007C  EA0000FFFF        jmp 0xffff:0x0
00000081  55                push bp
00000082  8BEC              mov bp,sp
00000084  50                push ax
00000085  56                push si
00000086  8B7604            mov si,[bp+0x4]
00000089  B40E              mov ah,0xe
0000008B  AC                lodsb
0000008C  3C00              cmp al,0x0
0000008E  7404              jz 0x94
00000090  CD10              int 0x10
00000092  EBF7              jmp short 0x8b
00000094  5E                pop si
00000095  58                pop ax
00000096  C9                leave
00000097  C3                ret
00000098  50                push ax
00000099  B40E              mov ah,0xe
0000009B  B00D              mov al,0xd
0000009D  CD10              int 0x10
0000009F  B00A              mov al,0xa
000000A1  CD10              int 0x10
000000A3  58                pop ax
000000A4  C3                ret
000000A5  55                push bp
000000A6  8BEC              mov bp,sp
000000A8  FF7604            push word [bp+0x4]
000000AB  E8D3FF            call 0x81
000000AE  83C402            add sp,byte +0x2
000000B1  E8E4FF            call 0x98
000000B4  C9                leave
000000B5  C3                ret
000000B6  55                push bp
000000B7  8BEC              mov bp,sp
000000B9  50                push ax
000000BA  51                push cx
000000BB  52                push dx
000000BC  53                push bx
000000BD  B402              mov ah,0x2
000000BF  8A4604            mov al,[bp+0x4]
000000C2  8A6E06            mov ch,[bp+0x6]
000000C5  8A4E08            mov cl,[bp+0x8]
000000C8  8A760A            mov dh,[bp+0xa]
000000CB  8A560C            mov dl,[bp+0xc]
000000CE  8B5E0E            mov bx,[bp+0xe]
000000D1  CD13              int 0x13
000000D3  7309              jnc 0xde
000000D5  68117D            push word 0x7d11
000000D8  E891FF            call 0x6c
000000DB  83C402            add sp,byte +0x2
000000DE  5B                pop bx
000000DF  5A                pop dx
000000E0  59                pop cx
000000E1  58                pop ax
000000E2  C9                leave
000000E3  C3                ret
000000E4  01426F            add [bp+si+0x6f],ax
000000E7  6F                outsw
000000E8  7469              jz 0x153
000000EA  6E                outsb
000000EB  6720436F          and [ebx+0x6f],al
000000EF  6D                insw
000000F0  7061              jo 0x153
000000F2  744F              jz 0x143
000000F4  53                push bx
000000F5  2E2E2E004C6F      add [cs:si+0x6f],cl
000000FB  61                popa
000000FC  64696E672065      imul bp,[fs:bp+0x67],word 0x6520
00000102  7874              js 0x178
00000104  7261              jc 0x167
00000106  207365            and [bp+di+0x65],dh
00000109  63746F            arpl [si+0x6f],si
0000010C  722E              jc 0x13c
0000010E  2E2E004469        add [cs:si+0x69],al
00000113  736B              jnc 0x180
00000115  207265            and [bp+si+0x65],dh
00000118  61                popa
00000119  64206661          and [fs:bp+0x61],ah
0000011D  696C656421        imul bp,[si+0x65],word 0x2164
00000122  005061            add [bx+si+0x61],dl
00000125  676520636F        and [gs:ebx+0x6f],ah
0000012A  756E              jnz 0x19a
0000012C  743A              jz 0x168
0000012E  2000              and [bx+si],al
00000130  0000              add [bx+si],al
00000132  0000              add [bx+si],al
00000134  0000              add [bx+si],al
00000136  0000              add [bx+si],al
00000138  FF                db 0xff
00000139  FF00              inc word [bx+si]
0000013B  0000              add [bx+si],al
0000013D  9ACF00FFFF        call 0xffff:0xcf
00000142  0000              add [bx+si],al
00000144  0092CF00          add [bp+si+0xcf],dl
00000148  17                pop ss
00000149  0030              add [bx+si],dh
0000014B  7D00              jnl 0x14d
0000014D  0000              add [bx+si],al
0000014F  0000              add [bx+si],al
00000151  0000              add [bx+si],al
00000153  0000              add [bx+si],al
00000155  0000              add [bx+si],al
00000157  0000              add [bx+si],al
00000159  0000              add [bx+si],al
0000015B  0000              add [bx+si],al
0000015D  0000              add [bx+si],al
0000015F  0000              add [bx+si],al
00000161  0000              add [bx+si],al
00000163  0000              add [bx+si],al
00000165  0000              add [bx+si],al
00000167  0000              add [bx+si],al
00000169  0000              add [bx+si],al
0000016B  0000              add [bx+si],al
0000016D  0000              add [bx+si],al
0000016F  0000              add [bx+si],al
00000171  0000              add [bx+si],al
00000173  0000              add [bx+si],al
00000175  0000              add [bx+si],al
00000177  0000              add [bx+si],al
00000179  0000              add [bx+si],al
0000017B  0000              add [bx+si],al
0000017D  0000              add [bx+si],al
0000017F  0000              add [bx+si],al
00000181  0000              add [bx+si],al
00000183  0000              add [bx+si],al
00000185  0000              add [bx+si],al
00000187  0000              add [bx+si],al
00000189  0000              add [bx+si],al
0000018B  0000              add [bx+si],al
0000018D  0000              add [bx+si],al
0000018F  0000              add [bx+si],al
00000191  0000              add [bx+si],al
00000193  0000              add [bx+si],al
00000195  0000              add [bx+si],al
00000197  0000              add [bx+si],al
00000199  0000              add [bx+si],al
0000019B  0000              add [bx+si],al
0000019D  0000              add [bx+si],al
0000019F  0000              add [bx+si],al
000001A1  0000              add [bx+si],al
000001A3  0000              add [bx+si],al
000001A5  0000              add [bx+si],al
000001A7  0000              add [bx+si],al
000001A9  0000              add [bx+si],al
000001AB  0000              add [bx+si],al
000001AD  0000              add [bx+si],al
000001AF  0000              add [bx+si],al
000001B1  0000              add [bx+si],al
000001B3  0000              add [bx+si],al
000001B5  0000              add [bx+si],al
000001B7  0000              add [bx+si],al
000001B9  0000              add [bx+si],al
000001BB  0000              add [bx+si],al
000001BD  0000              add [bx+si],al
000001BF  0000              add [bx+si],al
000001C1  0000              add [bx+si],al
000001C3  0000              add [bx+si],al
000001C5  0000              add [bx+si],al
000001C7  0000              add [bx+si],al
000001C9  0000              add [bx+si],al
000001CB  0000              add [bx+si],al
000001CD  0000              add [bx+si],al
000001CF  0000              add [bx+si],al
000001D1  0000              add [bx+si],al
000001D3  0000              add [bx+si],al
000001D5  0000              add [bx+si],al
000001D7  0000              add [bx+si],al
000001D9  0000              add [bx+si],al
000001DB  0000              add [bx+si],al
000001DD  0000              add [bx+si],al
000001DF  0000              add [bx+si],al
000001E1  0000              add [bx+si],al
000001E3  0000              add [bx+si],al
000001E5  0000              add [bx+si],al
000001E7  0000              add [bx+si],al
000001E9  0000              add [bx+si],al
000001EB  0000              add [bx+si],al
000001ED  0000              add [bx+si],al
000001EF  0000              add [bx+si],al
000001F1  0000              add [bx+si],al
000001F3  0000              add [bx+si],al
000001F5  0000              add [bx+si],al
000001F7  0000              add [bx+si],al
000001F9  0000              add [bx+si],al
000001FB  0000              add [bx+si],al
000001FD  0055AA            add [di-0x56],dl
00000200  55                push bp
00000201  8BEC              mov bp,sp
00000203  83EC02            sub sp,byte +0x2
00000206  50                push ax
00000207  53                push bx
00000208  52                push dx
00000209  837E040A          cmp word [bp+0x4],byte +0xa
0000020D  7212              jc 0x221
0000020F  8B4604            mov ax,[bp+0x4]
00000212  BA0000            mov dx,0x0
00000215  BB0A00            mov bx,0xa
00000218  F7F3              div bx
0000021A  50                push ax
0000021B  E8E2FF            call 0x200
0000021E  83C402            add sp,byte +0x2
00000221  8B4604            mov ax,[bp+0x4]
00000224  BA0000            mov dx,0x0
00000227  BB0A00            mov bx,0xa
0000022A  F7F3              div bx
0000022C  8856FF            mov [bp-0x1],dl
0000022F  B40E              mov ah,0xe
00000231  B030              mov al,0x30
00000233  0246FF            add al,[bp-0x1]
00000236  CD10              int 0x10
00000238  5A                pop dx
00000239  5B                pop bx
0000023A  58                pop ax
0000023B  C9                leave
0000023C  C3                ret
0000023D  B400              mov ah,0x0
0000023F  CD16              int 0x16
00000241  32E4              xor ah,ah
00000243  C3                ret
00000244  B81000            mov ax,0x10
00000247  8ED8              mov ds,ax
00000249  8ED0              mov ss,ax
0000024B  8EC0              mov es,ax
0000024D  8EE0              mov fs,ax
0000024F  8EE8              mov gs,ax
00000251  66BD00000900      mov ebp,0x90000
00000257  668BE5            mov esp,ebp
0000025A  66B800800B00      mov eax,0xb8000
00000260  67C60050          mov byte [eax],0x50
00000264  67C640010E        mov byte [eax+0x1],0xe
00000269  FA                cli
0000026A  F4                hlt
0000026B  45                inc bp
0000026C  6E                outsb
0000026D  7465              jz 0x2d4
0000026F  7269              jc 0x2da
00000271  6E                outsb
00000272  67207072          and [eax+0x72],dh
00000276  6F                outsw
00000277  7465              jz 0x2de
00000279  637465            arpl [si+0x65],si
0000027C  64206D6F          and [fs:di+0x6f],ch
00000280  64652E2E2E0000    add [cs:bx+si],al
00000287  0000              add [bx+si],al
00000289  0000              add [bx+si],al
0000028B  0000              add [bx+si],al
0000028D  0000              add [bx+si],al
0000028F  0000              add [bx+si],al
00000291  0000              add [bx+si],al
00000293  0000              add [bx+si],al
00000295  0000              add [bx+si],al
00000297  0000              add [bx+si],al
00000299  0000              add [bx+si],al
0000029B  0000              add [bx+si],al
0000029D  0000              add [bx+si],al
0000029F  0000              add [bx+si],al
000002A1  0000              add [bx+si],al
000002A3  0000              add [bx+si],al
000002A5  0000              add [bx+si],al
000002A7  0000              add [bx+si],al
000002A9  0000              add [bx+si],al
000002AB  0000              add [bx+si],al
000002AD  0000              add [bx+si],al
000002AF  0000              add [bx+si],al
000002B1  0000              add [bx+si],al
000002B3  0000              add [bx+si],al
000002B5  0000              add [bx+si],al
000002B7  0000              add [bx+si],al
000002B9  0000              add [bx+si],al
000002BB  0000              add [bx+si],al
000002BD  0000              add [bx+si],al
000002BF  0000              add [bx+si],al
000002C1  0000              add [bx+si],al
000002C3  0000              add [bx+si],al
000002C5  0000              add [bx+si],al
000002C7  0000              add [bx+si],al
000002C9  0000              add [bx+si],al
000002CB  0000              add [bx+si],al
000002CD  0000              add [bx+si],al
000002CF  0000              add [bx+si],al
000002D1  0000              add [bx+si],al
000002D3  0000              add [bx+si],al
000002D5  0000              add [bx+si],al
000002D7  0000              add [bx+si],al
000002D9  0000              add [bx+si],al
000002DB  0000              add [bx+si],al
000002DD  0000              add [bx+si],al
000002DF  0000              add [bx+si],al
000002E1  0000              add [bx+si],al
000002E3  0000              add [bx+si],al
000002E5  0000              add [bx+si],al
000002E7  0000              add [bx+si],al
000002E9  0000              add [bx+si],al
000002EB  0000              add [bx+si],al
000002ED  0000              add [bx+si],al
000002EF  0000              add [bx+si],al
000002F1  0000              add [bx+si],al
000002F3  0000              add [bx+si],al
000002F5  0000              add [bx+si],al
000002F7  0000              add [bx+si],al
000002F9  0000              add [bx+si],al
000002FB  0000              add [bx+si],al
000002FD  0000              add [bx+si],al
000002FF  0000              add [bx+si],al
00000301  0000              add [bx+si],al
00000303  0000              add [bx+si],al
00000305  0000              add [bx+si],al
00000307  0000              add [bx+si],al
00000309  0000              add [bx+si],al
0000030B  0000              add [bx+si],al
0000030D  0000              add [bx+si],al
0000030F  0000              add [bx+si],al
00000311  0000              add [bx+si],al
00000313  0000              add [bx+si],al
00000315  0000              add [bx+si],al
00000317  0000              add [bx+si],al
00000319  0000              add [bx+si],al
0000031B  0000              add [bx+si],al
0000031D  0000              add [bx+si],al
0000031F  0000              add [bx+si],al
00000321  0000              add [bx+si],al
00000323  0000              add [bx+si],al
00000325  0000              add [bx+si],al
00000327  0000              add [bx+si],al
00000329  0000              add [bx+si],al
0000032B  0000              add [bx+si],al
0000032D  0000              add [bx+si],al
0000032F  0000              add [bx+si],al
00000331  0000              add [bx+si],al
00000333  0000              add [bx+si],al
00000335  0000              add [bx+si],al
00000337  0000              add [bx+si],al
00000339  0000              add [bx+si],al
0000033B  0000              add [bx+si],al
0000033D  0000              add [bx+si],al
0000033F  0000              add [bx+si],al
00000341  0000              add [bx+si],al
00000343  0000              add [bx+si],al
00000345  0000              add [bx+si],al
00000347  0000              add [bx+si],al
00000349  0000              add [bx+si],al
0000034B  0000              add [bx+si],al
0000034D  0000              add [bx+si],al
0000034F  0000              add [bx+si],al
00000351  0000              add [bx+si],al
00000353  0000              add [bx+si],al
00000355  0000              add [bx+si],al
00000357  0000              add [bx+si],al
00000359  0000              add [bx+si],al
0000035B  0000              add [bx+si],al
0000035D  0000              add [bx+si],al
0000035F  0000              add [bx+si],al
00000361  0000              add [bx+si],al
00000363  0000              add [bx+si],al
00000365  0000              add [bx+si],al
00000367  0000              add [bx+si],al
00000369  0000              add [bx+si],al
0000036B  0000              add [bx+si],al
0000036D  0000              add [bx+si],al
0000036F  0000              add [bx+si],al
00000371  0000              add [bx+si],al
00000373  0000              add [bx+si],al
00000375  0000              add [bx+si],al
00000377  0000              add [bx+si],al
00000379  0000              add [bx+si],al
0000037B  0000              add [bx+si],al
0000037D  0000              add [bx+si],al
0000037F  0000              add [bx+si],al
00000381  0000              add [bx+si],al
00000383  0000              add [bx+si],al
00000385  0000              add [bx+si],al
00000387  0000              add [bx+si],al
00000389  0000              add [bx+si],al
0000038B  0000              add [bx+si],al
0000038D  0000              add [bx+si],al
0000038F  0000              add [bx+si],al
00000391  0000              add [bx+si],al
00000393  0000              add [bx+si],al
00000395  0000              add [bx+si],al
00000397  0000              add [bx+si],al
00000399  0000              add [bx+si],al
0000039B  0000              add [bx+si],al
0000039D  0000              add [bx+si],al
0000039F  0000              add [bx+si],al
000003A1  0000              add [bx+si],al
000003A3  0000              add [bx+si],al
000003A5  0000              add [bx+si],al
000003A7  0000              add [bx+si],al
000003A9  0000              add [bx+si],al
000003AB  0000              add [bx+si],al
000003AD  0000              add [bx+si],al
000003AF  0000              add [bx+si],al
000003B1  0000              add [bx+si],al
000003B3  0000              add [bx+si],al
000003B5  0000              add [bx+si],al
000003B7  0000              add [bx+si],al
000003B9  0000              add [bx+si],al
000003BB  0000              add [bx+si],al
000003BD  0000              add [bx+si],al
000003BF  0000              add [bx+si],al
000003C1  0000              add [bx+si],al
000003C3  0000              add [bx+si],al
000003C5  0000              add [bx+si],al
000003C7  0000              add [bx+si],al
000003C9  0000              add [bx+si],al
000003CB  0000              add [bx+si],al
000003CD  0000              add [bx+si],al
000003CF  0000              add [bx+si],al
000003D1  0000              add [bx+si],al
000003D3  0000              add [bx+si],al
000003D5  0000              add [bx+si],al
000003D7  0000              add [bx+si],al
000003D9  0000              add [bx+si],al
000003DB  0000              add [bx+si],al
000003DD  0000              add [bx+si],al
000003DF  0000              add [bx+si],al
000003E1  0000              add [bx+si],al
000003E3  0000              add [bx+si],al
000003E5  0000              add [bx+si],al
000003E7  0000              add [bx+si],al
000003E9  0000              add [bx+si],al
000003EB  0000              add [bx+si],al
000003ED  0000              add [bx+si],al
000003EF  0000              add [bx+si],al
000003F1  0000              add [bx+si],al
000003F3  0000              add [bx+si],al
000003F5  0000              add [bx+si],al
000003F7  0000              add [bx+si],al
000003F9  0000              add [bx+si],al
000003FB  0000              add [bx+si],al
000003FD  0000              add [bx+si],al
000003FF  00                db 0x00

如上所述,我必须手动编码一个跳远,因为MASM不支持它们。反汇编表明它得到了正确的编码(参见00000064(,但它根本不起作用。

如果你好奇,以下是用于构建源代码的命令:

jwasm /nologo /AT /c /Fotmp\boot.obj src\boot\boot.asm
masm_615/bin/link /nologo /TINY /NOD tmp\boot.obj, bin\CompatOS.img, NUL, NUL, NUL

任何帮助都是值得的。

问题是我认为这个option segment:use32将用32位编码以下代码。然而,要做到这一点,你必须将代码划分为多个段,你可以将属性分配给这些段

示例:

_TEXT16 SEGMENT USE16 'CODE'
;16bit code here
_TEXT16 ENDS
_TEXT32 SEGMENT USE32 'CODE'
;32bit code here
_TEXT32 ENDS

我的最终结果是这样的:

.386p
.model TINY, C, NEARSTACK
;------------------------------
; CODE
;------------------------------
_TEXT16 SEGMENT STACK USE16 'CODE'
org 07C00h
Error PROTO lpStr:WORD
Print PROTO lpStr:WORD
ReadDisk PROTO nSectorsRead:BYTE, nCylinder:BYTE, nSector:BYTE, nHead:BYTE, nDrive:BYTE, lpBuffer:WORD
NewLine PROTO
PrintLn PROTO lpStr:WORD
PrintWord PROTO num:WORD
ReadKey PROTO
_start:
xor ax, ax
mov ds, ax
mov ss, ax
mov es, ax
mov bp, 07C00h
mov sp, bp
mov [bootDisk], dl
INVOKE PrintLn, OFFSET bootInfo         ;Onyl works because we are using JWASM as our compiler!
INVOKE PrintLn, OFFSET loadingInfo
INVOKE ReadDisk, 1, 0, 2, 0, [bootDisk], 07E00h
INVOKE Print, OFFSET pageInfo
INVOKE PrintWord, MEM_PAGE_COUNT
INVOKE NewLine
INVOKE PrintLn, OFFSET protectedInfo

lgdt fword ptr [GDT_desc]
mov eax, cr0
or eax, 1
mov cr0, eax
db 66h                                  ;manually encoded far jump = CODE_SEG:start_protected_mode
db 0EAh
dd OFFSET start_protected_mode
dw CODE_SEG
Error PROC lpStr:WORD
INVOKE Print, lpStr
mov ah, 0
int 16h
db 0EAh                         ;manually encoded reboot
dw 0000h
dw 0FFFFh
Error ENDP
Print PROC USES ax si lpStr:WORD
mov si, lpStr
mov ah, 0Eh
@@loop:
lodsb
cmp al, 0
je @@done
int 10h
jmp @@loop
@@done:
ret
Print ENDP
NewLine PROC USES ax
mov ah, 0Eh
mov al, 13
int 10h
mov al, 10
int 10h
ret
NewLine ENDP
PrintLn PROC lpStr:WORD
INVOKE Print, lpStr
INVOKE NewLine
ret
PrintLn ENDP
ReadDisk PROC uses ax cx dx bx nSectorsRead:BYTE, nCylinder:BYTE, nSector:BYTE, nHead:BYTE, nDrive:BYTE, lpBuffer:WORD
mov ah, 02h
mov al, nSectorsRead
mov ch, nCylinder
mov cl, nSector
mov dh, nHead
mov dl, nDrive
mov bx, lpBuffer
int 13h
jnc @@end
INVOKE Error, OFFSET diskReadError
@@end:
ret
ReadDisk ENDP
;------------------------------
; DATA
;------------------------------
bootDisk byte 1
bootInfo db "Booting CompatOS...", 0
loadingInfo db "Loading extra sector...", 0
diskReadError db "Disk read failed!", 0
pageInfo db "Page count: ", 0
GDT_null    dq 0
GDT_code    dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0
GDT_data    dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0
GDT_desc    dw OFFSET GDT_desc - OFFSET GDT_null - 1
GDT_addr    dd OFFSET GDT_null
CODE_SEG equ GDT_code - GDT_null
DATA_SEG equ GDT_data - GDT_null
;------------------------------
; PADDING
;------------------------------
byte 510-($-_start) dup (0)
dw 0AA55h
org 07E00h
MEM_BEG equ 08000h
MEM_END equ 0FFFFh
MEM_PAGE_COUNT equ (MEM_END - MEM_BEG) / 0200h
;------------------------------
; CODE
;------------------------------
PrintWord PROC USES ax bx dx num:WORD
LOCAL digit:BYTE
.if num >= 10
mov ax, num
mov dx, 0
mov bx, 10
div bx
INVOKE PrintWord, ax
.endif
mov ax, num
mov dx, 0
mov bx, 10
div bx
mov digit, dl
mov ah, 0Eh
mov al, '0'
add al, digit
int 10h
ret
PrintWord ENDP
ReadKey PROC
mov ah, 0
int 16h
xor ah, ah
ret
ReadKey ENDP
_TEXT16 ENDS
_TEXT32 SEGMENT USE32 'CODE'
start_protected_mode:
mov ax, DATA_SEG
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ebp, 90000h
mov esp, ebp
mov eax, 0B8000h
mov [eax], 'P'
mov [eax+1], 14
cli
hlt
;------------------------------
; DATA
;------------------------------
protectedInfo db "Entering protected mode...", 0
;------------------------------
; PADDING
;------------------------------
byte 1024-($-start_protected_mode) dup (0)
_TEXT32 ENDS
END _start

相关内容

  • 没有找到相关文章

最新更新