【PM复习】保护模式下的一点迷你应用(下)

来源:互联网 发布:淘宝店铺手机端网址 编辑:程序博客网 时间:2024/05/29 03:53

     上一节中的与显示有关的函数已经写好了,下面来继续写关于读和写的代码。我们打算在5M地址处读和写,那么必须在GDT中添加一个数据段:

Code:
  1. LABEL_DESC_TEST:   
  2.     Descriptor  50000h,0ffffh,DA_DRW  

     添加选择子:

Code:
  1. Selector_Test       equ LABEL_DESC_TEST - LABEL_DESC_DUMMY  

     接下来是读测试函数:

Code:
  1. Read_Test:   
  2.     push    ebp   
  3.     mov ebp,esp   
  4.     push    ecx   
  5.     push    esi   
  6.        
  7.     mov ecx,8   
  8.     xor esi,esi   
  9.   
  10. .loop:   
  11.     mov al,[fs:esi]   
  12.     call    Disp_Al   
  13.     inc esi   
  14.     loop    .loop   
  15.        
  16.     pop esi   
  17.     pop ecx   
  18.     pop ebp   
  19.     ret  

     代码很简单,要注意的一点是调用此函数前要把fs赋为指向测试段的选择子。

     下面是写测试函数:

Code:
  1. Write_Test:   
  2.     push    ebp   
  3.     mov ebp,esp   
  4.     push    eax   
  5.     push    ecx   
  6.     push    esi   
  7.        
  8.     xor eax,eax   
  9.     mov eax,'A'  
  10.     xor esi,esi   
  11.     mov ecx,8   
  12.   
  13. .loop:   
  14.     mov [fs:esi],al   
  15.     inc esi   
  16.     inc eax   
  17.     loop .loop   
  18.        
  19.     pop esi   
  20.     pop ecx   
  21.     pop eax   
  22.     pop ebp   
  23.     ret  

     这个函数也很简单,没什么可说的,下面是全部代码:

Code:
  1. %include "pm.inc"  
  2.   
  3. org 0100h       
  4. jmp LABEL_BEGIN       
  5.       
  6. [section .gdt]       
  7. LABEL_DESC_DUMMY:       
  8.     Descriptor  0,0,0     
  9. LABEL_DESC_CODE32:    
  10.     Descriptor  0,0ffffh,DA_C | DA_32     
  11. LABEL_DESC_CODE16:   
  12.     Descriptor  0,0ffffh,DA_C   
  13. LABEL_DESC_VIDEO:       
  14.     Descriptor  0b8000h,0ffffh,DA_DRW   
  15. LABEL_DESC_DATA:   
  16.     Descriptor  0,0ffffh,DA_DRW   
  17. LABEL_DESC_TEST:   
  18.     Descriptor  50000h,0ffffh,DA_DRW   
  19. LABEL_DESC_STACK32:    
  20.     Descriptor  0,Stack_Len - 1,DA_DRW | DA_32   
  21. LABEL_DESC_NORMAL:   
  22.     Descriptor  0,0ffffh,DA_DRW   
  23.            
  24. GDT_Len equ $ - LABEL_DESC_DUMMY       
  25. GDT_Ptr:       
  26.     dw  GDT_Len - 1       
  27.     dd  0       
  28.       
  29. Selector_Code32 equ LABEL_DESC_CODE32 - LABEL_DESC_DUMMY       
  30. Selector_Code16 equ LABEL_DESC_CODE16 - LABEL_DESC_DUMMY   
  31. Selector_Video  equ LABEL_DESC_VIDEO - LABEL_DESC_DUMMY     
  32. Selector_Data       equ LABEL_DESC_DATA - LABEL_DESC_DUMMY     
  33. Selector_Test       equ LABEL_DESC_TEST - LABEL_DESC_DUMMY   
  34. Selector_Stack32 equ    LABEL_DESC_STACK32 - LABEL_DESC_DUMMY   
  35. Selector_Normal equ LABEL_DESC_NORMAL - LABEL_DESC_DUMMY   
  36.   
  37. [section .stack32]   
  38. [bits 32]   
  39. LABEL_STACK32:   
  40.     times 512 db 0   
  41. Stack_Len   equ $ - $$   
  42.   
  43. [section .data]   
  44. LABEL_DATA:   
  45. _d_Disp_Pos dd  160 * 5   
  46. d_Disp_Pos  equ _d_Disp_Pos - $$   
  47. _w_SP_Value_In_Real_Mode    dw  0   
  48. w_SP_Value_In_Real_Mode equ _w_SP_Value_In_Real_Mode - $$   
  49.       
  50. [section .s16]       
  51. [bits 16]       
  52. LABEL_BEGIN:       
  53.     mov ax,cs       
  54.   mov ds,ax       
  55.   mov es,ax       
  56.   mov   ax,ss   
  57.   mov   sp,0100h   
  58.      
  59.   mov   [LABEL_GO_BACK_TO_REAL + 3],ax   
  60.            
  61.   Fill_Descriptor   LABEL_DESC_CODE32,LABEL_CODE32   
  62.   Fill_Descriptor   LABEL_DESC_CODE16,LABEL_BEGIN   
  63.   Fill_Descriptor   LABEL_DESC_DATA,LABEL_DATA   
  64.   Fill_Descriptor   LABEL_DESC_STACK32,LABEL_STACK32   
  65.            
  66.   xor eax,eax       
  67.   mov ax,ds       
  68.   shl eax,4       
  69.   add eax,LABEL_DESC_DUMMY       
  70.   mov dword [GDT_Ptr + 2],eax       
  71.            
  72.   lgdt    [GDT_Ptr]       
  73.      
  74.   mov   [w_SP_Value_In_Real_Mode],sp   
  75.            
  76.   cli       
  77.            
  78.   in  al,92h       
  79.   or  al,00000010b       
  80.   out 92h,al       
  81.            
  82.   mov eax,cr0       
  83.   or  al,1       
  84.   mov cr0,eax       
  85.            
  86.   jmp Selector_Code32:0       
  87.        
  88. _LABEL_PREPARE_GO_BACK_TO_REAL:   
  89. LABEL_PREPARE_GO_BACK_TO_REAL equ   _LABEL_PREPARE_GO_BACK_TO_REAL - $$   
  90.     mov ax,Selector_Normal   
  91.     mov ds,ax   
  92.     mov es,ax   
  93.     mov ss,ax   
  94.     mov sp,[w_SP_Value_In_Real_Mode]   
  95.     mov gs,ax   
  96.     mov fs,ax   
  97.        
  98.     mov eax,cr0   
  99.     and al,11111110b   
  100.     mov cr0,eax   
  101.        
  102. LABEL_GO_BACK_TO_REAL:   
  103.     jmp 0:LABEL_ALREADY_REAL   
  104.        
  105. LABEL_ALREADY_REAL:   
  106.     in  al,92h   
  107.     and al,11111101b   
  108.     out 92h,al   
  109.        
  110.     sti   
  111.        
  112.     mov ax,4c00h   
  113.     int 21h   
  114.       
  115. [section .s32]       
  116. [bits 32]       
  117. LABEL_CODE32:       
  118.   ;mov ax,Selector_Video       
  119.   ;mov gs,ax       
  120.   ;mov ah,0ch       
  121.   ;mov al,'x'      
  122.   ;mov [gs:80 * 10],ax       
  123.      
  124.   mov   ax,Selector_Stack32   
  125.   mov   ss,ax   
  126.   mov   esp,Stack_Len   
  127.      
  128.   mov   ax,Selector_Data   
  129.   mov   ds,ax   
  130.   mov   ax,Selector_Video   
  131.   mov   gs,ax   
  132.   mov   ax,Selector_Test   
  133.   mov   fs,ax   
  134.      
  135.   call  Read_Test   
  136.   call  Disp_Return   
  137.   call  Write_Test   
  138.   call  Read_Test   
  139.      
  140.   jmp   Selector_Code16:LABEL_PREPARE_GO_BACK_TO_REAL    
  141.      
  142. Read_Test:   
  143.     push    ebp   
  144.     mov ebp,esp   
  145.     push    ecx   
  146.     push    esi   
  147.        
  148.     mov ecx,8   
  149.     xor esi,esi   
  150.   
  151. .loop:   
  152.     mov al,[fs:esi]   
  153.     call    Disp_Al   
  154.     inc esi   
  155.     loop    .loop   
  156.        
  157.     pop esi   
  158.     pop ecx   
  159.     pop ebp   
  160.     ret   
  161.        
  162. Write_Test:   
  163.     push    ebp   
  164.     mov ebp,esp   
  165.     push    eax   
  166.     push    ecx   
  167.     push    esi   
  168.        
  169.     xor eax,eax   
  170.     mov eax,'A'  
  171.     xor esi,esi   
  172.     mov ecx,8   
  173.   
  174. .loop:   
  175.     mov [fs:esi],al   
  176.     inc esi   
  177.     inc eax   
  178.     loop .loop   
  179.        
  180.     pop esi   
  181.     pop ecx   
  182.     pop eax   
  183.     pop ebp   
  184.     ret   
  185.   
  186. Disp_Al:   
  187.     push    ebp   
  188.     mov ebp,esp   
  189.        
  190.     push    esi   
  191.     push    ecx   
  192.     push    eax   
  193.     mov ecx,2   
  194.     shr al,4   
  195.        
  196. .loop:   
  197.     and al,0fh   
  198.     cmp al,9   
  199.     jb  .1   
  200.     add al,7   
  201.        
  202. .1:   
  203.     add al,30h   
  204.     mov esi,[d_Disp_Pos]   
  205.     mov byte [gs:esi],al   
  206.     mov byte [gs:esi + 1],0ch   
  207.     add dword [d_Disp_Pos],2   
  208.        
  209.     dec ecx   
  210.     cmp ecx,0   
  211.     je  .2   
  212.     pop eax   
  213.     jmp .loop   
  214.        
  215. .2:   
  216.     pop ecx   
  217.     pop esi   
  218.     pop ebp   
  219.     ret   
  220.        
  221. Disp_Return:   
  222.     push    ebp   
  223.     mov ebp,esp   
  224.     push    eax   
  225.     push    ebx   
  226.        
  227.     mov eax,[d_Disp_Pos]   
  228.     mov bl,160   
  229.     div bl   
  230.     and ax,0ffh   
  231.     inc ax   
  232.     mov bl,160   
  233.     mul bl   
  234.     mov [d_Disp_Pos],eax   
  235.        
  236.     pop ebx   
  237.     pop eax   
  238.     pop ebp   
  239.     ret   

     运行结果如下:

 

原创粉丝点击