1、打开分页机制即设置cr0的PG位为1,代码如下(在SetupPaging子程序中):
mov eax, cr0 ; ┓
or eax, 80000000h ; ┣ 设置cr0的PG=1,打开分页机制
mov cr0, eax ; ┛
2、在返回实模式的过渡16位代码段[SECTION .s16code]中需要关闭分页机制即cr0的PG设置为0,同时设置cr0的PE位设置为0。代码如下(在代码段[SECTION .s16code]中):
mov eax, cr0
and al, 11111110b ;应改为 and eax, 7ffffffeh ;关闭分页机制、进入保护模式
mov cr0, eax
如果按照红色标注的代码(也就是原来的代码)运行程序,会出现下面的“栈”错误。
image
---------------------
作者:zhenyongyuan123
来源:CSDN
原文:https://blog.csdn.net/zhenyongyuan123/article/details/5743267
版权声明:本文为博主原创文章,转载请附上博文链接!
上述错误的原因在Intel手册中有说明:
PG Paging (bit 31 of CR0). Enables paging when set; disables paging when clear. When
paging is disabled, all linear addresses are treated as physical addresses. The PG flag
has no effect if the PE flag (bit 0 of register CR0) is not also set; in fact, setting the PG
flag when the PE flag is clear causes a general-protection exception (#GP) to be gener-
ated.
也就是说当CR0的PE标志为0,但是PG标志不为0,则会产生错误