head.S代码如下:
#define Descriptor(base,lim,attr)\
.word lim&0xffff;\
.word base&0xffff;\
.byte (base>>16)&0xff;\
.word ((lim>>8)&0xf00)|(attr&0x0f0ff);\
.byte ((base>>24)&0xff)
#define Gate(Selector,Offset,PCount,Attr)\
.2byte (Offset&0xffff);\
.2byte (Selector);\
.2byte (PCount&0x1f)|((Attr<<8)&0xff00);\
.2byte ((Offset>>16)&0xffff)
DA_32=0x4000 //32位模式
DA_LIMIT_4K=0x8000 //颗粒度为4096
DA_DRW=0x92 //数据段可读可写
DA_CR=0x9A //可读可执行
DA_C = 0x98
SETUPSEG = 0x9020
SETUPAddr = SETUPSEG<<4
DA_386CGate = 0x8c
DA_386IGate = 0x8E
DA_PL0 = 0x00
.text
.globl start/*程序从start处开始运行*/
.code32
start:
movl $0x20,%eax
mov %ax,%ds //因为ds是数据段寄存器,所以要加载一个数据段,否则会发生异常(可以试试0x08描述符)
mov %ax,%es
mov %ax,%ss
mov $0xffff,%esp
/*加载gdtr即将全局描述符表gdt的首地址和gdt的界限赋给gdtr寄存器*/
lgdt GdtPtr
lidt IdtPtr
movl $0x20,%eax
mov %ax,%ds
mov %ax,%es
mov %ax,%ss
mov $0xffff,%esp
ljmp $0x8,$LABEL_SEG_CODE32
LABEL_SEG_CODE32:
.align 32
.code32
movw $0x10,%ax
movw %ax,%gs
movl $((80*12+79)*2),%edi/*第12行,79列*/
movb $0x0c,%ah/*高四位表示黑底,低四位表示红字*/
movb $'S',%al/*显示的字符*/
movw %ax,%gs:(%edi)
int $0x80
loop1:/*无限循环*/
jmp loop1
.align 4
INT_TEST:
.code32
movl $((80*13+79)*2),%edi/*第11行,79列*/
movb $0x0c,%ah/*高四位表示黑底,低四位表示红字*/
movb $'I',%al/*显示的字符*/
movw %ax,%gs:(%edi)
loop2:/*无限循环*/
jmp loop2
ret
.align 4
GDT_START:
Descriptor_DUMMY:Descriptor(0x0,0x0,0x0)
Descriptor_CODE32 :Descriptor(0x0,0xffffffff,DA_C+DA_32)
Descriptor_VIDEO:Descriptor(0xb8000,0x0ffff,DA_DRW)
Descriptor_SYSTM :Descriptor(0x00000,0xffffffff,DA_C+DA_32)
Descriptor_SYSTM2 :Descriptor(0x00000,0xffffffff,DA_DRW)
GDT_END:
GdtPtr:
.word (GDT_END-GDT_START)-1 # so does gdt
.long GDT_START # This will be rewrite by code.
.align 4
LABEL_IDT:
.align 32
.code32
.rep 255
Gate(0x08,(INT_TEST - start),0,(DA_386IGate+DA_PL0))
.endr
END_IDT:
.set Idtlen,(END_IDT - LABEL_IDT)
IdtPtr:
.word (END_IDT-LABEL_IDT)-1 # so does gdt
.long LABEL_IDT # This will be rewrite by code.