【PM复习】保护模式下的一点迷你应用(下)
来源:互联网 发布:淘宝店铺手机端网址 编辑:程序博客网 时间:2024/05/29 03:53
上一节中的与显示有关的函数已经写好了,下面来继续写关于读和写的代码。我们打算在5M地址处读和写,那么必须在GDT中添加一个数据段:
- LABEL_DESC_TEST:
- Descriptor 50000h,0ffffh,DA_DRW
添加选择子:
- Selector_Test equ LABEL_DESC_TEST - LABEL_DESC_DUMMY
接下来是读测试函数:
- Read_Test:
- push ebp
- mov ebp,esp
- push ecx
- push esi
- mov ecx,8
- xor esi,esi
- .loop:
- mov al,[fs:esi]
- call Disp_Al
- inc esi
- loop .loop
- pop esi
- pop ecx
- pop ebp
- ret
代码很简单,要注意的一点是调用此函数前要把fs赋为指向测试段的选择子。
下面是写测试函数:
- Write_Test:
- push ebp
- mov ebp,esp
- push eax
- push ecx
- push esi
- xor eax,eax
- mov eax,'A'
- xor esi,esi
- mov ecx,8
- .loop:
- mov [fs:esi],al
- inc esi
- inc eax
- loop .loop
- pop esi
- pop ecx
- pop eax
- pop ebp
- ret
这个函数也很简单,没什么可说的,下面是全部代码:
- %include "pm.inc"
- org 0100h
- jmp LABEL_BEGIN
- [section .gdt]
- LABEL_DESC_DUMMY:
- Descriptor 0,0,0
- LABEL_DESC_CODE32:
- Descriptor 0,0ffffh,DA_C | DA_32
- LABEL_DESC_CODE16:
- Descriptor 0,0ffffh,DA_C
- LABEL_DESC_VIDEO:
- Descriptor 0b8000h,0ffffh,DA_DRW
- LABEL_DESC_DATA:
- Descriptor 0,0ffffh,DA_DRW
- LABEL_DESC_TEST:
- Descriptor 50000h,0ffffh,DA_DRW
- LABEL_DESC_STACK32:
- Descriptor 0,Stack_Len - 1,DA_DRW | DA_32
- LABEL_DESC_NORMAL:
- Descriptor 0,0ffffh,DA_DRW
- GDT_Len equ $ - LABEL_DESC_DUMMY
- GDT_Ptr:
- dw GDT_Len - 1
- dd 0
- Selector_Code32 equ LABEL_DESC_CODE32 - LABEL_DESC_DUMMY
- Selector_Code16 equ LABEL_DESC_CODE16 - LABEL_DESC_DUMMY
- Selector_Video equ LABEL_DESC_VIDEO - LABEL_DESC_DUMMY
- Selector_Data equ LABEL_DESC_DATA - LABEL_DESC_DUMMY
- Selector_Test equ LABEL_DESC_TEST - LABEL_DESC_DUMMY
- Selector_Stack32 equ LABEL_DESC_STACK32 - LABEL_DESC_DUMMY
- Selector_Normal equ LABEL_DESC_NORMAL - LABEL_DESC_DUMMY
- [section .stack32]
- [bits 32]
- LABEL_STACK32:
- times 512 db 0
- Stack_Len equ $ - $$
- [section .data]
- LABEL_DATA:
- _d_Disp_Pos dd 160 * 5
- d_Disp_Pos equ _d_Disp_Pos - $$
- _w_SP_Value_In_Real_Mode dw 0
- w_SP_Value_In_Real_Mode equ _w_SP_Value_In_Real_Mode - $$
- [section .s16]
- [bits 16]
- LABEL_BEGIN:
- mov ax,cs
- mov ds,ax
- mov es,ax
- mov ax,ss
- mov sp,0100h
- mov [LABEL_GO_BACK_TO_REAL + 3],ax
- Fill_Descriptor LABEL_DESC_CODE32,LABEL_CODE32
- Fill_Descriptor LABEL_DESC_CODE16,LABEL_BEGIN
- Fill_Descriptor LABEL_DESC_DATA,LABEL_DATA
- Fill_Descriptor LABEL_DESC_STACK32,LABEL_STACK32
- xor eax,eax
- mov ax,ds
- shl eax,4
- add eax,LABEL_DESC_DUMMY
- mov dword [GDT_Ptr + 2],eax
- lgdt [GDT_Ptr]
- mov [w_SP_Value_In_Real_Mode],sp
- cli
- in al,92h
- or al,00000010b
- out 92h,al
- mov eax,cr0
- or al,1
- mov cr0,eax
- jmp Selector_Code32:0
- _LABEL_PREPARE_GO_BACK_TO_REAL:
- LABEL_PREPARE_GO_BACK_TO_REAL equ _LABEL_PREPARE_GO_BACK_TO_REAL - $$
- mov ax,Selector_Normal
- mov ds,ax
- mov es,ax
- mov ss,ax
- mov sp,[w_SP_Value_In_Real_Mode]
- mov gs,ax
- mov fs,ax
- mov eax,cr0
- and al,11111110b
- mov cr0,eax
- LABEL_GO_BACK_TO_REAL:
- jmp 0:LABEL_ALREADY_REAL
- LABEL_ALREADY_REAL:
- in al,92h
- and al,11111101b
- out 92h,al
- sti
- mov ax,4c00h
- int 21h
- [section .s32]
- [bits 32]
- LABEL_CODE32:
- ;mov ax,Selector_Video
- ;mov gs,ax
- ;mov ah,0ch
- ;mov al,'x'
- ;mov [gs:80 * 10],ax
- mov ax,Selector_Stack32
- mov ss,ax
- mov esp,Stack_Len
- mov ax,Selector_Data
- mov ds,ax
- mov ax,Selector_Video
- mov gs,ax
- mov ax,Selector_Test
- mov fs,ax
- call Read_Test
- call Disp_Return
- call Write_Test
- call Read_Test
- jmp Selector_Code16:LABEL_PREPARE_GO_BACK_TO_REAL
- Read_Test:
- push ebp
- mov ebp,esp
- push ecx
- push esi
- mov ecx,8
- xor esi,esi
- .loop:
- mov al,[fs:esi]
- call Disp_Al
- inc esi
- loop .loop
- pop esi
- pop ecx
- pop ebp
- ret
- Write_Test:
- push ebp
- mov ebp,esp
- push eax
- push ecx
- push esi
- xor eax,eax
- mov eax,'A'
- xor esi,esi
- mov ecx,8
- .loop:
- mov [fs:esi],al
- inc esi
- inc eax
- loop .loop
- pop esi
- pop ecx
- pop eax
- pop ebp
- ret
- Disp_Al:
- push ebp
- mov ebp,esp
- push esi
- push ecx
- push eax
- mov ecx,2
- shr al,4
- .loop:
- and al,0fh
- cmp al,9
- jb .1
- add al,7
- .1:
- add al,30h
- mov esi,[d_Disp_Pos]
- mov byte [gs:esi],al
- mov byte [gs:esi + 1],0ch
- add dword [d_Disp_Pos],2
- dec ecx
- cmp ecx,0
- je .2
- pop eax
- jmp .loop
- .2:
- pop ecx
- pop esi
- pop ebp
- ret
- Disp_Return:
- push ebp
- mov ebp,esp
- push eax
- push ebx
- mov eax,[d_Disp_Pos]
- mov bl,160
- div bl
- and ax,0ffh
- inc ax
- mov bl,160
- mul bl
- mov [d_Disp_Pos],eax
- pop ebx
- pop eax
- pop ebp
- ret
运行结果如下:
- 【PM复习】保护模式下的一点迷你应用(下)
- 【PM复习】保护模式下的一点迷你应用(上)
- 保护模式下的应用
- 保护模式下的应用
- 【PM复习】进入保护模式
- 保护模式下的应用1
- 【PM复习】进入保护模式改进
- 【PM复习】进入保护模式再改进
- 【PM复习】从保护模式切换回实模式
- 保护模式下的RPL(一)
- 特权级在保护模式下的应用
- 特权级在保护模式下的应用
- 特权级在保护模式下的应用
- 特权级在保护模式下的应用
- 保护模式下的寻址
- 保护模式下寻址
- 保护模式下寻址
- 保护模式下寻址
- 【PM复习】保护模式下的一点迷你应用(上)
- JMenu
- actionPerformed
- ASP.net
- 方法不是外在的形式
- 【PM复习】保护模式下的一点迷你应用(下)
- smarty模板
- 看郭克华老师的视频教学笔记(二)
- 关于三个概念:ActiveX、OLE和COM
- 使用PHP的gd库出现问题
- Java编码规范
- 【致歉】关于《Java WEB开发实战经典》推出的说明
- 拒绝平庸
- 方法不是外在的形式,而是内容的灵魂