【PM复习】终结:中断和异常

来源:互联网 发布:简述矢量数据 编辑:程序博客网 时间:2024/05/16 14:45

     直接上代码:

Code:
  1. %include "pm.inc"  
  2.   
  3. Page_Dir_Base1  equ 200000h   
  4. Page_Tbl_Base1  equ 201000h   
  5. Page_Dir_Base2  equ 210000h   
  6. Page_Tbl_Base2  equ 211000h   
  7.   
  8. Proc_Paging_Demo    equ 301000h   
  9. Linear_Addr             equ 401000h   
  10. Proc_A                      equ 401000h   
  11. Proc_B                      equ 501000h   
  12.   
  13. org 0100h       
  14. jmp LABEL_BEGIN       
  15.       
  16. [section .gdt]       
  17. LABEL_DESC_DUMMY:       
  18.     Descriptor  0,0,0     
  19. LABEL_DESC_CODE32:    
  20.     Descriptor  0,GDT_Code32_Len - 1,DA_CR + DA_32     
  21. LABEL_DESC_CODE16:   
  22.     Descriptor  0,GDT_Code16_Len - 1,DA_C   
  23. LABEL_DESC_VIDEO:       
  24.     Descriptor  0b8000h,0ffffh,DA_DRW   
  25. LABEL_DESC_DATA:   
  26.     Descriptor  0,Data_Len - 1,DA_DRW   
  27. LABEL_DESC_STACK32:    
  28.     Descriptor  0,Stack_Len - 1,DA_DRW + DA_32   
  29. LABEL_DESC_NORMAL:   
  30.     Descriptor  0,0ffffh,DA_DRW   
  31. LABEL_DESC_FLAT_C:   
  32.     Descriptor  0,0fffffh,DA_C + DA_32 + DA_LIMIT4K   
  33. LABEL_DESC_FLAT_RW:   
  34.     Descriptor  0,0fffffh,DA_DRW + DA_LIMIT4K   
  35.            
  36. GDT_Len equ $ - LABEL_DESC_DUMMY       
  37. GDT_Ptr:       
  38.   dw  GDT_Len - 1       
  39.   dd  0       
  40.       
  41. Selector_Code32 equ LABEL_DESC_CODE32 - LABEL_DESC_DUMMY       
  42. Selector_Code16 equ LABEL_DESC_CODE16 - LABEL_DESC_DUMMY   
  43. Selector_Video  equ LABEL_DESC_VIDEO - LABEL_DESC_DUMMY     
  44. Selector_Data       equ LABEL_DESC_DATA - LABEL_DESC_DUMMY     
  45. Selector_Stack32 equ    LABEL_DESC_STACK32 - LABEL_DESC_DUMMY   
  46. Selector_Normal equ LABEL_DESC_NORMAL - LABEL_DESC_DUMMY   
  47. Selector_Flat_C equ LABEL_DESC_FLAT_C - LABEL_DESC_DUMMY   
  48. Selector_Flat_RW    equ LABEL_DESC_FLAT_RW - LABEL_DESC_DUMMY   
  49.   
  50. [section .idt]   
  51. LABEL_IDT:   
  52. %rep    32   
  53.     Gate    Selector_Code32,General_Handler,0,DA_386IGate      
  54. %endrep   
  55.     Gate    Selector_Code32,Clock_Handler,0,DA_386IGate   
  56. %rep    222   
  57.     Gate    Selector_Code32,General_Handler,0,DA_386IGate      
  58. %endrep   
  59.        
  60. _Real_Mode_IDT_Value:   
  61.   dw    0   
  62.     dd  0   
  63.   
  64. IDT_Len equ $ - $$   
  65. IDT_Ptr:       
  66.     dw  IDT_Len   
  67.     dd  0   
  68.   
  69. [section .stack32]   
  70. [bits 32]   
  71. LABEL_STACK32:   
  72.     times 512 db 0   
  73. Stack_Len   equ $ - $$   
  74.   
  75. [section .data]   
  76. LABEL_DATA:   
  77. _d_Disp_Pos dd  160 * 5   
  78. d_Disp_Pos  equ _d_Disp_Pos - $$   
  79. _w_SP_Value_In_Real_Mode    dw  0   
  80. w_SP_Value_In_Real_Mode equ _w_SP_Value_In_Real_Mode - $$   
  81.   
  82. _sz_Memory_Info_Title   db 'BaseAddrL  BaseAddrH  LengthLow  LengthHigh  Type',0   
  83. sz_Memory_Info_Title    equ _sz_Memory_Info_Title - $$   
  84. _sz_Ram_Size    db 'RAM SIZE:',0   
  85. sz_Ram_Size equ _sz_Ram_Size - $$   
  86. _sz_Return  db  0ah,0   
  87. sz_Return equ   _sz_Return - $$   
  88. _sz_Space   db  20h,0   
  89. sz_Space    equ _sz_Space - $$   
  90.   
  91. _Memory_Info_Buffer times 256 db 0   
  92. Memory_Info_Buffer  equ _Memory_Info_Buffer - $$   
  93. _d_Memory_Info_Num  dd  0   
  94. d_Memory_Info_Num   equ _d_Memory_Info_Num - $$   
  95.   
  96. _ARDStruct:   
  97.     _d_Base_Addr_Low    dd  0   
  98.     _d_Base_Addr_High   dd  0   
  99.     _d_Length_Low   dd  0   
  100.     _d_Length_High  dd  0   
  101.     _d_Type dd  0   
  102.        
  103. ARDStruct   equ _ARDStruct - $$   
  104.     d_Base_Addr_Low equ _d_Base_Addr_Low    - $$   
  105.     d_Base_Addr_High    equ _d_Base_Addr_High   - $$   
  106.     d_Length_Low    equ _d_Length_Low - $$   
  107.     d_Length_High   equ _d_Length_High - $$   
  108.     d_Type  equ _d_Type - $$   
  109.        
  110. _d_Ram_Size dd  0   
  111. d_Ram_Size  equ _d_Ram_Size - $$   
  112. _d_Page_Table_Num   dd  0   
  113. d_Page_Table_Num    equ _d_Page_Table_Num - $$   
  114.   
  115. _Real_Mode_IMR_Value    db  0   
  116. Real_Mode_IMR_Value equ _Real_Mode_IMR_Value - $$   
  117.   
  118. Data_Len    equ $ - $$   
  119.       
  120. [section .s16]       
  121. [bits 16]       
  122. LABEL_BEGIN:       
  123.     mov ax,cs       
  124.   mov ds,ax       
  125.   mov es,ax       
  126.   mov   ax,ss   
  127.   mov   sp,0100h   
  128.      
  129.   mov   [LABEL_GO_BACK_TO_REAL + 3],ax   
  130.      
  131.   mov   ebx,0   
  132.   mov   di,_Memory_Info_Buffer   
  133.   mov   ecx,20   
  134.   mov   edx,0534d4150h   
  135.   
  136. .loop:   
  137.     mov eax,0e820h   
  138.   int   15h   
  139.   jc    LABEL_MEMORY_CHK_FAIL   
  140.   inc   dword [_d_Memory_Info_Num]   
  141.   add   di,20   
  142.   cmp   ebx,0   
  143.   jne   .loop   
  144.   jmp   LABEL_MEMORY_OK   
  145.      
  146. LABEL_MEMORY_CHK_FAIL:   
  147.     mov dword [_d_Memory_Info_Num],0   
  148. LABEL_MEMORY_OK:   
  149.            
  150.   Fill_Descriptor   LABEL_DESC_CODE32,LABEL_CODE32   
  151.   Fill_Descriptor   LABEL_DESC_CODE16,LABEL_BEGIN   
  152.   Fill_Descriptor   LABEL_DESC_DATA,LABEL_DATA   
  153.   Fill_Descriptor   LABEL_DESC_STACK32,LABEL_STACK32   
  154.            
  155.   xor eax,eax       
  156.   mov ax,ds       
  157.   shl eax,4       
  158.   add eax,LABEL_DESC_DUMMY       
  159.   mov dword [GDT_Ptr + 2],eax       
  160.      
  161.   xor   eax,eax   
  162.   mov   ax,ds   
  163.   shl   eax,4   
  164.   add   eax,LABEL_IDT   
  165.   mov   dword [IDT_Ptr + 2],eax   
  166.            
  167.   lgdt  [GDT_Ptr]       
  168.      
  169.   sidt  [_Real_Mode_IDT_Value]   
  170.   in    al,21h   
  171.     mov [_Real_Mode_IMR_Value],al   
  172.     lidt    [IDT_Ptr]   
  173.          
  174.   mov   [w_SP_Value_In_Real_Mode],sp   
  175.            
  176.   cli       
  177.            
  178.   in  al,92h       
  179.   or  al,00000010b       
  180.   out 92h,al       
  181.            
  182.   mov eax,cr0       
  183.   or  al,1       
  184.   mov cr0,eax       
  185.            
  186.   jmp dword Selector_Code32:0       
  187.        
  188. _LABEL_PREPARE_GO_BACK_TO_REAL:   
  189. LABEL_PREPARE_GO_BACK_TO_REAL equ   _LABEL_PREPARE_GO_BACK_TO_REAL - $$   
  190.     mov ax,Selector_Normal   
  191.     mov ds,ax   
  192.     mov es,ax   
  193.     mov ss,ax   
  194.     mov gs,ax   
  195.     mov fs,ax   
  196.        
  197.     mov eax,cr0   
  198.     and eax,7ffffffeh   
  199.     mov cr0,eax   
  200.        
  201. LABEL_GO_BACK_TO_REAL:   
  202.     jmp 0:LABEL_ALREADY_REAL   
  203.        
  204. LABEL_ALREADY_REAL:   
  205.     mov ax,cs   
  206.     mov ds,ax   
  207.     mov es,ax   
  208.     mov ss,ax   
  209.     mov sp,[w_SP_Value_In_Real_Mode]   
  210.     mov fs,ax   
  211.     mov gs,ax   
  212.        
  213.     in  al,92h   
  214.     and al,11111101b   
  215.     out 92h,al   
  216.        
  217.     mov al,[_Real_Mode_IMR_Value]   
  218.     out 21h,al   
  219.     lidt    [_Real_Mode_IDT_Value]   
  220.     sti   
  221.        
  222.     mov ax,4c00h   
  223.     int 21h   
  224.        
  225. GDT_Code16_Len  equ $ - $$   
  226.      
  227. [section .s32]       
  228. [bits 32]       
  229. LABEL_CODE32:       
  230.     mov ax,Selector_Stack32   
  231.   mov   ss,ax   
  232.   mov   esp,Stack_Len   
  233.      
  234.     mov ax,Selector_Video   
  235.   mov   gs,ax   
  236.      
  237.   mov   ax,Selector_Data   
  238.   mov   ds,ax   
  239.   mov   es,ax   
  240.      
  241.   call  Disp_Memory_Info   
  242.   
  243.     call    Paging_Demo   
  244.        
  245.     call    Init_8259A   
  246.        
  247.     int 80h   
  248.     sti   
  249.        
  250.     mov ecx,90000000h   
  251.        
  252. .1:   
  253.     dec ecx   
  254.     cmp ecx,0   
  255.     jne .1   
  256.        
  257.     cli   
  258.        
  259.     call    Set_Real_Mode_8259A   
  260.      
  261.     jmp Selector_Code16:LABEL_PREPARE_GO_BACK_TO_REAL    
  262.        
  263. Init_8259A:   
  264.     mov al,11h   
  265.     out 20h,al   
  266.     call    Delay   
  267.        
  268.     out 0a0h,al   
  269.     call    Delay   
  270.        
  271.     mov al,20h   
  272.     out 21h,al   
  273.     call    Delay   
  274.        
  275.     mov al,28h   
  276.     out 0a1h,al   
  277.     call    Delay   
  278.        
  279.     mov al,4   
  280.     out 21h,al   
  281.     call    Delay   
  282.        
  283.     mov al,2   
  284.     out 0a1h,al   
  285.     call    Delay   
  286.        
  287.     mov al,1   
  288.     out 21h,al   
  289.     call    Delay   
  290.        
  291.     out 0a1h,al   
  292.     call    Delay   
  293.        
  294.     mov al,11111110b   
  295.     out 21h,al   
  296.     call    Delay   
  297.        
  298.     mov al,11111111b   
  299.     out 0a1h,al   
  300.     call    Delay   
  301.        
  302.     ret   
  303.        
  304. Set_Real_Mode_8259A:   
  305.     mov al, 017h   
  306.     out 020h, al       
  307.     call    Delay   
  308.   
  309.     mov al, 008h       
  310.     out 021h, al       
  311.     call    Delay   
  312.   
  313.     mov al, 001h   
  314.     out 021h, al       
  315.     call    Delay   
  316.   
  317.     mov al, [ds:Real_Mode_IMR_Value]       
  318.     out 021h, al           
  319.     call    Delay   
  320.        
  321.     ret   
  322.        
  323. Delay:   
  324.     nop   
  325.     nop   
  326.     nop   
  327.     nop   
  328.     ret   
  329.   
  330. Paging_Demo:   
  331.     mov ax,Selector_Code32   
  332.     mov ds,ax   
  333.   mov   ax,Selector_Flat_RW   
  334.   mov   es,ax   
  335.     
  336.   push  Paging_Demo_Proc_Len   
  337.   push  Proc_Paging_Demo   
  338.   push  Paging_Demo_Proc   
  339.   call  Memory_Copy   
  340.   add   esp,12   
  341.      
  342.   push  A_Proc_Len   
  343.   push  Proc_A   
  344.   push  A_Proc   
  345.   call  Memory_Copy   
  346.   add   esp,12   
  347.      
  348.   push  B_Proc_Len   
  349.   push  Proc_B   
  350.   push  B_Proc   
  351.   call  Memory_Copy   
  352.   add   esp,12   
  353.      
  354.   mov   ax,Selector_Data   
  355.   mov   ds,ax   
  356.   mov   ax,Selector_Flat_RW   
  357.   mov   es,ax   
  358.      
  359.   call  Setup_Paging   
  360.      
  361.   call  Selector_Flat_C:Proc_Paging_Demo   
  362.   call  Page_Switch   
  363.   call  Selector_Flat_C:Proc_Paging_Demo   
  364.      
  365.     ret   
  366.        
  367. Disp_Memory_Info:   
  368.     push    eax   
  369.     push    ecx   
  370.     push    edx   
  371.     push    esi   
  372.     push    edi   
  373.        
  374.     push    sz_Memory_Info_Title   
  375.     call    Disp_Str   
  376.     add esp,4   
  377.     call    Disp_Return   
  378.        
  379.     mov ecx,[d_Memory_Info_Num]   
  380.     mov esi,Memory_Info_Buffer   
  381.        
  382. .1:   
  383.     mov edx,5   
  384.     mov edi,ARDStruct   
  385.        
  386. .2:   
  387.     push    dword [esi]   
  388.     call    Disp_Int   
  389.     pop eax   
  390.     stosd   
  391.     add esi,4   
  392.     call Disp_Space   
  393.     call Disp_Space   
  394.     dec edx   
  395.     cmp edx,0   
  396.     jne .2   
  397.        
  398.     cmp dword [d_Type],1   
  399.     jne .3   
  400.     mov eax,[d_Base_Addr_Low]   
  401.     add eax,[d_Length_Low]   
  402.     cmp eax,[d_Ram_Size]   
  403.     jb  .3   
  404.     mov [d_Ram_Size],eax   
  405.        
  406. .3:    
  407.     call    Disp_Return   
  408.     loop .1   
  409.        
  410.     push    sz_Ram_Size   
  411.     call    Disp_Str   
  412.     add esp,4   
  413.        
  414.     push    dword [d_Ram_Size]   
  415.     call    Disp_Int   
  416.     add esp,4   
  417.        
  418.     pop edi   
  419.     pop esi   
  420.     pop edx   
  421.     pop ecx   
  422.     pop eax   
  423.     ret   
  424.        
  425. Setup_Paging:   
  426.     push    eax   
  427.     push    ebx   
  428.     push    ecx   
  429.     push    edx   
  430.     push    edi   
  431.        
  432.     xor edx,edx   
  433.   mov   eax,[d_Ram_Size]   
  434.   mov   ebx,1024 * 1024 * 4   
  435.   div   ebx   
  436.   test  edx,edx   
  437.   jz    .skip   
  438.   inc   eax   
  439. .skip:   
  440.     mov [d_Page_Table_Num],eax   
  441.     mov ecx,eax   
  442.     mov eax,Page_Tbl_Base1 + 7h   
  443.     mov edi,Page_Dir_Base1   
  444.        
  445. .1:   
  446.     stosd   
  447.     add eax,4096   
  448.     loop .1   
  449.        
  450.     mov eax,[d_Page_Table_Num]   
  451.     mov ebx,1024   
  452.     mul ebx   
  453.     mov ecx,eax   
  454.     mov eax,7h   
  455.     mov edi,Page_Tbl_Base1   
  456.        
  457. .2:   
  458.     stosd   
  459.     add eax,4096   
  460.     loop .2   
  461.        
  462.     mov eax,Page_Dir_Base1   
  463.     mov cr3,eax   
  464.        
  465.     mov eax,cr0   
  466.     or  eax,80000000h   
  467.     mov cr0,eax   
  468.        
  469.     pop edi   
  470.     pop edx   
  471.     pop ecx   
  472.     pop ebx   
  473.     pop eax   
  474.   ret   
  475.      
  476. Page_Switch:   
  477.     push    eax   
  478.     push    ebx   
  479.     push    ecx   
  480.     push    edx   
  481.     push    edi   
  482.        
  483.     mov ecx,[d_Page_Table_Num]   
  484.     mov eax,Page_Tbl_Base2 + 7h   
  485.     mov edi,Page_Dir_Base2   
  486.   
  487. .1:   
  488.     stosd   
  489.     add eax,4096   
  490.     loop    .1   
  491.        
  492.     mov eax,[d_Page_Table_Num]   
  493.     mov bx,1024   
  494.     mul bx   
  495.     mov ecx,eax   
  496.     mov eax,7h   
  497.     mov edi,Page_Tbl_Base2   
  498.   
  499. .2:   
  500.     stosd   
  501.     add eax,4096   
  502.     loop .2   
  503.   
  504.     mov eax,Linear_Addr   
  505.     shr eax,22   
  506.     and eax,3ffh   
  507.     mov bx,4   
  508.     mul bx   
  509.     mov ecx,eax   
  510.     mov ecx,[es:Page_Dir_Base2 + ecx]   
  511.     and ecx,0fffffc00h   
  512.        
  513.     mov eax,Linear_Addr   
  514.     shr eax,12   
  515.     and eax,3ffh   
  516.        
  517.     mov bx,4   
  518.     mul bx   
  519.     add ecx,eax   
  520.        
  521.     mov dword [es:ecx],Proc_B + 7h   
  522.     mov edx,[es:ecx]   
  523.        
  524.     mov eax,Page_Dir_Base2   
  525.     mov cr3,eax   
  526.   
  527.     pop edi   
  528.   pop   edx   
  529.   pop   ecx   
  530.   pop   ebx   
  531.   pop   eax   
  532.     ret   
  533.        
  534. _Paging_Demo_Proc:   
  535. Paging_Demo_Proc    equ _Paging_Demo_Proc - $$   
  536.     mov eax,Linear_Addr   
  537.     call    eax   
  538.        
  539.     retf   
  540.        
  541. Paging_Demo_Proc_Len    equ $ - _Paging_Demo_Proc      
  542.        
  543. _A_Proc:   
  544. A_Proc  equ _A_Proc - $$   
  545.     mov ah,0ch   
  546.     mov al,'A'  
  547.     mov [gs:160 * 16],ax   
  548.     ret   
  549.        
  550. A_Proc_Len  equ $ - _A_Proc   
  551.   
  552. _B_Proc:   
  553. B_Proc  equ _B_Proc - $$   
  554.     mov ah,0ch   
  555.     mov al,'B'  
  556.     mov [gs:160 * 17],ax   
  557.     ret   
  558.        
  559. B_Proc_Len  equ $ - _B_Proc    
  560.   
  561. _General_Handler:   
  562. General_Handler equ _General_Handler - $$   
  563.     mov ah,0ch   
  564.     mov al,'I'  
  565.     mov [gs:(0 + 70) * 2],ax   
  566.        
  567.     iretd   
  568.        
  569. _Clock_Handler:   
  570. Clock_Handler   equ _Clock_Handler - $$   
  571.     inc byte [gs:(0 + 70) * 2]   
  572.     mov al,20h   
  573.     out 20h,al   
  574.        
  575.     iretd   
  576.        
  577. %include "lib.inc"  
  578.   
  579. GDT_Code32_Len  equ $ - $$  

     运行结果略。

原创粉丝点击