China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-13 07:09
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » Who can explain the detailed explanation of the following code? View 1,751 Replies 1
Original Poster Posted 2010-01-26 17:04 ·  中国 北京 联通
初级用户
Credits 54
Posts 17
Joined 2009-11-27 12:03
16-year member
UID 155470
Gender Male
Status Offline
The following will tell you how to access 4GB memory in real mode. This technology requires the support of protected mode, so it can only run on CPUs above 80386. Readers who have learned a little about protected mode know that the content in the segment address register in protected mode is no longer the base address of the segment like in real mode, but just an index in the descriptor table. The real information of the segment (base address, limit, access rights, etc.) is placed in the descriptor table. When accessing a piece of data, the CPU will take out the segment description information from the descriptor table to check whether the access is legal. If it is illegal, an exception will be generated. If it is legal, access is allowed. Reading the descriptor information and checking each time is a relatively time-consuming process. In order to improve the speed of memory access, Intel has equipped a high-speed buffer for each segment register in the CPU to store the segment descriptor information. In this way, when accessing memory, there is no need to frequently access the descriptor table, and only need to perform verification from the high-speed buffer. Only when the value of the segment register is changed will the descriptor table be accessed to load the new segment descriptor into the high-speed buffer. We use this feature of the CPU to achieve our purpose. First, enter protected mode, set a certain segment register to a base address of 0H and a limit of 4GB, and then return to real mode. In this way, you can directly access 4GB memory through this segment register (actually, you can only access all the memory on your machine, not 4GB)! There is also one thing to note: you must open the A20 line, otherwise... don't blame me for not warning you in advance!

The following lists the required code:

========================================================

Make4GBSegment MACRO _seg
local MyGdt,PM_Service,Old_GDTR,GDTR,Real_Service,MyGdt
local _Exit
Push DS
Push ES
Pushad
Pushfd ; Save the scene
Sub EBX,EBX
Mov BX,CS
Mov DS,BX
Shl EBX,4
Push EBX
Rol EBX,8
Mov BYTE Ptr MyGdt[8+7],BL
Mov BL,BYTE Ptr MyGdt[8+5]
Ror EBX,8
Mov Dword Ptr MyGdt[8+2],EBX
Pop EBX
lea EBX,[EBX+MyGdt]
Mov DWORD Ptr [GDTR+2],EBX
Mov WORD Ptr [GDTR],31 ; Establish new GDTR
Cli
Sgdt FWORD Ptr [Old_GDTR] ; Save the old GDTR
Lgdt FWORD Ptr [GDTR] ; Set new GDTR
Mov EBX,CR0
Or BL,1
Mov CR0,EBX ; Enter protected mode
db 0eah
DW PM_Service
DW 8 ; Jump to protected mode code execution



PM_Service:
Mov AX,16
Mov _seg,AX
Mov EBX,CR0
And EBX,0fffffffeh
Mov CR0,EBX
DB 0eah
DW Real_Service
DW seg Real_Service


Real_Service:
Lgdt FWORD Ptr [Old_GDTR]
Popfd ; Restore the scene
Popad
Pop ES
Pop DS
Jmp _Exit
MyGdt DQ 0
DW -1,0,9a00h,0
DW -1,0,9200h,0cfh
DQ 0
Old_GDTR DW 0,0,0
GDTR DW 0,0,0


_Exit:
Endm

========================================================

Here, for the convenience of me, I only change FS to a 4GB segment. You can decide which segment register to use according to your needs. Just copy this code into your program, and then call it at the beginning, and you can directly access large memory through this segment register. Cool, right! Finally, one more thing must be noted: if there are any extended memory management programs (HIMEM, EMM386, etc.) when your program is running, be very careful, because it is very easy to damage their internal data or other programs' data. If this is the case, there is only one way to crash. Remember, remember! My suggestion is to better use extended memory from the top of memory. At this time, the possibility of damaging other data is smaller.
===========================================
These are some materials I found, but I don't understand the code very well. The function of this program has been given. I hope some expert can explain it in detail. It would be better if there are comments for each line
Floor 2 Posted 2010-03-28 12:54 ·  中国 广东 深圳 罗湖区 天威有线宽带(关内)
初级用户
Credits 24
Posts 11
Joined 2008-08-21 15:25
17-year member
UID 123780
Gender Male
Status Offline
Maybe most friends here are not familiar with using instructions in protected mode and have fewer practical opportunities. After all, it's too easy to crash. Try to post the unclear snippets to the BIOSREN forum. Those who work on BIOS there are familiar with this aspect and may be able to get help.
Forum Jump: