Ch 37 «OÅ@¼Ò¦¡

¦b½Í¤F³\¦h¦b¯u¹ê¼Ò¦¡ ( real mode ) ¤Uªºµ{¦¡³]­p¤§«á¡A¤p¤ì°¸¥´ºâ½Í½Í«OÅ@¼Ò¦¡ ( protected mode )¡C³o¨âºØ¼Ò¦¡ªº®t§O¦b©ó¥i¨Ï¥Îªº°O¾ÐÅé¦h¹è¡C¦b¥Á°ê 70 ¦~ ( ¤]´N¬O¦è¤¸ 1981 ¦~ ) ®É¡A¨Ï¥Î 8088 ¬° CPU ªº IBM PC ­è±À¥X®É¡A¥D¬y¹q¸£¤Wªº°O¾ÐÅé¦h¬° 64KB¡A¦Ó 8088 ¦³ 20 ±ø¦ì§}½u¡A¨C±ø¦ì§}½u¥i¥H¥H°ª¹qÀ£©Î§C¹qÀ£ªí¥Ü 0 ©Î 1¡A¦]¦¹¥u¯àªí¥Ü 220 ­Ó¦ì§}¡A¤]´N¬O»¡ 8088 ¯à¦s¨ú 1MB ¥H¤ºªº°O¾ÐÅé¡C¦b¨º­Ó®É­Ô¡A³o¬O«Ü¤jªº°O¾ÐÅé¡A¦ý¬O«Ü§Ö´N¤£°÷¥Î¤F¡CÄ~°_ªº 80286 ¦³ 24 ±ø¦ì§}½u¡A¦]¦¹³Ì¦h¯à©w§} 224 ­Ó¤£¦P¦ì§}¡A¥ç§Y 16M ­Ó¦ì¤¸²Õ¡C80286 ¦³¨âºØ¤u§@¼Ò¦¡¡G¯u¹ê¼Ò¦¡»P«OÅ@¼Ò¦¡¡C³o¨âºØ¼Ò¦¡©w§}ªº¤è¦¡¤£¦P¡A©Ò¥H¯à¦s¨úªº°O¾ÐÅé¦h¹è¤]¤£¦P¡C¦b¯u¹ê¼Ò¦¡®É¡A80286 ¤´µM¥u¯à¦s¨ú 1MB ¥H¤ºªº°O¾ÐÅé¡A¦ý¬O³t«×¤ñ 8088 §Ö¦h¤F¡A³nÅé¤]¤£¶·­×§ï´N¯àª½±µ°õ¦æ¡A¯à¹F¦¨»P 8088 ¦V¤U¬Û®eªº¥Øªº¡C¦pªG 80286 ¤Á´«¨ì«OÅ@¼Ò¦¡®É¡A´N¯à¦s¨ú³Ì¦h 16MB ªº°O¾ÐÅé¡CÁöµM 80286 ©w§}ªÅ¶¡¬O 8086/8088 ªº 16 ­¿¡A¦ý¬O¤´µM¤£°÷¡C·Q·Q¡A¦p¤µ¤@­º MP3 À£ÁYªººq¦±¡A´N­n¼Æ­Ó MB ¤F¡C¨ì¤F¥Á°ê 74 ¦~ ( ¦è¤¸ 1985 ¦~ )¡A­^¯Sº¸µo°â·s¤@¥Nªº CPU¡A80386¡A§â¦ì§}½uÂX¥R¬° 32 ±ø¡A¥i¥H¦s¨ú 232 ­Ó¦ì¤¸²Õ¡A¥ç§Y 4GB¡A¤j¬ù¨¬°÷À³¥I¤@¯ë»Ý­n¤F¡A80386 ªº«OÅ@¼Ò¦¡¤]»P 80286 ªº«OÅ@¼Ò¦¡¤£¤Ó¬Û¦P¡A¥i¥H©w§}¨ì 4GB¡C¥H«á­^¯Sº¸©Òµo°âªº CPU¡A¤@ª½¨ì Pentium 4 ªº³¡¥÷«¬¸¹¬°¤î¡A¨ä©w§}¤è¦¡³£»P 80386 ¬Û¦P¡A³o¨Ç CPU ¬[ºc¬Û¦ü¡A³QºÙ¬°¡u80x86¡v¡B¡ux86¡v©Î¡uIA32¡v( ¤£¹L IA32 ªº©R¦W·|¥X²{³Â·Ð¡A¦]¬°¥¦¤]¥]¬A 16 ¦ì¤¸ªº CPU )¡CµwÅé¡A½Ñ¦p CPU ¤w¶i¤J 32 ¦ì¤¸«OÅ@¼Ò¦¡®É¥N¡A¦Ó§@·~¨t²Î¶i¤J«OÅ@¼Ò¦¡¡A¤´¦³¤@¬q«Üªøªº¸ô¡C

¦b 80286 ¤w¸g´¶¤Îªº®É­Ô ( ¥Á°ê 72 ¦~¥ª¥k )¡A±`¥Îªº§@·~¨t²Î¬O MS¡ÐDOS 3.x¡AMS¡ÐDOS 3.x ¬O¦b¯u¹ê¼Ò¦¡¤U°õ¦æªº¨t²Î¡A¨º®É­Ôªº°O¾ÐÅé¤]«Ü¶Q¡A¤j¬ù¨C MB ­n»ù¤@¸U¥x¹ô¡A¦A¥[¤W¨º®É§@·~¨t²Î¨Ã«D¹Ï§Î¬É­±¡A¦Ó¬O¤å¦r¾É¦V¡A¦]¦¹¹ï°O¾ÐÅé»Ý¨D¤£¤j¡A©Ò¥H¦ü¥G¤£Ä±±o¨Ï¥Î¶W¹L 1MB ªº°O¾ÐÅ馳­¢¤Áªº»Ý­n¡C³oºØ±¡§Î¤@ª½¨ì¦è¤¸ 1990 ¦~¥Nªì´Á¡AWindows µ¥¹Ï§Î¬É­±ªº§@·~¨t²Îµo®i«á¡A¤~³vº¥§ïÆ[¡C¨ì¤F¦è¤¸ 1995 ¦~¡A·L³nµo°â Windows 95 ¤§«á¡AWindows ¤~¯u¥¿¶i¤J«OÅ@¼Ò¦¡¡C

­^¯Sº¸©Ò¥X«~ªº CPU ¥u¦³¦b 80286 ©Î§ó°ªµ¥¯Åªº CPU ¤~¦³«OÅ@¼Ò¦¡¡A¦ý¬O 80286 »P 80386 ªº«OÅ@¼Ò¦¡¤£¤Ó¬Û¦P¡A80286 «OÅ@¼Ò¦¡ªº°¾²¾¦ì§}¤´¨Ï¥Î 16 ¦ì¤¸¡A¦]¦¹¨C­Ó°Ï¬q¤´µM¦³ 64KB ªº­­¨î¡A³o·|¾É­P³B²z¤j¶q¸ê®Æ®Éªº½ÆÂø©Ê¡A¦Ó 80386 «OÅ@¼Ò¦¡ªº°¾²¾¦ì§}±Ä¥Î 32 ¦ì¤¸¡A¦]¦¹´î§C¤F³o­Ó¯ÊÂI¡C¦A¥[¤W²{¤µ¤j·§¨S¤H¤´µM¨Ï¥Î 80286 ¤F¡A©Ò¥H©³¤U¤¶²Ðªº¬O¥H 80386 «OÅ@¼Ò¦¡¬°¥D¡C¦b 80386 ¥H«áªº CPU¡A¦p 80486¡BPentium ª½¨ì²{¦b ( ¥Á°ê 102 ¦~¡A¦è¤¸ 2013 ¦~ )¡A«OÅ@¼Ò¦¡¤´µM¦s¦b¡A¥u¬O²{¦bªº§@·~¨t²Î¡A±q Windows 9x ¨ì Windows 7¡A³£¬O¤@¶}¾÷¡A´N¤Á´«¨ì«OÅ@¼Ò¦¡¡AÅý¨Ï¥ÎªÌ¹îı¤£¥X¨Ó¡C

¥Ñ¯u¹ê¼Ò¦¡¶i¤J«OÅ@¼Ò¦¡«á¡A°£¤F¥i¥H¨Ï¥Î¶W¹L 1MB °O¾ÐÅ餧¥~¡A«OÅ@¼Ò¦¡ÁÙ¹ï°O¾ÐÅé°µ¨ì¡u«OÅ@¡vªº³d¥ô¡C·N«ä¬O¦b«OÅ@¼Ò¦¡¤U¡Aµ{¦¡µLªk¥ô·N¦s¨ú¨ä¥L°Ï¬qªº¸ê®Æ¡A©Ò¥H¦U­Óµ{¦¡¤§¶¡¤£·|¤¬¬Û¤zÂZ©Î¯}Ãa¡C¦pªG¦³¤@­Ó³]­p¤£¦nªºµ{¦¡¶i¤JµL½a°j°é¡A¨Ï¥ÎªÌ¥u»Ý§â³o­Óµ{¦¡Ãö³¬§Y¥i¡A¤£·|¼vÅT¨ä¥Lµ{¦¡¡A¤]¤£»Ý­n¦]³o­Óµ{¦¡·í±¼¦Ó­«·s±Ò°Ê¹q¸£¡C


¶i¤J«OÅ@¼Ò¦¡ªº­ì©l½X¡APM1.ASM

¤p¤ì°¸±N¦b¤U­±´X³¹ùØ¡A¤¶²Ð«OÅ@¼Ò¦¡¡C¤p¤ì°¸¥ý¹ê§@¤@­Ó¯u¹ê¼Ò¦¡¤Á´«¨ì«OÅ@¼Ò¦¡ªºµ{¦¡¬°¨Ò¤l¡C©³¤Uªºµ{¦¡½X¬O¥Ñ¯u¹ê¼Ò¦¡¤Á´«¨ì«OÅ@¼Ò¦¡¡G

                .386P           ;001

desc            STRUC
limit_l         DW      0       ;°Ï¬qÃä¬É(BIT0-15)
base_l          DW      0       ;°Ï¬q¦ì§}(BIT0-15)
base_m          DB      0       ;°Ï¬q¦ì§}(BIT16-23)
attributes      DB      0       ;°Ï¬qÄÝ©Ê
limit_h         DB      0       ;°Ï¬qÃä¬É(BIT16-19)(§t°Ï¬qÄݩʪº°ª4¦ì)
base_h          DB      0       ;°Ï¬q¦ì§}(BIT24-31)
desc            ENDS    0       ;10

pdesc           STRUC
limit           DW      0       ;¥þ°ì´y­z¾¹ªí®æ¤j¤p
base            DD      0       ;32¦ì¤¸°ò©³¦ì§}
pdesc           ENDS

jmp2pm          MACRO   s,o
                DB      66h,0eah;¾Þ§@½X
                DW      o,0     ;32¦ì¤¸°¾²¾¦ì§}
                DW      s       ;°Ï¬q­È©Î°Ï¬q¿ï¾Ü¤l
                ENDM            ;021

;*******************************************************************************
code    SEGMENT USE16
        ASSUME  cs:code,ds:code
        ORG     100h
;-------------------------------------------------------------------------------
start:  jmp     SHORT begin
ALIGN           8
gdt             LABEL   BYTE        ;030                ;¥þ°ì´y­z¾¹ªí®æ
dummy           desc    <0,0,0,0,0,0>                   ;ªÅ´y­z¾¹
pm_code         desc    <pm_c_len-1,0,0,98h,0,0>        ;µ{¦¡½X°Ï¬q´y­z¾¹
pm_vedio        desc    <0ffffh,8000h,0bh,92h,0,0>      ;Åã¥Ü°O¾ÐÅé°Ï¬q´y­z¾¹
pm_data         desc    <pm_d_len-1,0,0,92h,0,0>        ;¸ê®Æ°Ï¬q´y­z¾¹
gdt_len         =       $-gdt                           ;¥þ°ì´y­z¾¹ªí®æªº¤j¤p
gdt_ptr         pdesc   <gdt_len-1,0>                   ;¥þ°ì´y­z¾¹ªí®æ¸ê®Æ
code_selector   =       pm_code-gdt                     ;µ{¦¡½X°Ï¬q¿ï¾Ü¾¹
video_selector  =       pm_vedio-gdt                    ;Åã¥Ü°O¾ÐÅé°Ï¬q¿ï¾Ü¾¹
data_selector   =       pm_data-gdt                     ;¸ê®Æ°Ï¬q¿ï¾Ü¾¹
;------------------------------------040----------------------------------------
begin:  sub     eax,eax             ;041
;³]¸mµ{¦¡½X°Ï¬q´y­z¾¹
        mov     ebx,eax
        mov     ax,cs
        mov     bx,OFFSET pm
        shl     eax,4
        add     eax,ebx             ;µ{¦¡½X°Ï¬q°¾²¾¦ì§}¬° 0
        mov     pm_code.base_l,ax
        shr     eax,10h
        mov     pm_code.base_m,al   ;050
        mov     pm_code.base_h,ah

;³]¸m¸ê®Æ°Ï¬q´y­z¾¹
        sub     eax,eax
        mov     ebx,eax
        mov     ax,cs
        mov     bx,OFFSET pm_data_seg
        shl     eax,4
        add     eax,ebx
        mov     pm_data.base_l,ax   ;060
        shr     eax,10h
        mov     pm_data.base_m,al
        mov     pm_data.base_h,ah

;¶ñ¤J¥¿½Tªº¼Æ­È¨ì gdt_ptr ùØ
        sub     eax,eax
        mov     ebx,eax
        mov     ax,cs
        mov     bx,OFFSET gdt
        shl     eax,4               ;070
        add     eax,ebx             ;­pºâ GDT ¦ì§}¡A¦s©óEAX
        mov     gdt_ptr.base,eax    ;EAX¡×CS¡Ñ4¡Ï(OFFSET gdt)
;¸ü¤J GDT
        lgdt    QWORD PTR gdt_ptr   ;­Y¥Î MASM 6.x ²ÕĶ¡A§ï¥Î¡ulgdt gdt_ptr¡v

;Ãö¤¤Â_
        cli

;¶}±Ò A20 ¦a§}½u
        in      al,92h              ;080
        or      al,00000010b
        out     92h,al

;·Ç³Æ¤Á´«¨ì«OÅ@¼Ò¦¡
        mov     eax,cr0
        or      eax,1
        mov     cr0,eax

;¯u¥¿¶i¤J«OÅ@¼Ò¦¡
        jmp2pm  code_selector,0     ;090

;¥H¤U¨ì pm_len ¬O¦b«OÅ@¼Ò¦¡¤¤°õ¦æªºµ{¦¡½X°Ï¬q
pm:     mov     cx,video_selector
        mov     es,cx
        mov     cx,data_selector
        mov     ds,cx
        mov     esi,OFFSET string-OFFSET pm_data_seg
        mov     edi,(80*10+0)*2     ;¿Ã¹õ²Ä 10 ¦C¡A²Ä 0 ¦æ¡C
        mov     ah,0eh              ;¶Â©³¶À¦r
next:   lodsb                       ;100
        cmp     al,0
        jz      exit
        stosw
        jmp     next

exit:   jmp     $                   ;¨ì¦¹°±¤î
pm_c_len        EQU     $-pm
;«OÅ@¼Ò¦¡¤¤ªºµ{¦¡½X°Ï¬qµ²§ô
;¥H¤U¨ì pm_d_len ¬O¦b«OÅ@¼Ò¦¡¤¤ªº¸ê®Æ°Ï¬q
pm_data_seg:                        ;110
string          DB      'In protected mode.',0
pm_d_len        EQU     $-OFFSET pm_data_seg
;«OÅ@¼Ò¦¡¤¤ªº¸ê®Æ°Ï¬qµ²§ô
;-------------------------------------------------------------------------------
code            ENDS
;*******************************************************************************
END     start

§Q¥Î¤å®Ñ³B²z³nÅé¡A¿é¤J¤W­z­ì©l½X«á¡A¦s¦¨ PM1.ASM ÀÉ¡AµM«á¶}±Ò Virtual PC ( °Ñ¦Òªþ¿ý¤Q¤@ )¡A¨Ì·Ó¤U­±ºñ¦â®Ø®Øªº«ü¥O²ÕĶ¡B³sµ²¡A§O§Ñ¤F¨C¦æ¿é¤J«ü¥O«á­n«ö¡uEnter¡vÁä¡G

²ÕĶ¡B³sµ²¦¨¥\«á¡A¿é¤Jµµ¦â®Ø®Øªº¡upm1¡v¡A°õ¦æ PM1.COM «á¡A·|¬Ý¨£¦b¿Ã¹õ²Ä 10 ¦C¡A¦L¥X¤@¦æ¶À¦âªº¦r¦ê¡uIn protect mode.¡v( ¬õ¦â®Ø®Ø¤º ) «á¡A´N·í¾÷¤F¡A³o¬O¦]¬°¦b²Ä 106 ¦æªºµ{¦¡½X¸õ¶i¤FµL½a°j°é¤¤¡A¬G¦Ó·í¾÷¡F¤£¹L§Ú­Ìªºµ{¦¡½T¹ê¬O¶i¤J¤F«OÅ@¼Ò¦¡¡C©³¤U§Ú­Ì¨Ó¬Ý¬Ý PM1.ASM ªº­ì©l½X¡C


¶i¤J«OÅ@¼Ò¦¡¡G¸Ñ»¡ PM1.ASM

°Ï¬q´y­z¾¹ ( Segment Descriptor ) »P°Ï¬q´y­z¾¹ªí®æ ( Segment Descriptor Table )

PM1.ASM ªº²Ä 3 ¦æ¨ì²Ä 21 ¦æ©w¸q¤F¨â­Óµ²ºcÅé©M¤@­Ó¥¨¶°¡A¨ä¤¤¤@­Óµ²ºcÅéºÙ¬° desc¡A¥¦¬O¥Ñ 8 ­Ó¦ì¤¸²Õ©Ò²Õ¦¨ªº¡A¥¦¤]¬O«OÅ@¼Ò¦¡ªº¥D¨¤¡A³o­Óµ²ºcÅ馳­Ó¯S§Oªº¦W¦r¡AºÙ¬°¡u°Ï¬q´y­z¾¹¡v( segment descriptor¡A©Î²ºÙ´y­z¾¹¡A¤j³°ºÙ¬°´y­z²Å )¡C²Ä 30 ¦æ¨ì 35 ¦æ¡A¨Ì¾Ú°Ï¬q´y­z¾¹¡Adesc¡Aµ²ºcÅé©w¸q¤F¥|µ§¸ê®Æ¡A³o­Ó¥|µ§¸ê®Æ¦n¤ñ¬O¤@±iªí®æ¡A±z¤]¥i¥H·Q¦¨¬O¤@­Ó°}¦C¡A³o±iªí®æºÙ¬°¡u°Ï¬q´y­z¾¹ªí®æ¡v( segment descriptor table¡A©Î²ºÙ´y­z¾¹ªí®æ¡A¤j³°ºÙ¬°´y­z²Åªí®æ )¡C¨ì¦¹¬°¤î¡A§Ú­Ì¥i¥HÁA¸Ñ¡A°Ï¬q´y­zªí®æ¬O¥Ñ¤@­Ó¤@­Óªº°Ï¬q´y­z¾¹©Òºc¦¨¡A¨C­Ó°Ï¬q´y­z¾¹¤S¥Ñ 8 ­Ó¦ì¤¸²Õ©Ò²Õ¦¨¡C

¡u°Ï¬q´y­z¾¹¡v¡AÅU¦W«ä¸q¡A´N¬O¥Î¨Ó´y­z«OÅ@¼Ò¦¡¤§¤Uªº°Ï¬q¡A¤£¹L¡A¦b«OÅ@¼Ò¦¡¤U©M¯u¹ê¼Ò¦¡¤U¡A°Ï¬qªº·N¸q¬O¤£¦Pªº¡AÁöµM¨âªÌ³£ºÙ¬°¡usegment¡v( ¤¤¤å¤]³£ºÙ¬°¡u°Ï¬q¡v¡A¤]¦³¤HºÙ¬°¡u¸`°Ï¡v©Î¡u¸`¡v)¡C¦b¯u¹ê¼Ò¦¡¤U¡A¨C­Ó°Ï¬q³Ì¤j¥u¯à¦³ 64K ¦ì¤¸²Õ¡F¦Óµ{¦¡¥i¥HŪ¨ú¥ô·N°Ï¬qªº¸ê®Æ¡A¤]¯à§â¥ô·N¸ê®Æ¼g¤J¥ô·N°Ï¬q¤¤¡F¦¹¥~¡A¦ì§}ªºªí¥Ü¤è¦¡¬O¥H¡u°Ï¬q:°¾²¾¦ì§}¡vªí¥Ü¡A¦pªG±z·QÀò±o¹êÅé¦ì§} ( physical address )¡A¥u»Ý¨Ì·Ó¤U­±ªº¤½¦¡¡G

¹êÅé¦ì§}¡×°Ï¬q¡Ñ16¡Ï°¾²¾¦ì§}

´N¥i¥H±o¨ì¡C( ¹ï 32 ¦ì¤¸ªº CPU ¦Ó¨¥¡A¹q¸£¥D¾÷ªO¤W©Ò²Õ¦¨°O¾ÐÅé´¹¤ùùØ¡A¦³³\³\¦h¦hªº°O¾ÐÅéªÅ¶¡¡A¨C­ÓªÅ¶¡³Qµe¤À¬°¤@­Ó¦ì¤¸²Õªº¤j¤p¡A¥Ñ 0 ¶}©l½s¸¹¤@ª½¨ì 4G ¬°¤î¡A¨C­Ó¼Ð¸¹¥Nªí¤@­Ó¦ì¤¸²Õ¡A³o­Ó½s¸¹´N¥s¹êÅé¦ì§}©Îµ´¹ï¦ì§} )

¦b«OÅ@¼Ò¦¡¤U¡A¨C­Ó°Ï¬q³Ì¤j¥i¥H¦³ 4G ¦ì¤¸²Õ¡A·íµM¤]¥i¥H¤p©ó 4G ¦ì¤¸²Õ¡A¨Ã¥Bµ{¦¡µLªk¥ô·NŪ¨ú©Î¼g¤J¨ä¥L°Ï¬q¡A¬°¤F¹F¨ì³o¨Ç­n¨D¡AÅãµM¥²¶·­n¦³¤@¶ô°O¾ÐÅé°O¸üµÛ¨C­Ó°Ï¬qªº¸ê®Æ¡A³o¨Ç¸ê®Æ¥]§t°Ï¬q°_©l¦ì§}¡B°Ï¬q¤j¤p¡B¥i¥HŪ¨ú©Î¼g¤JªºÄݩʵ¥µ¥¡A³o¼Ëªº¤@¶ô°O¾ÐÅé´N¬O«e­±©Ò»¡ªº¡u°Ï¬q´y­z¾¹¡v¡A§Ú­Ì¤S§â©Ò¦³ªº°Ï¬q´y­z¾¹³£¶°¤¤°_¨Ó¡A§Î¦¨¤@­Óªí®æ¡A´N¬O¡u°Ï¬q´y­z¾¹ªí®æ¡v¡C§Ú­Ì¥ý¨Ó¬Ý¬Ý¡u°Ï¬q´y­z¾¹¡v¡C¡u°Ï¬q´y­z¾¹¡v¬O¥Ñ³sÄòªº 8 ­Ó¦ì¤¸²Õ ( ¥ç§Y 64 ¦ì¤¸ ) ²Õ¦¨ªº°O¾ÐÅé¡A¨ä¤º®e¦p¤U¡G

¤W¹Ï©Ò¼Ð¥Üªº 0¡ã15 ¦ì¤¸©M 48¡ã51 ¦ì¤¸¡A¦@ 20 ­Ó¦ì¤¸ ( ²HÂŦⳡ¥÷ ) ªí©ú¤F°Ï¬qªºÃä¬É ( limit )¡C¦]¬°¥ô¦ó°Ï¬qªº¶}©l¦ì§}³£¬O¥Ñ½s¸¹ 0 ªº¦ì§}¶}©l¡A¨C­Ó¦ì¤¸²Õ¨Ï¥Î¤@­Ó½s¸¹¡AÃä¬Éªº·N«ä´N¬Oªí¥Ü³o­Ó°Ï¬q³Ì¦h¥i¥H¨Ï¥Î¨ì½s¸¹²Ä´X­Ó¡A·N§Y¨ì³o­Ó°Ï¬qªº¡uÃä¬É¡v¤F¡C¨Ò¦p¡A¦³¤@­Ó°Ï¬qªº°Ï¬q´y­z¾¹ªºÃä¬É¬° 100¡A¨ºªí¥Ü³o­Ó°Ï¬q¥Ñ¦ì§} 0 ¶}©l¨ì 100¡A¦pªG§â¤@­Ó¦ì¤¸²Õªº¼Æ­È©ñ¨ì½s¸¹ 100 ªº¦ì§}¡A´N¨ì¹FÃä¬É¤F¡A¦ý¤£¯à©ñ¨ì½s¸¹ 101 ªº¦ì§}¡A¬G³o­Ó°Ï¬q¤j¤p¬° 101 ­Ó¦ì¤¸²Õ¡C¤]´N¬O»¡¡AÃä¬É»P°Ï¬q¤j¤p¬Û®t¤@­Ó¦ì¤¸²Õ¡A¦Ó­è­è©Ò»¡±o¦ì§}½s¸¹¡A´N¥s°µ¡u°¾²¾¦ì§}¡v¡C

¦Ó²Ä 16¡ã39¡B56¡ã63 ¦ì¤¸¡A¦@ 32 ¦ì¤¸ ( ºñ¦â³¡¥÷ ) ªí©ú¤F°Ï¬qªº°ò©³¦ì§} ( base )¡A°ò©³¦ì§}´N¬O°Ï¬q¥Ñ¹êÅé¦ì§}ªº­þ¤@­Ó¦a¤è¶}©l¡C¨Ò¦p¡A¦pªG°ò©³¦ì§}¬O 30000H¡A¨º»òªí¥Ü³o­Ó°Ï¬q¬O¥Ñ¹êÅé¦ì§} 30000H ¶}©l¡A¦Ó°Ï¬qªºÃä¬É´N¬O°Ï¬q¤j¤p´î¤@¡C±z¥i¯à·|ı±o©_©Ç¡A¬°¤°»ò¤£§â°Ï¬qªº°ò©³¦ì§}Â\¦b¤@¶ô¡A¦Ó­n¤À¶}©O¡H³o¨ä¹ê¬O¬°¤F»P 286 «OÅ@¼Ò¦¡¬Û®e©Ò±Ä¨úªº¤£±o¤w±¹¬I¡A¦]¦¹³y¦¨µ{¦¡ªº½ÆÂø©Ê¡A³½»Pºµ´x¤£¥i­Ý±o¡A³o¤]¬O¨S¿ìªkªº¨Æ¡C²Ä 40¡ã47 ¦ì¤¸¡B²Ä 52¡ã55 ¦ì¤¸¡A¦@ 12 ­Ó¦ì¤¸¡A«h¬Oªí©ú¦¹°Ï¬qªºÄÝ©Ê¡A°Ï¬qªºÄݩʸû¬°½ÆÂø¡Aµy«á¦A²Ó»¡¡C²{¦b§Ú­Ì¥ý¨Ó¬Ý¬Ý°Ï¬q´y­z¾¹ªº°ò©³¦ì§}¤Î¤j¤p¦p¦ó¨Ï¥Î¡C

¦b«OÅ@¼Ò¦¡¤¤¡A¹³ CS¡BDS¡BES µ¥°Ï¬q¼È¦s¾¹¬O¥Î¨Ó«ü¦V¡u°Ï¬q´y­z¾¹ªí®æ¡v¤¤ªº¬Y­Ó¡u°Ï¬q´y­z¾¹¡v¡A©Ò¥H¤]¥i¥H»¡°Ï¬q¼È¦s¾¹¬O¬Y­Ó¡u°Ï¬q´y­z¾¹¡vªº«ü¼Ð©Î¯Á¤Þ¡C¨Ò¦p¡A¦b«OÅ@¼Ò¦¡¤¤¡ACS ¼È¦s¾¹´NÀ³¸Ó«ü¦Vµ{¦¡½X°Ï¬qªº¡u°Ï¬q´y­z¾¹¡v¡A¦Ó³o­Óµ{¦¡½X°Ï¬qªº¡u°Ï¬q´y­z¾¹¡v¤º°ò©³¦ì§}´N¬Oµ{¦¡½X°Ï¬qªº°_©l¦ì§}¡A¦Ó¦¹¡u°Ï¬q´y­z¾¹¡v¤ºªºÃä¬É´N¬Oµ{¦¡½X°Ï¬qªº¤j¤p´î¤@¡C¥Ñ PM1.ASM ªº²Ä 31¡ã34 ¦æ¬Ý¨Ó¡A±oª¾ PM1 ¶i¤J«OÅ@¼Ò¦¡«á¦³¥|­Ó°Ï¬q´y­z¾¹¡Adummy¡Bpm_code¡Bpm_vedio¡Bpm_data¡A¤À§O¬OªÅªº°Ï¬q´y­z¾¹¡Bµ{¦¡½X°Ï¬q´y­z¾¹¡BÅã¥Ü°O¾ÐÅé°Ï¬q´y­z¾¹¡B¸ê®Æ°Ï¬q´y­z¾¹¡C³o¥|­Ó°Ï¬q´y­z¾¹¶°¤¤°_¨Ó¡A´N§Î¦¨¤@­Ó¡u°Ï¬q´y­z¾¹ªí®æ¡v¡C¦b¶i¤J«OÅ@¼Ò¦¡¤§«e¡A­n¥ý§â³o¥|­Ó¡u°Ï¬q´y­z¾¹¡vªº©Ò¦³¸ê®Æ³£³]¦n¡CªÅªº°Ï¬q¤ñ¸û¯S§O¡A¥H«á¦A»¡¡A¤Ï¥¿¤j³¡¤À±¡§Î¤U¡A¡u°Ï¬q´y­z¾¹ªí®æ¡vùتº²Ä¤@­Ó¡u°Ï¬q´y­z¾¹¡v¬°ªÅªº°Ï¬q´y­z¾¹¡CPM1.ASM ùتº²Ä¤G­Ó°Ï¬q´y­z¾¹¬O pm_code¡A¬Ý¥¦ªº¦W¦r¡A´Nª¾¹D¥¦¬O«OÅ@¼Ò¦¡¤Uµ{¦¡½X°Ï¬qªº¡u°Ï¬q´y­z¾¹¡v¡CPM1 ¦b«OÅ@¼Ò¦¡°õ¦æ®É¡A¥u§â¡uIn protect mode.¡v¦r¦ê¦L¦b¿Ã¹õ¤W¡A¨ä­ì©l½X¦ì©ó²Ä 92¡ã108 ¦æ¡A¦p¤U­±©Ò¥Ü¡G

;¥H¤U¨ì pm_len ¬O¦b«OÅ@¼Ò¦¡¤¤°õ¦æªºµ{¦¡½X°Ï¬q
pm:     mov     cx,video_selector
        mov     es,cx
        mov     cx,data_selector
        mov     ds,cx
        mov     esi,OFFSET string-OFFSET pm_data_seg
        mov     edi,(80*10+0)*2     ;¿Ã¹õ²Ä 10 ¦C¡A²Ä 0 ¦æ¡C
        mov     ah,0eh              ;¶Â©³¶À¦r
next:   lodsb                       ;100
        cmp     al,0
        jz      exit
        stosw
        jmp     next

exit:   jmp     $                   ;¨ì¦¹°±¤î
pm_c_len        EQU     $-pm
;«OÅ@¼Ò¦¡¤¤ªºµ{¦¡½X°Ï¬qµ²§ô

ÁöµMµ{¦¡½X°Ï¬q¶Èµuµu¤Q´X¦æ¡A©Ò¦û¦ì¤¸²Õ¤]¤£¤j¡A¦ý¬O¤´¬O¤@­Ó°Ï¬q¡A­n¬°¥¦·Ç³Æ¤@­Ó°Ï¬q´y­z¾¹¡C©Ò¥H¦b pm_code ´y­z¾¹¤ºªº°ò©³¦ì§}»Ý¶ñ¤J pm: ¼Ð¸¹ªº¹êÅé¦ì§}¡A¨ä¹êÅé¦ì§}¥i¥H¥Ñ¡u°Ï¬q:°¾²¾¦ì§}¡v±o¨ì¡C¦]¬°¦b¶i¤J«OÅ@¼Ò¦¡¤§«e¡ACPU ¤´³B©ó¯u¹ê¼Ò¦¡¡A©Ò¥H¹êÅé¦ì§}´N¬O¡u°Ï¬q¡Ñ16¡Ï°¾²¾¦ì§}¡v¡A­ì©l½Xªº²Ä 41¡ã51 ¦æ ( ¤U­±ªºµ{¦¡½X ) ´N¬O­pºâ¹êÅé¦ì§}¡A¤Î§â¹êÅé¦ì§}¶ñ¤Jµ{¦¡½X°Ï¬q´y­z¾¹ªº¹Lµ{¡G

begin:  sub     eax,eax             ;041
;³]¸mµ{¦¡½X°Ï¬q´y­z¾¹
        mov     ebx,eax
        mov     ax,cs
        mov     bx,OFFSET pm
        shl     eax,4
        add     eax,ebx             ;µ{¦¡½X°Ï¬q°¾²¾¦ì§}¬° 0
        mov     pm_code.base_l,ax
        shr     eax,10h
        mov     pm_code.base_m,al   ;050
        mov     pm_code.base_h,ah

¦Ü©óµ{¦¡½X°Ï¬qªºªø«×«h¦b²Ä 107 ¦æ¡A¥Ñ²ÕĶ¾¹­pºâ¥X¨Ó¡A¨Ã©ó²Ä 32 ¦æª½±µ¶ñ¤J¡G

pm_code         desc    <pm_c_len-1,0,0,98h,0,0>        ;µ{¦¡½X°Ï¬q´y­z¾¹

²Ä¤T­Ó°Ï¬q´y­z¾¹¬O¥Î¨Ó«ü¦VÅã¥Ü°O¾ÐÅ骺°Ï¬q¡A³o¬O¬°¤F¦b¶i¤J«OÅ@¼Ò¦¡«á¡Aª½±µ§â¦r¦ê¶ñ¤JÅã¥Ü°O¾ÐÅ餺¡A´N¥i¥H§â¦r¦êÅã¥Ü¦b¿Ã¹õ¤W¤F¡CÅã¥Ü°O¾ÐÅé¬O¦b 0B8000H ªº¦a¤è¡A¤j¤p¬O 64K ¦ì¤¸²Õ¡A¦]¦¹§Ú­Ì¦bµ{¦¡½X²Ä 33 ¦æª½±µ©w¸q¡G

pm_vedio        desc    <0ffffh,8000h,0bh,92h,0,0>      ;Åã¥Ü°O¾ÐÅé°Ï¬q´y­z¾¹

²Ä¥|­Ó°Ï¬q´y­z¾¹¬O¥Î¨Ó«ü¦V¸ê®Æ°Ï¬qªº¡A¨ä¹êÅé¦ì§}ªº­pºâ¤è¦¡©Mµ{¦¡½X°Ï¬q­pºâ¤è¦¡¬Û¦P¡C¤p¤ì°¸´N¤£¦AÂØ­z¡A½Ð¦Û¦æ°Ñ¦Ò­ì©l½X¡C

¥þ°ì´y­z¾¹ªí®æ ( GDT )

¶i¤J«OÅ@¼Ò¦¡«á¡A§Ú­Ì§â DS¡BES ¼È¦s¾¹¤À§O«ü¦V pm_data¡Bpm_vedio¡ACPU ´N¥i¥H§ä¨ì¬Û¹ïÀ³ªº°Ï¬q´y­z¾¹¡C¦ý¬O­è¤~Á¿¹L¡ADS¡BES µ¥°Ï¬q¼È¦s¾¹¬O¤@­Ó¯Á¤Þ­È¡A¬O¬Û¹ï©ó°Ï¬q´y­z¾¹ªí®æªº¯Á¤Þ¡C¤]´N¬O»¡¡ADS¡BES µ¥°Ï¬q¼È¦s¾¹¥²¶·¥H¬Y­Ó¦ì§}¬°°ò·Ç¡A¦Ó³o­Ó¦ì§}´N¬O°Ï¬q´y­z¾¹ªí®æªº¦ì§}¡C¨º»ò CPU ¤S¬O¦p¦ó±oª¾°Ï¬q´y­z¾¹ªí®æªº¹êÅé¦ì§}©O¡Hµª®×´N¦b 80386 CPU ·s¼Wªº¤@­Ó¼È¦s¾¹¡AºÙ¬° GDTR ( global descriptor table register¡A¥þ°ì´y­z¾¹ªí®æ¼È¦s¾¹ )¡C¨Æ¹ê¤W°Ï¬q´y­z¾¹ªí®æ¤À¬°¦n´XºØ¡A¥þ°ì´y­z¾¹ªí®æ¥u¬O¨ä¤¤ªº¤@ºØ¡A¨ä¥LÁÙ¦³¦n´XºØ¡A¦ý¬O¥H«á¦A²Ó»¡§a¡CGDTR ±Mªù¥Î¨Ó°O¿ý¥þ°ì´y­z¾¹ªí®æ©Ò¦b¦ì§}¡A¸ü¤J¦¹¦ì§}¨ì GDTR ¼È¦s¾¹ªº¤èªk¬O¥Î·sªº 80386 «ü¥O¡ALGDT¡C

LGDT «ü¥O

LGDT «ü¥O¬O 80286 ¥H¤W¤~¦³ªº«ü¥O¡A¨ä¥\¯à¬O§â°O¾ÐÅ餺ªº¥þ°ì´y­z¾¹ªí®æªº¸ê®Æ¸ü¤J¨ì GDTR ¼È¦s¾¹ùØ¡C¥þ°ì´y­z¾¹ªí®æªº¸ê®Æ¬O¤@­Ó 6 ¦ì¤¸²Õªøªºµ²ºcÅé¡APM1.ASM ©R¦W¬° pdesc¡A·íµM±z¥i¥H¨Ì³ß·R¦Û¦æ©R¦W¡A¨ÃµLµw©Ê³W©w¡C¦¹µ²ºcÅ馳¨â­ÓÄæ¦ì¡G²Ä¤@­ÓÄæ¦ìªø¤@­Ó¦r²Õ¡A¬O¥þ°ì´y­z¾¹ªí®æªº¤j¤p¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡F²Ä¤G­ÓÄæ¦ìªø¤@­ÓÂù¦r²Õ¡A¬O¥þ°ì´y­z¾¹ªí®æªº¦ì§}¡C¦p¤U¡G

pdesc           STRUC
limit           DW      0       ;¥þ°ì´y­z¾¹ªí®æ¤j¤p
base            DD      0       ;32¦ì¤¸°ò©³¦ì§}
pdesc           ENDS

LGDT »yªk¬°

LGDT    ¡updesc ¦ì§}¡v

°²¦p±z¥Î MASM 5.x ²ÕĶ¡A¥²¶·¨Ï¥Î¡uLGDT QWORD PTR pdesc¦ì§}¡v¡A§_«h·|µo¥Íĵ§i¡A¦ý¬O¨ä¹ê¸ü¤J¨ì LGDT ªº¦ì¤¸²ÕÀ³¬° 6 ­Ó¦ì¤¸²Õ¡A³o¬O MASM 5.x ªº¿ù»~¡A¤£¹L¤£¼vÅTµ{¦¡¹ê»Ú¹B§@¡A°Ñ¦Ò·L³nªººô­¶¡CMASM 6.x ¤w­×¥¿³o­Ó¿ù»~¡A¤£»Ý­n«ü©w¦ì¤¸²Õªø«×¡C¦b¥H LGDT «ü¥O¸ü¤J pdesc ¤ºªº¸ê®Æ¨ì GDTR ¼È¦s¾¹«e¡A¤@¼Ë­n¥ý§â¥¿½Tªº¸ê®Æ¶ñ¤J pdesc µ²ºcÅé¡A¨äµ{¦¡½X¦b²Ä 65¡ã74 ¦æ¡A½Ð¦Û¦æ°Ñ¦Ò¡C

¶}±Ò A20 ¦ì§}½u

ÁöµM­n¶i¤J«OÅ@¼Ò¦¡¤£¤@©w­n¶}±Ò A20 ¦ì§}½u¡A¦ý¬O­Y¤£¶}±Ò¥¦¡A«hµLªkŪ¼g¶W¹L 1MB ¥H¤Wªº°O¾ÐÅé¡C¤°»ò¬O A20 ¦ì§}½u¡A¤S¬°¤°»ò­n¶}±Ò A20 ¦ì§}½u¤~¯àŪ¼g¶W¹L 1MB ¥H¤Wªº°O¾ÐÅé©O¡H­ì¨Ó 8086/8088 ¥u¦³ 20 ±ø¦ì§}½u¡A±q½s¸¹ AD0¡ãAD7¡BA8¡ãA19¡A¥i¥H©w§} 220 ¦ì¤¸²Õ¡A¤]´N¬O 1MB¡A¦ý¬O 8086/8088 ªº¼È¦s¾¹¥u¦³ 16 ¦ì¤¸¡AµLªkªí¥Ü³o»ò¦hªº¦ì§}¡A¦]¦¹³]­p 8086/8088 ªº¤uµ{®v·Q¨ì¤@­Ó¤èªk¡A¥Î¨â­Ó 16 ¦ì¤¸ªº¼È¦s¾¹¤À§O¥Nªí¡u°Ï¬q¡v¡B¡u°¾²¾¦ì§}¡v¡A¨Ã¥H¡u°Ï¬q:°¾²¾¦ì§}¡vªí¥Ü¡C¥Î³o¼Ëªºªí¥Ü¤è¦¡¡A¸Ñ¨M¤F¥H 16 ¦ì¤¸ªº¼È¦s¾¹ªí¥Ü 20 ¦ì¤¸ªº¦ì§}¡C¦ý¬O³o¼Ë°µ·|²£¥Í¤@­Ó¤£¦X²zªº²{¶H¡A°²¦p¦³­Ó«ü¥OŪ¨ú¦ì§}¡uFFFF:FFFF¡vªº¤º®e¡A¦ì§}¡uFFFF:FFFF¡v¨ä¹ê¬O¹êÅé¦ì§} 10FFEF¡A¤]´N¬O 1114095¡A¤w¸g¶W¹L 8086/8088 20 ®Ú¦ì§}½uªº©w§}½d³ò¤F¡A¤£¹L¡uFFFF:FFFF¡v«o²Å¦X¡u°Ï¬q:°¾²¾¦ì§}¡vªí¥Ü¦ì§}ªº³W«h¡C¤uµ{®v¤]«Ü§Öªº¸Ñ¨M¤F³o­Ó°ÝÃD¡A¨º´N¬O¦s¨ú¶W¹L 1MB ªº¦ì§}®É¡ACPU ·|¦Û°Ê¥ý´î¥h 1MB¡C¤]´N¬O»¡¡A·í¦s¨úÃþ¦ü¡uFFFF:FFFF¡v³oºØ¦ì§}®É¡A¨ä¹ê¬O¦s¨ú¡u0000:FFEF¡v¦ì§}ªº¤º®e¡C³o´N¬O©Ò¿×ªº¦^¶ ( wrap-around )¡C

«á¨Óªº 80286 ¦@¦³ 24 ±ø¦ì§}½u¡A¥i©w§} 16MB¡A©Ò¥H¦s¨úÃþ¦ü¡uFFFF:FFFF¡v³oºØ¦ì§}®É¡A¦³¥i¯à¬O¯uªº­n¦s¨ú¹êÅé¦ì§} 10FFEF ( «OÅ@¼Ò¦¡¤U )¡F¦ý¬O¤S§Æ±æ 80286 ¦b¯u¹ê¼Ò¦¡®É¯à¦V¤U¬Û®e¡C¬°¤F¸Ñ¨M³o­Ó°ÝÃD¡A¤uµ{®v­Ì¤S·Q¥X¤@­Ó¤èªk¡A¨º´N¬O¦b¯u¹ê¼Ò¦¡®É¡AA20 ¦ì§}½u¬O¬OÃö³¬ªº¡A­¢¨Ï CPU µo¥Í¦^¶®ÄªG¡F·í¶i¤J«OÅ@¼Ò¦¡®É¡A¤~¶}±Ò A20 ¦ì§}½u¡A¥H«KŪ¨ú 1MB ¥H¤Wªº°O¾ÐÅé¡C( °Ñ¦Òµù¤@¥i¾\Ū§ó¦hªº¸ê®Æ )

¤@¯ë¶}±Ò A20 ¦ì§}½uªº¤èªk¬O§Q¥Î 92H °ð¡A92H °ð¦ì©ó«n¾ô´¹¤ù¤W¡A±N¨ä²Ä¤@­Ó¦ì¤¸³]¬°¡u1¡v¡A´N¯à¶}±Ò A20 ¦ì§}½u¡F­Y³]¬°¡u0¡v¡A´N¯àÃö³¬¡Cµ{¦¡½X¦p¤U¡G

;¶}±Ò A20 ¦a§}½u
        in      al,92h              ;080
        or      al,00000010b
        out     92h,al

CR0 ±±¨î¼È¦s¾¹ªº PE ¦ì¤¸

80286 ¤Î¨ä¸û°ª¯Åªº CPU¡A·s¼W¤F¤@¨Ç±±¨î¼È¦s¾¹¡GCR0¡BCR1 µ¥µ¥¡C¨ä¤¤ CR0 ¼È¦s¾¹ªº²Ä 0 ¦ì¤¸¥s°µ PE ¦ì¤¸¡A­^¤åºÙ¬° protection enabled¡A·N§Y¡u«OÅ@¼Ò¦¡±Ò°Ê¡v¡C·í¦¹¦ì¤¸¬°¡u0¡v®É¡ACPU ³B©ó¯u¹ê¼Ò¦¡¡F·í¦¹¦ì¤¸¬°¡u1¡v®É¡ACPU ³B©ó«OÅ@¼Ò¦¡¡C

¦]¦¹¥u­n±N PE ¦ì¤¸Åܬ°¡u1¡v¡ACPU ´N³B©ó«OÅ@¼Ò¦¡¤F¡A³o¬qµ{¦¡½X¦b²Ä 84¡ã87 ¦æ¡A¦p¤U­±µ{¦¡¤ù¬q¡C·íµM¦pªG¶È¶È§â³o­Ó¦ì¤¸Åܦ¨¡u1¡v¡A¦Ó¨S¦³³]¸m¥¿½Tªº¡u°Ï¬q´y­z¾¹ªí®æ¡v¡A©ÎªÌ°Ï¬q¼È¦s¾¹¨S¦³«ü¦V¥¿½Tªº¡u°Ï¬q´y­z¾¹¡v¡A¨º»ò¹q¸£¬O·|·í¾÷ªº¡C©Ò¥H¥Ñ¯u¹ê¼Ò¦¡¤Á´«¨ì«OÅ@¼Ò¦¡¡A¤£¬O¶È¶È§â PE ¦ì¤¸Åܦ¨¡u1¡v´N¥i¥H¤F¡C

;·Ç³Æ¤Á´«¨ì«OÅ@¼Ò¦¡
        mov     eax,cr0
        or      eax,1
        mov     cr0,eax

¯S®íªº¸õÅD

¨ì¦¹¤w¬O¸U¨Æ¨ã³Æ¡A¥u¤íªF­·¡A¶ZÂ÷¶i¤J«OÅ@¼Ò¦¡¶È¤@¨B¤§»»¡A°õ¦æµ{¦¡½Xªº²Ä 90 ¦æ´N¶i¤J«OÅ@¼Ò¦¡¤F¡Cµ{¦¡²Ä 90 ¦æ¡A¬O¤@­Ó¥¨¶°«ü¥O¡Ajmp2pm¡C¥Ñ¯u¹ê¼Ò¦¡¶i¤J«OÅ@¼Ò¦¡¡A¤£¶È¶È DS¡BES µ¥°Ï¬q¼È¦s¾¹»Ý«ü¦V¬Û¹ïÀ³ªº°Ï¬q´y­z¾¹¡ACS ¼È¦s¾¹¤]¬O¦p¦¹¡CDS¡BES µ¥¼È¦s¾¹¥i¥H¥Î MOV «ü¥O¡A§â¾A·íªº¼Æ­È¸ü¤J¡F¦ý¬O CS µLªk³o¼Ë°µ¡A¥u¯à¥H JMP¡BJx¡BCALL¡BRET µ¥¸õÅD«ü¥O§ïÅܨä­È¡C¦b¦¹³B¡A¤p¤ì°¸±Ä¥Î JMP «ü¥O¡A©Ò¥H¦¹³Bªº«ü¥O¬°¡ujmp code_selector:0¡v¡C¦ý¬O¡A¦¹³BÁÙ­±Á{¤@­Ó°ÝÃD¡C¦b«OÅ@¼Ò¦¡ùØ¡A°¾²¾¦ì§}¬O¥H 32 ¦ì¤¸ªí¥Ü¡A¦Ó¯u¹ê¼Ò¦¡ùØ«o¬O 16 ¦ì¤¸¡A¦]¦¹³o­Ó¸õÅD«ü¥OÀ³¸Ó¬O­n¸õÅD¨ì°¾²¾¦ì§}¡u00000000¡v³B¡A¦Ó¤£¬O¡u0000¡v¡F¦Ó³o­Ó«ü¥O¤S¥²¶·¦b 16 ¦ì¤¸ªº°Ï¬q¡Aµ¥°õ¦æ¸õÅD«á¡A¤~·|Åܦ¨ 32 ¦ì¤¸°Ï¬q¡C´«¥y¸Ü»¡¡A³o­Ó¸õÅD«ü¥Oªºµ{¦¡½X¬O­Ó«Ü¯S§Oªºµ{¦¡½X¡A¥¦²V¦X¤F 16 ¦ì¤¸»P 32 ¦ì¤¸¡CMASM ¦ü¥G¬OµLªk²ÕĶ¡A©Ò¥H¤@¯ëªº°µªk¬Oª½±µ¶ñ¤W¾÷±ñ½X¡A´N¦p¦P¤U­±¡A¥H¥¨¶°¤è¦¡¶ñ¤J¾÷±ñ½X¡G

jmp2pm          MACRO   s,o
                DB      66h,0eah;¾Þ§@½X
                DD      o       ;32¦ì¤¸°¾²¾¦ì§}
                DW      s       ;°Ï¬q­È©Î°Ï¬q¿ï¾Ü¤l
                ENDM            ;021

JMP ªº¾÷±ñ½X¬O 0EAH¡A¦pªG¯u¹ê¼Ò¦¡¤U¡A­n¥Î¨ì 32 ¦ì¤¸ªºµ{¦¡½X¡A«h»Ý¥[¤W 66H §@¬°«e¸m½X¡AÃö©ó«e¸m½X¡A¥i°Ñ¦Òµù¤@¡C¤S¦]¬°°¾²¾¦ì§}¬° 32 ¦ì¤¸¡A¬G¡uo¡vªº¸ê®Æ§ÎºA¬°Âù¦r²Õ¡A¥H¡uDD¡vªí¥Ü¡C³Ì°ª¦ì§}ªº¦r²Õ«h¬O°Ï¬q¿ï¾Ü¾¹¡A¡us¡v¡C

¦b«OÅ@¼Ò¦¡¤U¡A¦@¦³¤T­Ó°Ï¬q¡Gµ{¦¡½X°Ï¬q¡B¸ê®Æ°Ï¬q¡BÅã¥Ü°O¾ÐÅé°Ï¬q¡C³o¨Ç°Ï¬qªº¯u¥¿¦ì§}¨Ã¤£¬O CS¡BDS¡BES ¤§¼Æ­È¡A¦Ó¬OÂǥѦ¹¼Æ­È¬d¸ß¡u°Ï¬q´y­z¾¹ªí®æ¡v¡C

¥Î DEBUG32 Æ[¹î

©³¤U¥H DEBUG32 ¨ÓÆ[¹î³o¨Ç¦ì§}ªºÃö«Y¡C¥H DEBUG32 ¸ü¤J PM1.COM¡G

E:\HomePage\SOURCE\PM>h:debug32 pm1.com [Enter]
Debug32 - Version 1.0 - Copyright (C) Larson Computing 1994

CPU = ?86, Virtual 8086 Mode, Id/Step = 0F10, A20 enabled
-

²Ä¤@­Ó«ü¥O¬O¸õÅD¡A¥H¯d¤U¤@¶ô°O¾ÐÅé·í§@¦s©ñ¸ê®Æ¤§¥Î¡A¥ý¬Ýµ{¦¡·|¸õÅD¦Ü¦ó³B¡A¦AÀˬd­ì¨Ó¸ê®Æ¦s¤F­þ¨Ç¡C

-u 100 101 [Enter]
291C:0100 EB2C             JMP     Short 012E

-d 100 l40 [Enter]
291C:0100 EB 2C 90 90 90 90 90 90-00 00 00 00 00 00 00 00  k,..............
291C:0110 23 00 00 00 00 98 00 00-FF FF 00 80 0B 92 00 00  #...............
291C:0120 12 00 00 00 00 92 00 00-1F 00 00 00 00 00 66 2B  ..............f+
291C:0130 C0 66 8B D8 8C C8 BB A4-01 66 C1 E0 04 66 03 C3  @f.X.H;$.fA`.f.C

±q 291C:0108 ³B¶}©l¨ì 291C:0127 ¬°¤î¡A´N¬O¡u°Ï¬q´y­z¾¹ªí®æ¡v¡A¦Ó¤W­±¬õ¦âªº³¡¥÷¬O¡uµ{¦¡½X°Ï¬q´y­z¾¹¡v¡C¡u23 00¡v¨ä¹ê¬O 23H ­Ó¦ì¤¸²Õ¡A³o¬O¦]¬° x86 ¬[ºcªº¹q¸£¸ê®Æ¦w±Æ¤è¦¡¬O¡u¼Æ¦r¦ì¼Æ¤jªº¡A¦b°ª¦ì§}¡A±Æ¦b¥kÃä¡v¡A¹³³oºØ±Æ¦C¤è¦¡ºÙ¤§¬°¡uLittle-Endian¡v¡C¦ì§} 291C:0112¡ã291C:0114 ¬Oµ{¦¡½X°Ï¬qªº°ò©³¦ì§}¡A¤£¹LÁÙ¨S¦³³]©w¦n¥¿½Tªº¼Æ­È¡A±µ¤U¨Ó¤@ª½¨ì 014E ªºµ{¦¡´N¬O¦b°µ³o¤@¥ó¨Æ¡C§Ú­Ì¥ý¤U 4 ­Ó¡ut¡v«ü¥O°lÂܬݬݡC

-t [Enter]
AX=0000  BX=0000  CX=00DB  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
DS=291C  ES=291C  SS=291C  CS=291C  IP=012E  NV UP DI PL NZ NA PO NC
291C:012E 662BC0           SUB     EAX,EAX
Trace Interrupt
-t [Enter]
AX=0000  BX=0000  CX=00DB  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
DS=291C  ES=291C  SS=291C  CS=291C  IP=0131  NV UP DI PL ZR NA PE NC
291C:0131 668BD8           MOV     EBX,EAX
Trace Interrupt
-t [Enter]
AX=0000  BX=0000  CX=00DB  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
DS=291C  ES=291C  SS=291C  CS=291C  IP=0134  NV UP DI PL ZR NA PE NC
291C:0134 8CC8             MOV     AX,CS
Trace Interrupt
-t [Enter]
AX=291C  BX=0000  CX=00DB  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
DS=291C  ES=291C  SS=291C  CS=291C  IP=0136  NV UP DI PL ZR NA PE NC
291C:0136 BBA401           MOV     BX,01A4h
Trace Interrupt

¨ì 0136 ¦ì§}®É¡A¹ï·Ó­ì©l½X¡A±oª¾²¾¶i BX ªº¼Æ­È´N¬O«OÅ@¼Ò¦¡µ{¦¡©Ò¦b¦ì§}¡A291C:01A4¡A¤]´N¬O¦bµ´¹ï¦ì§} 29364 ³B ( 291C¡Ñ10¡Ï1A4 )¡C±z¥i¦Û¦æ°lÂÜ¡A¬°¤F¸`¬Ù½g´T¡Aª½±µ°õ¦æ¨ì§â¥¿½Tªºµ´¹ï¦ì§}¶ñ¨ìµ{¦¡½X°Ï¬q´y­z¾¹¤º¡A¦A¬Ý¬Ý°O¾ÐÅ餺®e¡A¦p¤U­±²HÂŦ⪺¦a¤è¡C

-g 14e [Enter]
AX=0002  BX=01A4  CX=00DB  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
DS=291C  ES=291C  SS=291C  CS=291C  IP=014E  NV UP DI PL NZ AC PO CY
291C:014E 662BC0           SUB     EAX,EAX
Trace Interrupt
-d 100 l40 [Enter]
291C:0100 EB 2C 90 90 90 90 90 90-00 00 00 00 00 00 00 00  k,..............
291C:0110 23 00 64 93 02 98 00 00-FF FF 00 80 0B 92 00 00  #.d.............
291C:0120 12 00 00 00 00 92 00 00-1F 00 00 00 00 00 66 2B  ..............f+
291C:0130 C0 66 8B D8 8C C8 BB A4-01 66 C1 E0 04 66 03 C3  @f.X.H;$.fA`.f.C

¦A¨ìµ´¹ï¦ì§} 291C:01A4 ¤Ï²ÕĶ¬Ý¬Ý¡A¨Ã¹ï·Ó­ì©l½X¡AªG¯u¬O«OÅ@¼Ò¦¡¤Uªºµ{¦¡¤ù¬q¡A¦¹¤ù¬q¥Ñµ´¹ï¦ì§} 291C:01A4 °_¨ì 291C:01C6¡A¦@ 36 ­Ó¦ì¤¸²Õ¡A©Ò¥H¬É­­¬° 23H¡A´N¬O¤W­±¥Õ¦âªº³¡¥÷¡C


-u 1a4 1c6 [Enter]
291C:01A4 B91000           MOV     CX,0010h
291C:01A7 8EC1             MOV     ES,CX
291C:01A9 B91800           MOV     CX,0018h
291C:01AC 8ED9             MOV     DS,CX
291C:01AE 66BE00000000     MOV     ESI,00000000h
291C:01B4 66BF40060000     MOV     EDI,00000640h
291C:01BA B40E             MOV     AH,0Eh
291C:01BC AC               LODSB
291C:01BD 3C00             CMP     AL,00h
291C:01BF 0F840300         JZ      01C6
291C:01C3 AB               STOSW
291C:01C4 EBF6             JMP     Short 01BC
291C:01C6 EBFE             JMP     Short 01C6

¤À¬qÂà´« ( Segment Translation )

¨Æ¹ê¤W¡A¦b 80386 ¯u¹ê¼Ò¦¡©Î«OÅ@¼Ò¦¡¤Uªº¦ì§}¦³¦n´XºØ¡C¦¹³B©Ò¥Î¨ìªº¦³¨âºØ¡GÅÞ¿è¦ì§} ( logical address ) »Pµ´¹ï¦ì§}¡CÅÞ¿è¦ì§}¬O«ü±z¦b°£¿ù¾¹¤W©Ò¬Ý¨ìªº¦ì§}¡A³o¤]¬Oµ{¦¡©Ò¬Ý¨ìªº¦ì§}¡A¦b¯u¹ê¼Ò¦¡¤¤¡AÅÞ¿è¦ì§}´N¬O¥H¡u°Ï¬q:°¾²¾¦ì§}¡vªí¥Ü¡A¦b DEBUG/SYMDEB µ¥°£¿ù¾¹¬Ý¨ìªº¤]¬O³oºØ¦ì§}¡C¦b«OÅ@¼Ò¦¡¤§¤U¡A¦ü¥G¨Ã¨S¦³°£¿ù¾¹¥i¥Î¡A¦ý¬O¦b Windows ¨t²ÎùØ¡A¦³¦n´XºØ°£¿ù¾¹¥i¥H¨Ï¥Î¡A¨Ò¦p Olly Debug¡A¥¦­Ì©ÒÅã¥Üªº¦ì§}¤]³£¬OÅÞ¿è¦ì§}¡Cµ´¹ï¦ì§}¤]ºÙ¬°¹êÅé¦ì§} ( physical address )¡A½Ð°Ñ¦Ò«e­±ªº»¡©ú¡C¤@¯ë¦Ó¨¥¡AÅÞ¿è¦ì§}¶·¸g¹LÂà´«¤~¯àÅܦ¨¹êÅé¦ì§}¡C¦b¯u¹ê¼Ò¦¡¤¤¡AÂà´«¤è¦¡´N¬O«e­±©Ò»¡ªº¡u¹êÅé¦ì§}¡×°Ï¬q¡Ñ16¡Ï°¾²¾¦ì§}¡v¤½¦¡¡C¦Ó¦b«OÅ@¼Ò¦¡ùØ¡A¦³¨âºØÂà´«¤è¦¡¡G¤À¬qÂà´« ( segment translation ) »P¤À­¶Âà´« ( paging translation )¡C¤W­±¡A¤p¤ì°¸¥H PM1.COM ¬°¨Ò¤l¡A¤w²³æÁ¿§¹¤À¬qÂà´«¡C

¦b¦¹°µ­Ó¤pµ²½×¡C¤À¬qÂà´«¥i°Ñ¦Ò¥kÃ䪺²¹Ï¡C¹Ï¤¤¥ªÃä¶À®Ø³¡¥÷¬O CPU¡A¥u¦C¥X»P PM1.COM ¦³Ãöªº¼È¦s¾¹¡F¹Ï¤¤¥kÃä¬õ¦â¤j®Ø¬°¹êÅé°O¾ÐÅé¡A³Ì¥kÃä¥H¬õ¦âªí¥Üªº 32 ¦ì¤¸¤Q¤»¶i¦ì¼Æ­È¬°¹êÅé¦ì§}¡A¶V©³¤U¦ì§}¶V°ª¡C·íµM¤£¥i¯à§â¨C­Ó¦ì¤¸²Õ¤Î¨ä¹êÅé¦ì§}¼Ð¥Ü¥X¨Ó¡A¤p¤ì°¸¥u¼Ð¥Ü­«­nªº¡A¦Ó¨C­Ó³Ì¤pªº¡A¤¤¶¡¨S¦³¤À¹j½uªº¾î®Ø³£¤@¼Ë¤j¡A¥Nªí 8 ­Ó¦ì¤¸²Õ¡C¦b¦¹¬õ¦â®Ø¤º¡A¦³¥|³¡¥÷¹Ï¦³­I´ºÃC¦â¡A©@°Ø¦â¡BÂŦâ¡B¦Ç¦â¡Bµµ¦â¡A¤À§O¥Nªí¥þ°ì´y­z¾¹ªí®æ ( GDT ) ¡Bµ{¦¡½X°Ï¬q¡B¸ê®Æ°Ï¬q¡BÅã¥Ü°O¾ÐÅé°Ï¬q¡C¦b¦¹¬õ¦â®Ø¥ª°¼¡A¦³¥|­Ó¶À¦âªº¼Æ¦r¡A¬O¬Û¹ï©ó¥þ°ì´y­z¾¹ªí®æªº¦ì§}¡A³o¨Ç¼Æ­ÈÀ³¸Ó¶ñ¤J¾A·íªº°Ï¬q¼È¦s¾¹¡C

CPU ¤¤ªº GDTR §t¦³¥þ°ì´y­z¾¹ªí®æªº°ò©³¦ì§}¡A¦¹°ò©³¦ì§}¬O¥H¹êÅé¦ì§}ªí¹F¡A¨Ã§Q¥Î¦¹¤@¦ì§}«ü¦V°O¾ÐÅ餤ªº¥þ°ì´y­z¾¹ªí®æ¡A§YÂŦâ½bÀY©Ò«üªº¸ô½u¡C¥þ°ì´y­z¾¹ªí®æ¤¤§t¦³¨C­Ó°Ï¬qªº¸ê®Æ¡A¨C­Ó°Ï¬q¼È¦s¾¹¡A¦p CS¡BDS¡BES µ¥¡A¬Ò¬°¬Û¹ï©ó¥þ°ì´y­z¾¹ªí®æªº«ü¼Ð¡A¥HÀò±o¸Ó°Ï¬qªº¸ê®Æ¡C¨Ò¦p CS ¤§­È¬° 0008¡A§Yªí¥Ü CS ©Ò«üªº°Ï¬q´y­z¾¹¬O¦b¥þ°ì´y­z¾¹ªí®æªº²Ä 8 ­Ó¦ì¤¸²Õ¶}©l¡A§YÂëC¦â½bÀY©Ò«üªº¸ô½u¡C¦¹´y­z¾¹ªº°ò©³¦ì§}¬°¡u0002 9364¡v¡A¦]¦¹ CS ©Ò«üªº¹êÅé¦ì§}¬O¦b¡u0002 9364¡v³B ( ²`ºñ¦â½bÀY©Ò«ü )¡A¨ä¤j¤p¬°¡u23H¡Ï1¡v¡C¦b«OÅ@¼Ò¦¡¤Uªºµ{¦¡½X°Ï¬qªº°¾²¾¦ì§}¬O EIP¡A¦]¦¹ CPU ´N¥i¥H¥Ñ GDTR ©M CS §ä¨ì¦b¥þ°ì´y­z¾¹ªí®æªºµ{¦¡½X°Ï¬q´y­z¾¹ªº¦ì§}¡A¥Ñ³o¸Ì¨ú¥X°ò©³¦ì§}¡A¦A¥[¤W EIP ´N¯à§ä¨ì¤U¤@¦æ«ü¥Oªº¦ì§}¡C

¸ê®Æ¤]¬O³o¼Ë¡ACPU ÂÇ¥Ñ GDTR ©M DS §ä¨ì¥þ°ì´y­z¾¹ªí®æªº¸ê®Æ°Ï¬q´y­z¾¹ªº¦ì§}¡A¥Ñ¦¹±o¨ì¸ê®Æ°Ï¬qªº°ò©³¦ì§}¡A0002 9388¡AµM«á¦A¥Ñ 32 ¦ì¤¸ªº°¾¤@¦ì§}¡A´N¯à¦s¨ú¸ê®Æ¡CPM1 ùØ¡A¦b«OÅ@¼Ò¦¡ùئs¨úªº¸ê®Æ´N¥u¦³ string ¦r¦ê¡A¥¦¦b¸ê®Æ°Ï¬qªº°¾²¾¦ì§}¥i¥Ñ¤U­±µ{¦¡ ( ²Ä 97 ¦æ ) ºâ¥X¨Ó¡A¦s¨ì ESI ùØ¡G

        mov     esi,OFFSET string-OFFSET pm_data_seg

OFFSET string ¬O string ªº¦ì§}¡AOFFSET pm_data_seg ¬O¸ê®Æ°Ï¬qªº°ò©³¦ì§}¡A¨âªÌ¬Û´î¡A´N±o¨ì string ¦r¦êªº°¾²¾¦ì§}¡C


°Ï¬qªºÄÝ©Ê

³o¤@¸`¨Ó»¡»¡°Ï¬q´y­z¾¹¤º³Ñ¤U¨SÁ¿ªº³¡¥÷¡A¥ý¬Ý¬Ý°Ï¬q´y­z¾¹ªº¼Ë¤l¡G

TYPE

¨ä¤¤°ò©³¦ì§}¡BÃä¬É§¡¤w¸ÑÄÀ¹L¤F¡A±µ¤U¨Ó½Í½Í TYPE¡CTYPE ¦@¦³¥|­Ó¦ì¤¸¡A40¡ã43¡A¥¦­Ì©Ò¥Nªíªº·N¸q¡A¦³ÂI½ÆÂø¡C²Ä 43 ¦ì¤¸ºÙ¬° T ¦ì¤¸¡A¦Ó²Ä 40¡ã42 ¦ì¤¸ªº·N¸q©M T ¦ì¤¸¤§¼Æ­È¦³Ãö¡F­Y T¡×1¡Aªí¥Ü¦¹°Ï¬q¬°µ{¦¡½X°Ï¬q¡F­Y T¡×0¡Aªí¨Ï¦¹°Ï¬q¤£¬Oµ{¦¡½X°Ï¬q¡A¦p¸ê®Æ¬q¡B°ïÅ|¬qµ¥¡C¦p¥k¹Ï©Ò¥Ü¡C

  1. T¡×1¡Aªí¥Ü¦¹°Ï¬q¬°µ{¦¡½X°Ï¬q¡A¨º»ò²Ä 42 ¦ì¤¸ºÙ¬° C ¦ì¤¸¡F²Ä 41 ¦ì¤¸ºÙ¬° R ¦ì¤¸¡G
  2. T¡×0¡Aªí¨Ï¦¹°Ï¬q¤£¬Oµ{¦¡½X°Ï¬q¡A¦p¸ê®Æ¬q¡B°ïÅ|¬qµ¥¡A¨º»ò²Ä 42 ¦ì¤¸ºÙ¬° E ¦ì¤¸¡F²Ä 41 ¦ì¤¸ºÙ¬° W ¦ì¤¸¡G

²Ä 40 ¦ì¤¸¬O A ¦ì¤¸¡A¬O accessed ªº·N«ä¡A§PÂ_¦¹°Ï¬q¬O§_´¿¸g³Q¦s¨ú¹L¡C­Y A¡×0¡A¥¼¦s¨ú¹L¡FA¡×1¡A¤w³Q¦s¨ú¹L¡C

S ¦ì¤¸¡BDPL ©M P ¦ì¤¸

¦A¨Ó¡A¸ÑÄÀ S ¦ì¤¸¡BDPL¡BP ¦ì¤¸¤T¶µ¡G

  1. S ( system ) ¦ì¤¸¡GS¡×0¡Aªí¥Ü¦¹°Ï¬q¬° LDT ( °Ï°ì´y­z¾¹ )¡BTSS ´y­z¾¹µ¥¡A¤@¯ë¬Oµ¹§@·~¨t²Î¨Ï¥Î¡FS¡×1¡Aªí¥Ü¦¹°Ï¬q¬°µ{¦¡½X¡B¸ê®Æ¡B°ïÅ|µ¥¤@¯ë°Ï¬q¡C
  2. DPL ( descriptor privilege level)¡Gªí¥Ü¦¹°Ï¬qªº¯SÅvµ¥¯Å¡A¦@¦³¨â­Ó¦ì¤¸¡A80386 ªº¯SÅvµ¥¯Å¤À¬°¥|¯Å¡A0 ¬O³Ì°ªµ¥¯Å¡A3 ¬O³Ì§Cµ¥¯Å¡A¥H«á¦A¤¶²Ð¡C
  3. P ( present ) ¦ì¤¸¡GP¡×0¡Aªí¥Ü¦¹°Ï¬q¦b¹êÅé°O¾ÐÅéùØ¡FP¡×1¡Aªí¥Ü¦¹°Ï¬q¤£¦b¹êÅé°O¾ÐÅéùØ¡A¥i¯à©ñ¦bµêÀÀ°O¾ÐÅéùØ¡C

TYPE¡BS ¦ì¤¸¡BDPL¡BP ¦ì¤¸¥ç¦XºÙ¦s¨úÅv­­ ( access rights )¡C

¨ä¥L¦ì¤¸¡GAVL¡BD¡BG ¦ì¤¸

  1. AVL ( available ) ¦ì¤¸¡G«O¯dµ¹¨t²Îµ{¦¡¨Ï¥Î¡C
  2. D ¦ì¤¸ ( default size )¡G¦¹¦ì¤¸¸û¬°½ÆÂø¡A¥i¤À¬°¤TºØ±¡§Î¡G
  3. G ( granularity ) ¦ì¤¸¡G¦¹¦ì¤¸¨M©wÃä¬É ( limit ) ªº³æ¦ì¡A­Y G¡×0¡AÃä¬Éªº³æ¦ì¬°¦ì¤¸²Õ¡F­Y G¡×1¡AÃä¬Éªº³æ¦ì¬° 4KB¡C

¦A¦^ÀY¬Ý¬Ý PM1 ªºµ{¦¡½X°Ï¬q´y­z¾¹¡A¥Ñ§C¦ì¤¸²Õ¨ì°ª¦ì¤¸²Õ¬°¡u23 00 64 93 02 98 00 00¡v¡Aºñ¦â³¡¥÷¬°Ãä¬É¡A¥Õ¦â³¡¥÷¬°°ò©³¦ì§}¡A´N¤£²Ó»¡¤F¡C¬õ¦â³¡¥÷¡A¡u098¡v¡A¬°ÄÝ©Ê¡A³Ì°ª¦ìªº 4 ­Ó¦ì¤¸§¡¬°¹s¡A¦]¦¹ G¡×0¡BD¡×0¡BAVL¡×0¡C¥Ñ G¡×0 ¥iª¾¡A¦¹°Ï¬qÃä¬É¥H¦ì¤¸²Õ¬°³æ¦ì¡A¦]¦¹¦¹¥Ñ¹êÅé¦ì§} 29364H ¶}©l¡A¨ì 29387H ¬°¤î¡A¦@ 24H ­Ó¦ì¤¸²Õ¡C¥Ñ D¡×0 ¥iª¾¡A¦¹°Ï¬q¹w³]¬° 16 ¦ì¤¸ªº°Ï¬q¡A¦]¦¹¼È¦s¾¹¡B¹Bºâ¤¸¹w³]¬° 16 ¦ì¤¸¡A¦pªG­n¥Î¨ì 32 ¦ì¤¸ªº¼È¦s¾¹¡A±o¦b¾÷±ñ½X«e­±¥[¤W«e¸m½X 66H¡A¥i¹ï·Ó PM1 ­ì©l½X pm: ¼Ð¸¹«á´X¦æªº

        mov     esi,OFFSET string-OFFSET pm_data_seg
        mov     edi,(80*10+0)*2     ;¿Ã¹õ²Ä 10 ¦C¡A²Ä 0 ¦æ¡C

³Q²ÕĶ«áªº¾÷±ñ½X¡G

291C:01AE 66BE00000000     MOV     ESI,00000000h
291C:01B4 66BF40060000     MOV     EDI,00000640h

ª`·N¨ì¬O¤£¬O¦h¤F¬õ¦âªº«e¸m½X¡H( ¾\Ū²Ä 30 ³¹¥iª¾¹D§ó¦hªº«e¸m½Xª¾ÃÑ )

¦A¨Óªº P ¦ì¤¸¡BDPL ©M S ¦ì¤¸¬°¡u9¡v¡A9 ªº¤G¶i¦ì¬°¡u1001¡v¡A¦]¦¹ P¡BS ¦ì¤¸§¡¬°¡u1¡v¡A¤]´N¬O¦¹°Ï¬q¦s¦b©ó¹êÅé°O¾ÐÅ餤¡A¨Ã¥B¬°¤@¯ë°Ï¬q¡A§Yµ{¦¡½X°Ï¬q¦Ó«D LDT µ¥¨t²Î¨Ï¥Îªº°Ï¬q¡CDPL ¬° 0¡Aªí¥Ü¯SÅvµ¥§Y¬O³Ì°ªµ¥¯Å¡C

³Ì«áªº TYPE ­È¬°¡u8¡v¡AÅܦ¨¤G¶i¦ì¬O¡u1000¡v¡A¦]¦¹ T¡×1¡Aªí¥Ü¬Oµ{¦¡½X°Ï¬q¡AC¡BR¡BA §¡¬°¡u0¡v¡A¤À§Oªí¥Ü¤@­P¡B¤£¯àŪ¨ú¡B¥¼³Q¦s¨ú¹L¡C


±q«OÅ@¼Ò¦¡ªð¦^¯u¹ê¼Ò¦¡

ÁöµMÂÇ¥Ñ PM1¡A§Ú­Ì¤¶²Ð¤F¦p¦ó¶i¤J«OÅ@¼Ò¦¡¡A¦ý¬O¤´µM¨S¦³¨Ï¥Î¨ì«OÅ@¼Ò¦¡¥¨¤j°O¾ÐÅ骺¦n³B¡A¦P®É PM1 ¤]¨S¦³±q«OÅ@¼Ò¦¡ªð¦^ DOS¡A¥u¦nÅý PM1 ¶i¤JµL½a°j°é¦Ó·í¾÷¡C©³¤U¤p¤ì°¸¦A¹ê§@¤@­Óµ{¦¡¡APM2¡A³o­Óµ{¦¡¶i¤J«OÅ@¼Ò¦¡«á¡A¥ýŪ¨ú¹êÅé¦ì§} 200000H ³B ( ¥Ñ 2MB ) ¶}©lªº´X­Ó¦ì¤¸²Õ¡A±N¨ä¤º®e¦L¦b¿Ã¹õ¤W¡A¦A©ó¬Û¦P¦ì§}¼g¤J¡uI learn protected mode with assembly.¡v¦r¦ê¡C±µµÛ¦A±N¦ì§} 200000H ªº¤º®e¦L¦b¿Ã¹õ¤W¡C¦pªG¯uªº¶i¤J«OÅ@¼Ò¦¡¡A¨Ã¥B¼g¤J¦¨¥\¡A¨º»ò¿Ã¹õ¤W©ÒÅã¥Üªº¤º®e¡AÀ³·í¤£¦P¡C©³¤U¬O PM2 ªº­ì©l½X¡G

.386P
PAGE    ,132

tr_seg  EQU     200000h         ;¥Øªº¸ê®Æ°Ï¬q¦ì§}¬° 2M ³B
tr_len  EQU     string_pm_len-1 ;¥Øªº¸ê®Æ°Ï¬qªø«×

jmp2pm  MACRO   s,o
        DB      66h,0eah;¾Þ§@½X
        DW      o,0     ;32¦ì¤¸°¾²¾¦ì§}
        DW      s       ;°Ï¬q¿ï¾Ü¾¹
        ENDM

jmp2_16 MACRO   s,o
        DB      66h,0eah;¾Þ§@½X
        DW      o       ;16¦ì¤¸°¾²¾¦ì§}
        DW      s       ;°Ï¬q¿ï¾Ü¾¹
        ENDM

pdesc   STRUC
limit   DW      0       ;¥þ°ì´y­z¾¹ªí®æ¤j¤p
base    DD      0       ;32¦ì¤¸°ò©³¦ì§}
pdesc   ENDS

desc            STRUC
limit_l         DW      0       ;°Ï¬qÃä¬É(BIT0-15)
base_l          DW      0       ;°Ï¬q¦ì§}(BIT0-15)
base_m          DB      0       ;°Ï¬q¦ì§}(BIT16-23)
attributes      DB      0       ;°Ï¬qÄÝ©Ê
limit_h         DB      0       ;°Ï¬qÃä¬É(BIT16-19)(§t°Ï¬qÄݩʪº°ª4¦ì)
base_h          DB      0       ;°Ï¬q¦ì§}(BIT24-31)
desc            ENDS

;¥H¥¨¶°«ü¥O©w¸q°Ï¬q¿ï¾Ü´y­z¾¹©w¸q
;¥Îªk¡Gdescriptor       °Ï¬q¦W,°ò©³¦ì§},°Ï¬q¤j¤p,ÄÝ©Ê
;descriptor¥¨¶°·|¦Û°Ê§â°ò©³¦ì§}¡B°Ï¬q¤j¤p¡BÄÝ©ÊÂkÃþ¨ì¦UÄæ¦ì
;¡A¥Bdescriptor¥¨¶°¥²¶·©M desc µ²ºcÅé¦@¥Î
descriptor      MACRO   desc_name,base,limit,attribute
limit_0         =       limit AND 0ffffh
limit_1         =       ( ( limit AND 0ffff0000h ) SHR 10h ) AND 0fh
attrib_0        =       attribute AND 0ffh
attrib_1        =       ( ( ( attribute SHR 8 ) SHL 4 ) OR limit_1 ) AND 0ffh
base_0          =       base AND 0ffffh
base_1          =       ( ( base AND 0ff0000h ) SHR 10h ) AND 0ffh
base_2          =       ( ( base AND 0ff000000h ) SHR 18h ) AND 0ffh
desc_name       desc    <limit_0,base_0,base_1,attrib_0,attrib_1,base_2>
                ENDM

;*******************************************************************************
data            SEGMENT USE16
gdt             LABEL   BYTE                            ;¥þ°ì´y­z¾¹ªí®æ
descriptor      dummy,0,0,0                             ;ªÅ´y­z¾¹
descriptor      pm_code32,      0,   pm_c_len-1,498h    ;32¦ì¤¸µ{¦¡½X°Ï¬q´y­z¾¹
descriptor      pm_datasr,      0,pm_d_sr_len-1, 92h    ;¨Ó·½¸ê®Æ°Ï¬q´y­z¾¹
descriptor      pm_datatr, tr_seg,       tr_len, 92h    ;¥Øªº¸ê®Æ°Ï¬q´y­z¾¹
descriptor      pm_video, 0b8000h,       0ffffh, 92h    ;Åã¥Ü°O¾ÐÅé°Ï¬q´y­z¾¹
descriptor      pm_stack,       0,  stack_len-1, 92h
descriptor      pm_code16,      0,       0ffffh, 98h    ;·Ç³Æ¸õ¦^¯u¹ê¼Ò¦¡ªº16¦ì¤¸µ{¦¡½X°Ï¬q´y­z¾¹
descriptor      normal,         0,       0ffffh, 92h
gdt_len         =       $-gdt                   ;¥þ°ì´y­z¾¹ªí®æªº¤j¤p
gdt_ptr         pdesc   <gdt_len-1,0>           ;¥þ°ì´y­z¾¹ªí®æ¸ê®Æ
code32_selector =       pm_code32-gdt           ;µ{¦¡½X°Ï¬q¿ï¾Ü¾¹
datasr_selector =       pm_datasr-gdt           ;¨Ó·½¸ê®Æ°Ï¬q¿ï¾Ü¾¹
datatr_selector =       pm_datatr-gdt           ;¥Øªº¸ê®Æ°Ï¬q¿ï¾Ü¾¹
video_selector  =       pm_video-gdt            ;Åã¥Ü°O¾ÐÅé°Ï¬q¿ï¾Ü¾¹
stack_selector  =       pm_stack-gdt            ;°ïÅ|°Ï¬q¿ï¾Ü¾¹
code16_selector =       pm_code16-gdt           ;·Ç³Æ¸õ¦^¯u¹ê¼Ò¦¡ªº 16 ¦ì¤¸µ{¦¡½X°Ï¬q¿ï¾Ü¾¹
normal_selector =       normal-gdt
SaveSP          DW      ?                       ;¥Î©ó«O¦sSP¼È¦s¾¹
SaveSS          DW      ?                       ;¥Î©ó«O¦sSS¼È¦s¾¹
;-------------------------------------------------------------------------------
data            ENDS
;*******************************************************************************
pm_stack_seg    SEGMENT PARA STACK USE16
stack_len       =       256
                DB      stack_len DUP(0)
pm_stack_seg    ENDS
;*******************************************************************************
pm_datasr_seg   SEGMENT PARA USE16
string_pm       DB      'I learn protected mode with assembly.',0
string_pm_len   =       $-string_pm+1
char_per_line   DD      ?       ;¿Ã¹õ¤W¡A¨C¤@¦C¦L¥X 16 ­Ó¦r¤¸¤Î¸Ó¦r¤¸©Ò¥Nªíªº¼Æ­È
x               DB      0       ;¸ê®Æ±q¿Ã¹õ²Ä 9 ¦C¡B²Ä 0 ¦æ¶}©lÅã¥Ü
y               DB      9
pm_d_sr_len     =       $-string_pm
pm_datasr_seg   ENDS
;*******************************************************************************
code    SEGMENT USE16
        ASSUME  cs:code,ds:data
;-------------------------------------------------------------------------------
main    PROC
        mov     ax,data
        mov     ds,ax

;³]¸m«OÅ@¼Ò¦¡¤¤¡A32 ¦ì¤¸µ{¦¡½X°Ï¬qªº´y­z¾¹
        sub     eax,eax
        xor     ebx,ebx
        mov     ax,pm_code32_seg
        mov     bx,OFFSET pm_code32_start
        shl     eax,4
        add     eax,ebx
        mov     pm_code32.base_l,ax
        shr     eax,10h
        mov     pm_code32.base_m,al
        mov     pm_code32.base_h,ah

;³]¸m«OÅ@¼Ò¦¡¤¤¡A16 ¦ì¤¸µ{¦¡½X°Ï¬qªº´y­z¾¹
        sub     eax,eax
        xor     ebx,ebx
        mov     ax,pm_code16_seg
        mov     bx,OFFSET main16
        shl     eax,4
        add     eax,ebx
        mov     pm_code16.base_l,ax
        shr     eax,10h
        mov     pm_code16.base_m,al
        mov     pm_code16.base_h,ah

;³]¸m«OÅ@¼Ò¦¡¤¤¡A¨Ó·½¸ê®Æ°Ï¬qªº´y­z¾¹
        xor     eax,eax
        mov     ax,pm_datasr_seg
        shl     eax,4
        mov     pm_datasr.base_l,ax
        shr     eax,10h
        mov     pm_datasr.base_m,al
        mov     pm_datasr.base_h,ah

;³]¸m«OÅ@¼Ò¦¡¤¤¡A°ïÅ|°Ï¬q´y­z¾¹
        mov     ax,ss
        mov     WORD PTR SaveSS,ax
        mov     WORD PTR SaveSP,sp
        sub     eax,eax
        mov     ax,pm_stack_seg
        shl     eax,4
        mov     WORD PTR pm_stack.base_l,ax
        shr     eax,10h
        mov     BYTE PTR pm_stack.base_m,al
        mov     BYTE PTR pm_stack.base_h,ah

;¶ñ¤J¥¿½Tªº¼Æ­È¨ì gdt_ptr ùØ
        sub     eax,eax
        xor     ebx,ebx
        mov     ax,ds
        mov     bx,OFFSET gdt
        shl     eax,4
        add     eax,ebx         ;­pºâ¨Ã³]¸m°ò¦ì§}
        mov     gdt_ptr.base,eax
;¥Î MASM 6.x ²ÕĶ®É¡A§ï¦¨¡ulgst gdt_ptr¡v¡F¥Î MASM 5.x ²ÕĶ®É¡A
        lgdt    QWORD PTR gdt_ptr       ;§ï¦¨¡ulgst QWORD PTR gdt_ptr¡v

        cli                     ;Ãö¤¤Â_

;¶}°_ A20 ¦ì§}½u
        in      al,92h
        or      al,00000010b
        out     92h,al

;¤Á´«¨ì«OÅ@¼Ò¦¡
        mov     eax,cr0
        or      al,1
        mov     cr0,eax

;²M«ü¥O¹w¨ú¶¤¦C,¨Ã¯u¥¿¶i¤J«OÅ@¼Ò¦¡
        jmp2pm   code32_selector,<OFFSET main32>

rm_entry:       ;¦^¨ì¯u¹ê¼Ò¦¡®Éªº¶i¤JÂI
        mov     ax,data
        mov     ds,ax
        mov     sp,SaveSP
        mov     ss,SaveSS

;Ãö³¬ A20 ¦ì§}½u
        in      al,92h
        and     al,11111101b
        out     92h,al
        sti

;µ²§ôµ{¦¡¡Aªð¦^ DOS
        mov     ax,4c00h
        int     21h
main    ENDP
;-------------------------------------------------------------------------------
code    ENDS
;*******************************************************************************
pm_code32_seg   SEGMENT USE32
                ASSUME  cs:pm_code32_seg,ds:pm_datasr_seg
pm_code32_start:
;-------------------------------------------------------------------------------
;¥Ñ DL ( ²Ä´X¦æ¡Ax )¡BDH ( ²Ä´X¦C¡Ay ) ¨D¥XÅã¥Ü°O¾ÐÅ骺¦ì§}¡A¦s©ó EDI ùØ¡C
;¤½¦¡¡GEDI¡×160*DH+2*DL¡×128*DH+32*DH+2*DL
set_edi PROC
        movzx   eax,x
        movzx   edi,y
        mov     ebx,edi
        shl     eax,1   ;EAX¡×2*DL
        shl     ebx,7   ;EBX¡×128*DH
        shl     edi,5   ;EBX¡×32*DH
        add     edi,ebx
        add     edi,eax
        ret
set_edi ENDP
;-------------------------------------------------------------------------------
ascii   PROC
        and     al,0fh
        add     al,'0'
        cmp     al,'9'
        jbe     number
        add     al,7
number: stosw
        ret
ascii   ENDP
;-------------------------------------------------------------------------------
;¦b¿Ã¹õ¤W B8000:EDI ³B¦L¥X AL ¤§¤Q¤»¶i¦ì¼Æ­È
print_char      PROC
        mov     bl,al
        shr     al,4
        call    ascii
        mov     al,bl
        call    ascii
        ret
print_char      ENDP
;-------------------------------------------------------------------------------
;¦¹°Æµ{¦¡·|§â FS:ESI ©Ò«ü¦ì§}¤§¤º®e¦L¦b¿Ã¹õ B8000:EDI ¤W
print_line      PROC
        mov     ah,0eh
        push    ecx
        mov     edx,tr_seg
        xor     ebx,ebx
        add     edx,esi
        mov     ecx,4
next0:  shld    ebx,edx,8
        mov     al,bl
        call    print_char
        shl     edx,8
        loop    next0
        mov     al,' '
        stosw
        pop     ecx

        mov     char_per_line,10h
        push    esi
        push    ecx
next1:  mov     al,fs:[esi]
        inc     esi
        call    print_char
        mov     al,' '
        stosw
        dec     ecx
        jz      blank
        dec     char_per_line
        jnz     next1
        jmp     ok1

blank:  dec     char_per_line
        jz      ok1
        mov     al,' '
        stosw
        stosd
        jmp     blank

ok1:    pop     ecx
        pop     esi
        mov     char_per_line,10h
next2:  mov     al,fs:[esi]
        inc     esi
        cmp     al,20h
        jae     print
        mov     al,'.'
print:  stosw
        dec     ecx
        jz      ok2
        dec     char_per_line
        jnz     next2
ok2:    ret
print_line      ENDP
;-------------------------------------------------------------------------------
;¦¹°Æµ{¦¡¥ýŪ¨ú FS:00000000 ¶}©lªº string_len ­Ó¦ì¤¸²Õ¡A¨Ã§â¸ê®ÆÅã¥Ü¦b¿Ã¹õ¤W¡A
;µM«á§â DS:string_pm ©Ò§}ªº¦r¦ê¼g¤J FS:00000000¡A¦A§â¨ä¤º®e¦L¦b¿Ã¹õ¤W¡C
;FS:00000000 ¦ì§}¦ì©óª«²z¦ì§} tr_seg ³B¡C
main32  PROC
        mov     ax,stack_selector
        mov     ss,ax
        mov     esp,stack_len
        mov     ax,datasr_selector
        mov     ds,ax
        mov     ax,video_selector
        mov     es,ax
        mov     ax,datatr_selector
        mov     fs,ax

        xor     esi,esi
        mov     ecx,string_pm_len
        cld
next3:  call    set_edi
        call    print_line
        jecxz   ok3
        mov     x,0
        inc     y
        loop    next3

ok3:    mov     esi,OFFSET string_pm
        xor     ebx,ebx
        mov     ecx,string_pm_len
next4:  lodsb
        mov     fs:[ebx],al
        inc     ebx
        loop    next4

        mov     x,0
        mov     y,13
        mov     ecx,string_pm_len
        xor     esi,esi
next5:  call    set_edi
        call    print_line
        jecxz   ok4
        mov     x,0
        inc     y
        loop    next5

ok4:    jmp2_16 code16_selector,<OFFSET main16>
main32  ENDP
;-------------------------------------------------------------------------------
pm_c_len        =       $-pm_code32_start
pm_code32_seg   ENDS
;*******************************************************************************
pm_code16_seg   SEGMENT USE16
                ASSUME  cs:pm_code16_seg
;-------------------------------------------------------------------------------
main16  PROC
        mov     ax,normal_selector
        mov     ds,ax
        mov     es,ax
        mov     ss,ax
        mov     eax,cr0
        and     al,11111110b
        mov     cr0,eax
        jmp     FAR PTR rm_entry
main16  ENDP
;-------------------------------------------------------------------------------
pm_code16_seg   ENDS
;*******************************************************************************
END     main

¸ò PM1 ¤@¼Ë¡A§Q¥Î¤å®Ñ³B²z³nÅé¡A¿é¤J¤W­z­ì©l½X«á¡A¦s¦¨ PM2.ASM ÀÉ¡A¶}±Ò Virtual PC¡A¨Ì¤U¹Ïºñ®Ø¤º①¡B②«ü¥O²ÕĶ¨Ã³sµ²¡G

²ÕĶ¨Ã³sµ²¦¨¥\«á¡A¿é¤J③³Bµµ®Ø¤º«ü¥O¡A°õ¦æ PM2¡A·|¬Ý¨£¦b»È¹õ¤¤¦L¥X¶À¦â¤º®e¡A¤W­±¥H¬õ®Ø®Ø¦íªº¬O­ì¨Ó 2MB ³Bªº 36 ­Ó¦ì¤¸²Õ¡A¤º®e§¡¬°¡u0¡v¡A¦ÓÂÅ®Ø®Ø¦íªº¬O PM2 ¶ñ¤Jªº¤º®e¡C«Ü©úÅ㪺¡A¨âªÌ¤£¬Û¦P¡C¹Ï¤¤³Ì¤U­±⑥¥Õ®Ø³B¥i¥H¨£¨ì¡A¤wªð¦^ DOS¡A¥i¥HÄ~Äò¿é¤J«ü¥O¡A¤£·|·í¾÷¡C©³¤U»¡©ú PM2¡C

descriptor ¥¨¶°

¨C¦¸©w¸q°Ï¬q´y­z¾¹¡A³£»Ý­n­«·s­pºâ¡A«Ü¬O³Â·Ð¡A¦]¦¹¤p¤ì°¸ÁÙ¼¶¼g¤F descriptor ¥¨¶°¡A³o­Ó¥¨¶°¥i¥H¿é¤J°Ï¬q´y­z¾¹ªº¦WºÙ¡B°ò©³¦ì§}¡BÃä¬É¡BÄÝ©Ê¡AµM«áÂàÅܦ¨­^¯Sº¸©Ò©w¸qªº¨ººØ«Ü¤£ª½Ä±ªº°Ï¬q´y­z¾¹®æ¦¡ ( °Ï¬q°ò©³¦ì§}¡B°Ï¬qÃä¬É³£¤À¦¨¨â³¡¥÷ )¡A³o­Ó¥¨¶°¬O¡G

;¥H¥¨¶°«ü¥O©w¸q°Ï¬q¿ï¾Ü´y­z¾¹©w¸q
;¥Îªk¡Gdescriptor       °Ï¬q¦W,°ò©³¦ì§},°Ï¬q¤j¤p,ÄÝ©Ê
;descriptor¥¨¶°·|¦Û°Ê§â°ò©³¦ì§}¡B°Ï¬q¤j¤p¡BÄÝ©ÊÂkÃþ¨ì¦UÄæ¦ì
;¡A¥Bdescriptor¥¨¶°¥²¶·©M desc µ²ºcÅé¦@¥Î
descriptor      MACRO   desc_name,base,limit,attribute
limit_0         =       limit AND 0ffffh
limit_1         =       ( ( limit AND 0ffff0000h ) SHR 10h ) AND 0fh
attrib_0        =       attribute AND 0ffh
attrib_1        =       ( ( ( attribute SHR 8 ) SHL 4 ) OR limit_1 ) AND 0ffh
base_0          =       base AND 0ffffh
base_1          =       ( ( base AND 0ff0000h ) SHR 10h ) AND 0ffh
base_2          =       ( ( base AND 0ff000000h ) SHR 18h ) AND 0ffh
desc_name       desc    <limit_0,base_0,base_1,attrib_0,attrib_1,base_2>
                ENDM

¦¹³Bªº AND¡BSHR¡BSHL¡BOR µ¥¨Ã¤£¬O x86 «ü¥O¡A¦Ó¬O¹Bºâ¤l¡A¦b¨Ï¥Î®É¡Abase¡Blimit¡Battribute ³£¬O¤wª¾¼Æ¡A¤£¯à¬O¼È¦s¾¹©ÎÅܼơA¥¦­Ì¦b²ÕĶ¶¥¬q´N¤w¸g½T©w¤F¡C¦³¤F³o­Ó¥¨¶°¡A¥H«á­n©w¸q°Ï¬q´y­z¾¹¡A¥u»Ý­n¹³

descriptor      pm_video, 0b8000h,       0ffffh, 92h

´N¥i¥H¤F¡C³o¬O©w¸q¤@­Ó¦W¬° pm_video °Ï¬q´y­z¾¹¡A°ò©³¦ì§}¬O 0B8000H¡A°Ï¬qÃä¬É¬O 0FFFFH¡AÄݩʬ° 92H¡C


µù¤G¡G