【PM复习】显示内存信息

     我贴。。。。:

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,GDT_Code32_Len - 1,DA_C + DA_32     
  11. LABEL_DESC_CODE16:   
  12.     Descriptor  0,GDT_Code16_Len - 1,DA_C   
  13. LABEL_DESC_VIDEO:       
  14.     Descriptor  0b8000h,0ffffh,DA_DRW   
  15. LABEL_DESC_DATA:   
  16.     Descriptor  0,Data_Len - 1,DA_DRW   
  17. LABEL_DESC_STACK32:    
  18.     Descriptor  0,Stack_Len - 1,DA_DRW + DA_32   
  19. LABEL_DESC_NORMAL:   
  20.     Descriptor  0,0ffffh,DA_DRW   
  21.            
  22. GDT_Len equ $ - LABEL_DESC_DUMMY       
  23. GDT_Ptr:       
  24.     dw  GDT_Len - 1       
  25.     dd  0       
  26.       
  27. Selector_Code32 equ LABEL_DESC_CODE32 - LABEL_DESC_DUMMY       
  28. Selector_Code16 equ LABEL_DESC_CODE16 - LABEL_DESC_DUMMY   
  29. Selector_Video  equ LABEL_DESC_VIDEO - LABEL_DESC_DUMMY     
  30. Selector_Data       equ LABEL_DESC_DATA - LABEL_DESC_DUMMY     
  31. Selector_Stack32 equ    LABEL_DESC_STACK32 - LABEL_DESC_DUMMY   
  32. Selector_Normal equ LABEL_DESC_NORMAL - LABEL_DESC_DUMMY   
  33.   
  34. [section .stack32]   
  35. [bits 32]   
  36. LABEL_STACK32:   
  37.     times 512 db 0   
  38. Stack_Len   equ $ - $$   
  39.   
  40. [section .data]   
  41. LABEL_DATA:   
  42. _d_Disp_Pos dd  160 * 5   
  43. d_Disp_Pos  equ _d_Disp_Pos - $$   
  44. _w_SP_Value_In_Real_Mode    dw  0   
  45. w_SP_Value_In_Real_Mode equ _w_SP_Value_In_Real_Mode - $$   
  46.   
  47. _sz_Memory_Info_Title   db 'BaseAddrL  BaseAddrH  LengthLow  LengthHigh  Type',0   
  48. sz_Memory_Info_Title    equ _sz_Memory_Info_Title - $$   
  49. _sz_Ram_Size    db 'RAM SIZE:',0   
  50. sz_Ram_Size equ _sz_Ram_Size - $$   
  51. _sz_Return  db  0ah,0   
  52. sz_Return equ   _sz_Return - $$   
  53. _sz_Space   db  20h,0   
  54. sz_Space    equ _sz_Space - $$   
  55.   
  56. _Memory_Info_Buffer times 256 db 0   
  57. Memory_Info_Buffer  equ _Memory_Info_Buffer - $$   
  58. _d_Memory_Info_Num  dd  0   
  59. d_Memory_Info_Num   equ _d_Memory_Info_Num - $$   
  60.   
  61. _ARDStruct:   
  62.     _d_Base_Addr_Low    dd  0   
  63.     _d_Base_Addr_High   dd  0   
  64.     _d_Length_Low   dd  0   
  65.     _d_Length_High  dd  0   
  66.     _d_Type dd  0   
  67.        
  68. ARDStruct   equ _ARDStruct - $$   
  69.     d_Base_Addr_Low equ _d_Base_Addr_Low    - $$   
  70.     d_Base_Addr_High    equ _d_Base_Addr_High   - $$   
  71.     d_Length_Low    equ _d_Length_Low - $$   
  72.     d_Length_High   equ _d_Length_High - $$   
  73.     d_Type  equ _d_Type - $$   
  74.        
  75. _d_Ram_Size dd  0   
  76. d_Ram_Size  equ _d_Ram_Size - $$   
  77.   
  78. Data_Len    equ $ - $$   
  79.       
  80. [section .s16]       
  81. [bits 16]       
  82. LABEL_BEGIN:       
  83.     mov ax,cs       
  84.   mov ds,ax       
  85.   mov es,ax       
  86.   mov   ax,ss   
  87.   mov   sp,0100h   
  88.      
  89.   mov   [LABEL_GO_BACK_TO_REAL + 3],ax   
  90.      
  91.   mov   ebx,0   
  92.   mov   di,_Memory_Info_Buffer   
  93.   mov   ecx,20   
  94.   mov   edx,0534d4150h   
  95.   
  96. .loop:   
  97.     mov eax,0e820h   
  98.   int   15h   
  99.   jc    LABEL_MEMORY_CHK_FAIL   
  100.   inc   dword [_d_Memory_Info_Num]   
  101.   add   di,20   
  102.   cmp   ebx,0   
  103.   jne   .loop   
  104.   jmp   LABEL_MEMORY_OK   
  105.      
  106. LABEL_MEMORY_CHK_FAIL:   
  107.     mov dword [_d_Memory_Info_Num],0   
  108. LABEL_MEMORY_OK:   
  109.            
  110.   Fill_Descriptor   LABEL_DESC_CODE32,LABEL_CODE32   
  111.   Fill_Descriptor   LABEL_DESC_CODE16,LABEL_BEGIN   
  112.   Fill_Descriptor   LABEL_DESC_DATA,LABEL_DATA   
  113.   Fill_Descriptor   LABEL_DESC_STACK32,LABEL_STACK32   
  114.            
  115.   xor eax,eax       
  116.   mov ax,ds       
  117.   shl eax,4       
  118.   add eax,LABEL_DESC_DUMMY       
  119.   mov dword [GDT_Ptr + 2],eax       
  120.            
  121.   lgdt  [GDT_Ptr]       
  122.      
  123.   mov   [w_SP_Value_In_Real_Mode],sp   
  124.            
  125.   cli       
  126.            
  127.   in  al,92h       
  128.   or  al,00000010b       
  129.   out 92h,al       
  130.            
  131.   mov eax,cr0       
  132.   or  al,1       
  133.   mov cr0,eax       
  134.            
  135.   jmp dword Selector_Code32:0       
  136.        
  137. _LABEL_PREPARE_GO_BACK_TO_REAL:   
  138. LABEL_PREPARE_GO_BACK_TO_REAL equ   _LABEL_PREPARE_GO_BACK_TO_REAL - $$   
  139.     mov ax,Selector_Normal   
  140.     mov ds,ax   
  141.     mov es,ax   
  142.     mov ss,ax   
  143.     mov sp,[w_SP_Value_In_Real_Mode]   
  144.     mov gs,ax   
  145.     mov fs,ax   
  146.        
  147.     mov eax,cr0   
  148.     and al,11111110b   
  149.     mov cr0,eax   
  150.        
  151. LABEL_GO_BACK_TO_REAL:   
  152.     jmp 0:LABEL_ALREADY_REAL   
  153.        
  154. LABEL_ALREADY_REAL:   
  155.     in  al,92h   
  156.     and al,11111101b   
  157.     out 92h,al   
  158.        
  159.     sti   
  160.        
  161.     mov ax,4c00h   
  162.     int 21h   
  163.        
  164. GDT_Code16_Len  equ $ - $$   
  165.       
  166.      
  167. [section .s32]       
  168. [bits 32]       
  169. LABEL_CODE32:       
  170.   mov   ax,Selector_Stack32   
  171.   mov   ss,ax   
  172.   mov   esp,Stack_Len   
  173.      
  174.   mov   ax,Selector_Data   
  175.   mov   ds,ax   
  176.   mov   es,ax   
  177.   mov   ax,Selector_Video   
  178.   mov   gs,ax   
  179.      
  180.   call  Disp_Memory_Info   
  181.      
  182.     jmp Selector_Code16:LABEL_PREPARE_GO_BACK_TO_REAL    
  183.        
  184. Disp_Memory_Info:   
  185.     push    eax   
  186.     push    ecx   
  187.     push    edx   
  188.     push    esi   
  189.     push    edi   
  190.        
  191.     push    sz_Memory_Info_Title   
  192.     call    Disp_Str   
  193.     add esp,4   
  194.     call    Disp_Return   
  195.        
  196.     mov ecx,[d_Memory_Info_Num]   
  197.     mov esi,Memory_Info_Buffer   
  198.        
  199. .1:   
  200.     mov edx,5   
  201.     mov edi,ARDStruct   
  202.        
  203. .2:   
  204.     push    dword [esi]   
  205.     call    Disp_Int   
  206.     pop eax   
  207.     stosd   
  208.     add esi,4   
  209.     call Disp_Space   
  210.     call Disp_Space   
  211.     dec edx   
  212.     cmp edx,0   
  213.     jne .2   
  214.        
  215.     cmp dword [d_Type],1   
  216.     jne .3   
  217.     mov eax,[d_Base_Addr_Low]   
  218.     add eax,[d_Length_Low]   
  219.     cmp eax,[d_Ram_Size]   
  220.     jb  .3   
  221.     mov [d_Ram_Size],eax   
  222.        
  223. .3:    
  224.     call    Disp_Return   
  225.     loop .1   
  226.        
  227.     push    sz_Ram_Size   
  228.     call    Disp_Str   
  229.     add esp,4   
  230.        
  231.     push    dword [d_Ram_Size]   
  232.     call    Disp_Int   
  233.     add esp,4   
  234.        
  235.     pop edi   
  236.     pop esi   
  237.     pop edx   
  238.     pop ecx   
  239.     pop eax   
  240.     ret   
  241.        
  242. Disp_Int:   
  243.     push    ebp   
  244.     mov ebp,esp   
  245.        
  246.     push    eax   
  247.     push    ecx   
  248.     push    edi   
  249.        
  250.     mov eax,[ebp + 8]   
  251.     mov ecx,4   
  252.        
  253. .1:   
  254.     rol eax,8   
  255.     call    Disp_Al   
  256.     loop .1   
  257.        
  258.     mov edi,[d_Disp_Pos]   
  259.     mov ah,7h   
  260.     mov al,'h'  
  261.     mov [gs:edi],ax   
  262.     add edi,2   
  263.     mov [d_Disp_Pos],edi   
  264.        
  265.     pop edi   
  266.     pop ecx   
  267.     pop eax   
  268.        
  269.     pop ebp   
  270.     ret    
  271.   
  272. Disp_Str:   
  273.     push    ebp   
  274.     mov ebp,esp   
  275.        
  276.     push    eax   
  277.     push    ebx   
  278.     push    esi   
  279.     push    edi   
  280.        
  281.     mov esi,[ebp + 8]   
  282.     mov edi,[d_Disp_Pos]   
  283.     mov ah,0fh   
  284.   
  285. .1:   
  286.     lodsb   
  287.     test    al,al   
  288.     jz  .2   
  289.     cmp al,0ah   
  290.     jne .3   
  291.     mov eax,edi   
  292.     mov bl,160   
  293.     div bl   
  294.     and ax,0ffh   
  295.     inc ax   
  296.     mov bl,160   
  297.     mul bl   
  298.     mov edi,eax   
  299.     jmp .1   
  300.        
  301. .3:   
  302.     mov [gs:edi],ax   
  303.     add edi,2   
  304.     jmp .1   
  305.        
  306. .2:   
  307.     mov [d_Disp_Pos],edi   
  308.        
  309.     pop edi   
  310.     pop esi   
  311.     pop ebx   
  312.     pop eax   
  313.        
  314.     pop ebp   
  315.     ret    
  316.   
  317. Disp_Al:   
  318.     push    ebp   
  319.     mov ebp,esp   
  320.        
  321.     push    esi   
  322.     push    ecx   
  323.     push    eax   
  324.     mov ecx,2   
  325.     shr al,4   
  326.        
  327. .3:   
  328.     and al,0fh   
  329.     cmp al,9   
  330.     jbe .1   
  331.     add al,7   
  332.        
  333. .1:   
  334.     add al,30h   
  335.     mov esi,[d_Disp_Pos]   
  336.     mov byte [gs:esi],al   
  337.     mov byte [gs:esi + 1],0fh   
  338.     add dword [d_Disp_Pos],2   
  339.        
  340.     dec ecx   
  341.     cmp ecx,0   
  342.     je  .2   
  343.     pop eax   
  344.     jmp .3   
  345.        
  346. .2:   
  347.     pop ecx   
  348.     pop esi   
  349.     pop ebp   
  350.     ret   
  351.        
  352. Disp_Return:   
  353.     push    sz_Return   
  354.     call    Disp_Str   
  355.     add esp,4   
  356.     ret   
  357.        
  358. Disp_Space:   
  359.     push    sz_Space   
  360.     call    Disp_Str   
  361.     add esp,4   
  362.     ret   
  363.   
  364. GDT_Code32_Len  equ $ - $$  

     运行结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值