4G Memory Access (谁能帮我改下,能在VC6.0下编译通过)

qq506664095 2008-12-18 02:04:12
2.4.1 程序的意义
此程序具有如下功能:

Ø 不需要在保护模式状态下就可以直接把386的4GB内存读出来;

Ø 利用此程序可直接在DOS中做物理设备的检测;

Ø 理解GDT表的对应关系后,所谓386 32位模式也就很容易理解;

Ø 在DOS下,可根据此类方法将中断向量表移到任意位置,达到反跟踪或其他等目的。

2.4.2 程序代码
程序代码如下所示。

#include <dos.h>

////////////////////////////////////////////////////////////////

// 4G Memory Access

// This Program Can Access 4G Bytes in DOS Real

//Mode,Needn't in Protection Mode It Works.

// The Program Enter 32 Bit Flat Mode a moment and

//Only Load FS a 32 Bit Flat Mode Selector,Then Return

//Real Mode.

// Used The FS Can Access All 4G Memory till It be

//reloaded.

//

///////////////////////////////////////////////////////////////



unsigned long GDT_Table[]=

{ 0, 0, //NULL - 00H

0x0000FFFF, 0x00CF9A00, //Code32 - 08H Base=0

//Limit=4G-1 Size=4G

0x0000FFFF, 0x00CF9200 //Data32 - 10H Base=0

//Limit=4G-1 Size=4G

};



//Save The IDTR before Enter Protect Mode.

unsigned char OldIDT[6]={0};



//NULL The IDTR,IDTR's Limit=0 will disable all

//Interrupts,include NMI.

unsigned char pdescr_tmp[6]={0};



#define KeyWait() {while(inportb(0x64)&2);}

void A20Enable(void)

{

KeyWait();

outportb(0x64,0xD1);

KeyWait();

outportb(0x60,0xDF); //Enable A20 with 8042.

KeyWait();

outportb(0x64,0xFF);

KeyWait();

}





void LoadFSLimit4G(void)

{

A20Enable(); //Enable A20



//**************************************

//* Disable ints & Null IDT *

//**************************************

asm {

CLI //Disable inerrupts

SIDT OldIDT //Save OLD IDTR

LIDT pdescr_tmp //Set up empty IDT.Disable any

//interrupts,

} //Include NMI.

//***************************************

//* Load GDTR *

//***************************************

asm {



//The right Code is Real,But BC++'s Linker NOT Work

//with 32-bits Code.

db 0x66 //32 bit Operation Prefix in 16 Bit DOS.

MOV CX,DS //MOV ECX,DS

db 0x66 //Get Data segment physical Address

SHL CX,4 //SHL ECX,4

MOV word ptr pdescr_tmp[0],(3*8-1)

//MOV word ptr pdescr_tmp[0],(3*8-1)

db 0x66

XOR AX,AX //XOR EAX,EAX

MOV AX,offset GDT_Table

//MOV AX,offset GDT_Table

db 0x66

ADD AX,CX //ADD EAX,ECX

MOV word ptr pdescr_tmp[2],AX

//GDTR Base high16 bits

db 0x66

SHR AX,16 //SHR EAX,16

MOV word ptr pdescr_tmp[4],AX

//GDTR Base high16 bits

LGDT pdescr_tmp //Load GDTR

}

//**************************************

//* Enter 32 bit Flat Protected Mode *

//**************************************

// Set CR0 Bit-0 to 1 Enter 32 Bit Protection

//Mode,And NOT Clear machine perform cache,It Meaning

//the after Code HAD Ready To RUN in 32 Bit Flat Mode,

//Then Load Flat Selector to FS and Description into it's

//Shadow register,After that,ShutDown Protection Mode

//And ReEnter Real Mode immediately.

// The FS holds Base=0 Size=4G Description and

//it can Work in Real Mode as same as Pretect Mode,

//untill FS be reloaded.

// In that time All the other Segment Registers are

//Not Changed,except FS.(They are ERROR Value holded in CPU).

asm {

MOV DX,0x10 //The Data32 Selector

db 0x66,0x0F,0x20,0xC0 //MOV EAX,CR0

db 0x66

MOV BX,AX //MOV EBX,EAX

OR AX,1

db 0x66,0x0F,0x22,0xC0 //MOV CR0,EAX

//Set Protection enable bit

JMP Flush

} //Clear machine perform cache.

Flush: //Now In Flat Mode,But The

//CS is Real Mode Value.

asm { //And it's attrib is 16-Bit Code

//Segment.

db 0x66

MOV AX,BX //MOV EAX,EBX

db 0x8E,0xE2 //MOV FS,DX //Load FS now

db 0x66,0x0F,0x22,0xC0

//MOV CR0,EAX

//Return Real Mode.Now FS's Base=0 Size=4G

LIDT OldIDT

//LIDT OldIDT Restore IDTR

STI //STI Enable INTR

}

}

//With FS can Access All 4G Memory Now.But if FS be reloaded

//in Real Mode It's Limit will Be Set to FFFFh(Size=64K),

//then Can not used it

// to Access 4G bytes Memory Again,Because FS is Segment:Offset

//Memory type after that.

//If Use it to Access large than 64K will generate Execption 0D.

//unsigned char ReadByte(unsigned long Address)

{

asm db 0x66

asm mov di,word ptr Address //MOV EDI,Address

asm db 0x67 //32 bit Address Prefix

asm db 0x64 //FS:

asm mov al,byte ptr [BX] //=MOV AL,FS:[EDI]

return _AL;

}

unsigned char WriteByte(unsigned long Address)

{

asm db 0x66

asm mov di,word ptr Address //MOV EDI,Address

asm db 0x67 //32 bit Address Prefix

asm db 0x64 //FS:

asm mov byte ptr [BX],al //=MOV FS:[EDI],AL

return _AL;

}

///////////////// Don't Touch Above Code /////////////

#include <stdio.h>

/////////////////////////////////////////////////////////////

//打印出Address指向的内存中的数据

///////////////////////////////////////////////////////////

void Dump4G(unsigned long Address)

{

int i;

int j;

for(i=0;i<20;i++)

{

printf("%08lX: ",(Address+i*16));

for(j=0;j<16;j++)

printf("%02X ",ReadByte(Address+i*16+j));

printf(" ");

for(j=0;j<16;j++)

{

if(ReadByte(Address+i*16+j)<0x20) printf(".");

else printf("%c",ReadByte(Address+i*16+j));

}

printf("\n");

}

}

main()

{

char KeyBuffer[256];

unsigned long Address=0;

unsigned long tmp;



LoadFSLimit4G();

printf("====Designed By Southern.1995.7.17====\n");

printf("Now you can Access The Machine All 4G Memory.\n");

printf("Input the Start Memory Physical to DUMP.\n");

printf("Press D to Cuntinue DUMP,0 to End & Quit.\n");

do {

printf("-");

gets(KeyBuffer);

sscanf(KeyBuffer,"%lX",&tmp);

if(KeyBuffer[0]=='q') break;

if(KeyBuffer[0]=='d') Address+=(20*16);

else Address=tmp;

Dump4G(Address);

}while(Address!=0);

return 0;

}

程序运行后,等用户从键盘输入一个字符。当输入“Q”字符时,整个程序将退出,当输入“D”时,将在屏幕上显示一屏内存的数据,最左边为绝对地址,其后一列显示的是以十六进制位表示的内存的数据,后一列是数据所对应的ASCII码。

我要使上面的程序能在VC6.0编译通过,要怎么改?

还有:

#define KeyWait() {while(inportb(0x64)&2);}

的inportb是做什么用的?


...全文
234 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mLee79 2008-12-18
  • 打赏
  • 举报
回复
inportb == _inp , 从一个端口读一个字节...
vc6 就表想了,跑不了的...

内容概要:本文围绕“新型电力系统下多分布式电源接入配电网承载力评估方法”的研究,系统性地介绍了基于Matlab的仿真建模与代码实现方案,旨在评估高比例分布式电源(如光伏、风电等)接入背景下配电网的接纳能力。研究融合了智能优化算法(如蜣螂优化、灰狼优化、遗传算法)、多目标优化、鲁棒优化及双层优化模型,结合潮流计算、稳定性分析与故障仿真,构建了完整的承载力评估体系。文档不仅提供核心算法实现,还拓展至微电网调度、储能配置、电氢耦合系统、电动汽车协同等前沿方向,强调“复现+创新”相结合的科研路径,助力研究者快速掌握高水平论文复现技巧并激发原创思路。; 适合人群:具备电力系统、自动化或相关专业背景,熟悉Matlab/Simulink仿真环境,正在从事科研或工程应用的研究生及初级科研人员(工作1-3年);; 使用场景及目标:①复现高水平期刊中关于配电网承载力的优化模型;②开展高比例可再生能源接入下的配电网规划与运行研究;③学习并应用智能优化算法解决复杂电力系统问题;④获取完整科研资源包以加速课题进展与论文撰写; 阅读建议:建议读者关注公众号“荔枝科研社”获取网盘资源,下载全套代码与模型文件,按照文档结构循序渐进学习,重点理解算法设计逻辑与仿真建模细节,结合所提供的复现案例深化对优化模型与工程应用场景的理解,提升科研效率与创新能力。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧