How to directly access 4GB of memory in real mode

zhaozj2021-02-16  32

Programming under DOS Because of the limit of real mode, the maximum can only access 1M byte memory space, even if the 65520 byte that can be accessed after the opening A20 is opened, only the zone 1088k is only, and there is still a large area in such memory. Used by the operating system, BIOS, and TSR and other programs, it can really use the space to use to be very poor. Fortunately, the development of computers provides some ways to solve this problem. The most common method has the following: 1. Effove the size of the program, try to avoid using too many memory once (no mistakes? I am just an initiator, not an algorithm expert.) 2. Use the file coverage technology, only When you use the specified code, you read it in memory. After the execution is released (even how slow it? Hard disk mad ...) 3. Standard DOS extended technology such as EMS, XMS, DPMI (Wow! The function is strong, but the program is changed, it is tired) 4. Write the protection mode platform (who wants to do this, don't forget to send me a source) 5. Transfer to other platforms such as Windows / UNIX / Linux (please , I just want to run down in DOS) The compression effect is also good, I can't reach more than 50%, the program uses 2M or even 10M memory? File coverage needs to write a scheduling module yourself, and because it is frequently read and write hard drives, it will lead to the speed of the program. EMS, XMS, DPMI power, strong compatibility, especially the DPMI specification, not only support large memory access, but also Run the code in the protection mode and exceed 64KB of segment address restrictions, it is a first choice when writing large programs. However, EMS, XMS can only use extended memory as a high-speed hard drive, all access can only be done through a series of interrupt calls, which is not suitable for programs that often require small size memory access, and they are subject to The 64KB address space of the real model is not very convenient for big data volume; there is certainly no such limitation with DPMI, but it needs to override all the procedures, which is also a trouble. Write the protection mode platform (sweat! ... I don't think there are a few people to get it, I will not do it even if I don't do it). As for transferring to other platforms, you don't have to say it. Seeing this, the reader can't help but ask: "There is no way to say that it is not like this?" In fact, it is not the case, each method has its advantages and shortcomings, to see your needs to decide Which method is. Ok, there is so much nonsense, and if you don't cut into the topic, it is estimated that someone will throw eggs to me. Let's tell you how to access 4GB of memory in real mode. This technology requires protection mode support, so it can only be run in the CPU over 80,386 or more. Readers who have learned a certain protection model know that the baseline of the segment in the lower address register in the protection mode is only the baseline of the segment, but only the index in the table, the real information of the segment (base address, limited Long, access rights, etc.) In the descriptor table, the CPU will check if the description of the descriptor table is legal, and if it is not legal, it is allowed to generate an abnormality when accessing a data. Each access is to read the descriptor information and check is a relatively time-consuming process. In order to improve the speed of memory access, Intel is equipped with a high-speed buffer for each segment register in the CPU to store the descriptor information of the segment. This way to access the memory is not used frequently without frequently accessing the description, as long as the check is verified from the cache, only the descriptor table is accessed when the value of the segment register is changed, and the new segment descriptor is loaded into the cache. We use the CPU's feature to reach our goal.

First enter the protection mode, set a segment register to the base site, limited to 4GB, and then return the real mode. This allows you to directly access 4GB of memory through this segment register (actually only access to all the memory on your machine is not 4GB)! There is also something to pay attention to, you must open the A20 line, otherwise ... Don't blame me not to pre-! Codes are listed below require: Make4GBSegment MACRO _seglocal MyGdt, PM_Service, Old_GDTR, GDTR, Real_Service, MyGdtlocal _Exit Push DS Push ES Pushad Pushfd; protect the site 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; Create a new GDTR CLI SGDT FWORD PTR [OLD_GDTR]; Save Old GDTR LGDT FWORD PTR [GDTR]; set new GDTR MOV EBX, CR0 or BL, 1 MOV CR0, EBX; Enter Protection Mode DB 0EAH DW PM_Service DW 8; Jump to Protection Mode Code Perform 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_SERVICEREAL_S ERVICE: LGDT fword PTR [OLD_GDTR] POPFD; Restore Scene Popad Pop ES POP DS JMP _EXITMYGDT DQ 0 DW-1, 0, 9A00H, 0 DW -1, 0, 9200H, 0CFH DQ 0OLD_GDTR DW 0, 0, 0GDTR DW 0, 0, 0_exit: Endm Here, I only change fs to 4GB segments, readers can decide which segment registers you need to use by yourself.

转载请注明原文地址:https://www.9cbs.com/read-24559.html

New Post(0)