首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > 汇编语言 >

自己动手写操作系统中的有关问题

2012-08-09 
自己动手写操作系统中的问题随书附带源代码用nasm编译时出现这样的错误:nasm :fatal:assertion addr s-

自己动手写操作系统中的问题
随书附带源代码用nasm编译时出现这样的错误:
nasm :fatal:assertion addr <=s->start failed at output/outbin.c:146
当时由于我自己写的代码,成功编译,也没有太过在意。可是当第二天我给自己写的代码加描述符特权级时,同样的问题出现了。
没有加特权级之前一切正常,编译显示都OK,但是在
selectordataequLABEL_DESC_DATA-LABEL_GDT这里+SA_RPL3之后显示了同样的错误。实在不明白怎么一回事!
下面是修改前的代码:
%include "pm.inc"
org0100h
jmp LABEL_BEGIN
[SECTION .gdt]
LABEL_GDT:Descriptor0,0,0
LABEL_DESC_LDT:Descriptor 0,LDTLEN-1,DA_LDT
LABEL_NORMAL:Descriptor0,0ffffh,DA_DRW
LABEL_DESC_CODE32:Descriptor0, SEGCODE32LEN-1,DA_C+DA_32
LABEL_DESC_CODE16: Descriptor0,0ffffh,DA_C
LABEL_DESC_DATA:Descriptor0,DATALEN-1,DA_DRW
LABEL_DESC_STACK:Descriptor0,TOPOFSTACK,DA_DRWA+DA_32
LABEL_DESC_VIDEO:Descriptor0B8000H,0ffffh,DA_DRW
;GDT

gdtlen equ $-LABEL_GDT
gdtptr dw gdtlen-1
dd 0

selectorldtequLABEL_DESC_LDT-LABEL_GDT
selectornomal equ LABEL_NORMAL-LABEL_GDT
selectorcode32 equLABEL_DESC_CODE32-LABEL_GDT
selectorcode16equLABEL_DESC_CODE16-LABEL_GDT
selectordataequLABEL_DESC_DATA-LABEL_GDT
selectorstackequ LABEL_DESC_STACK-LABEL_GDT
selectortestequLABEL_DESC_TEST-LABEL_GDT
selectorvideoequLABEL_DESC_VIDEO-LABEL_GDT
;GDT选择子

[SECTION .data1]
ALIGN 32
[BITS 32]
LABEL_DATA:
inreal_proctect dw 0
pmmassage: db "In Protect Mode now. ^-^",0
offsetpmmassage equ pmmassage - $$
strtest:db"ABCDEFGHIJKLMNOPQRSTUVWXYZ",0
offsetstrtestequstrtest - $$
DATALENequ$ - LABEL_DATA

[SECTION .gs]
ALIGN 32 
[BITS 32]
LABEL_STACK:
times 512 db0
TOPOFSTACKequ$ - LABEL_STACK - 1

[SECTION .s16]
[BITS 16]
LABEL_BEGIN:
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax

mov sp,0100h

mov [back_real+3],ax
mov [inreal_proctect],sp

mov ax,cs
movzx eax,ax
shl eax,4
add eax,seg_code16
mov word[LABEL_DESC_CODE16+2],ax
shr eax,16
mov byte[LABEL_DESC_CODE16+4],al
mov byte[LABEL_DESC_CODE16+7],ah

xor eax,eax
mov ax,cs
shl eax,4
add eax,seg_code32
mov word[LABEL_DESC_CODE32+2],ax
shr eax,16
mov byte[LABEL_DESC_CODE32+4],al
mov byte[LABEL_DESC_CODE32+7],ah

xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_DATA
mov word[LABEL_DESC_DATA+2],ax
shr eax,16
mov byte[LABEL_DESC_DATA+4],al
mov byte[LABEL_DESC_DATA+7],ah

xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_STACK
mov word[LABEL_DESC_STACK+2],ax
shr eax,16
mov byte[LABEL_DESC_STACK+4],al
mov byte[LABEL_DESC_STACK+7],ah

xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_LDT
mov word[LABEL_DESC_LDT+2],ax
shr eax,16
mov byte[LABEL_DESC_LDT+4],al
mov byte[LABEL_DESC_LDT+7],ah

xor eax,eax
mov ax,ds
shl eax,4
add eax,seg_ldt_codea
mov word[LABEL_LDT_DESC_CODEA+2],ax
shr eax,16
mov byte[LABEL_LDT_DESC_CODEA+4],al
mov byte[LABEL_LDT_DESC_CODEA+7],ah

xor eax,eax
mov ax,ds
shl eax,4
add eax,LABEL_GDT
mov dword[gdtptr+2],eax

lgdt [gdtptr]

cli

in al,92h
or al,00000010b
out 92h,al

mov eax,cr0
or eax,1
mov cr0,eax

jmpdword selectorcode32:0

back_inreal_proctect:
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax

mov sp,[inreal_proctect]

in al,92h
and al,11111101b
out 92h,al

sti

mov ax,4c00h
int 21h

[SECTION .s32]
[BITS 32]
seg_code32:
mov ax,selectordata
mov ds,ax
mov ax,selectorstack
mov ss,ax

mov ax,selectorvideo
mov gs,ax



mov esp,TOPOFSTACK

mov ah,0ch
xor esi,esi
xor edi,edi
mov esi,offsetpmmassage 

mov edi,(80*10+0)*2 
cld

.1 :
lodsb 
test al,al
jz .2
mov [gs:edi],ax
add edi,2
jmp .1
.2:
call dispreturn

mov ax,selectorldt
lldtax
jmp selectorldtcodea:0


dispreturn:
push eax
push edx
mov eax,edi
mov dl,160
div dl
and eax,0ffh 
inc eax
mov dl,160
mul dl
mov edi,eax
pop edx
pop eax
ret
SEGCODE32LEN equ $-seg_code32



[SECTION .s16]
ALIGN32
[BITS 16]
seg_code16:
mov ax,selectornomal
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax

mov eax,cr0
and al,11111110b
mov cr0,eax

back_real:
jmp 0:back_inreal_proctect

SEGCODE16LEN equ $-seg_code16

[SECTION .ldt]
ALIGN 32
LABEL_LDT:
LABEL_LDT_DESC_CODEA:Descriptor0,LDTCODEALEN-1,DA_C+DA_32
;LDT
LDTLEN EQU $-LABEL_LDT

selectorldtcodea equLABEL_LDT_DESC_CODEA-LABEL_LDT+SA_TIL
;LDT选择子

[SECTION .la] 
ALIGN 32
[BITS 32]
seg_ldt_codea:
mov ax,selectorvideo
mov gs,ax

mov edi,(80*12+0)*2
mov ah,0ch
mov al,'L'
mov [gs:edi],ax


jmp selectorcode16:0
LDTCODEALEN equ $-seg_ldt_codea


修改后只是在

LABEL_DESC_DATA:Descriptor0,DATALEN-1,DA_DRW+DA_PDL1(表示特权级为1)

selectordataequLABEL_DESC_DATA-LABEL_GDT+SA_RDL3(请求特权级为3,这里刚开始时设置为1也无法编译)





[解决办法]
pm.inc 中的宏参数在引用时要加括号, 这是作者的一个疏忽!
如:
%macro 1
 mov eax, (%1)<<8&&0xffff
%macroend

就是那个%1要加括号,否则就会出错!我在学习时也遇到这个问题,找了好久!

热点排行