主要加入set_8259A代码
代码如下:
#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)
/*
*InitDescrptor(Descriptor,SegBase)初始化描述符函数
*Descriptor:要初始化的描述符
*SegBase:段基址
*/
#define InitDescrptor(Descriptor,SegBase)\
xor %eax,%eax; \
mov %cs,%ax ; \
shl $4,%eax ; \
addl $(SegBase), %eax ;\
movw %ax, (Descriptor + 2);\
shr $16, %eax;\
movb %al, (Descriptor + 4);\
movb %ah, (Descriptor + 7)
DA_32=0x4000 //32位模式
DA_LIMIT_4K=0x8000 //颗粒度为4096
DA_DRW=0x92 //数据段可读可写
DA_CR=0x9A //可读可执行
DA_C = 0x98
SETUPSEG = 0x9020
SETUPAddr = SETUPSEG<<4
.text
.globl start/*程序从start处开始运行*/
.code16
start:
jmp code
msg:
.string "I'm setup!"
/**-----------------------------------------------------------------
* Global Descriptor Table: GDT
*-------------------------------*/
GDT_START:
Descriptor_DUMMY:Descriptor(0x0,0x0,0x0)
Descriptor_CODE32 :Descriptor(SETUPAddr,0xffffffff,DA_C+DA_32)
Descriptor_VIDEO:Descriptor(0xb8000,0x0ffff,DA_DRW)
Descriptor_SYSTM :Descriptor(0x10000,0xffffffff,DA_C+DA_32)
GDT_END:
GdtPtr:
.word (GDT_END-GDT_START)-1 # so does gdt
.long GDT_START+SETUPAddr # This will be rewrite by code.
.set selector_Code32,(Descriptor_CODE32-GDT_START)
.set Selector_Video,(Descriptor_VIDEO-GDT_START)
.set selector_System,(Descriptor_SYSTM-GDT_START)
code:
mov %cs,%ax
mov %ax,%ds
mov %ax,%es
mov %ax,%ss
mov $0x400,%sp
call DispStr/*调用显示字符串函数*/
//关中断
cli
/*初始全局描述符Descriptor_CODE32*/
InitDescrptor(Descriptor_CODE32,LABEL_SEG_CODE32);
/*加载gdtr即将全局描述符表gdt的首地址和gdt的界限赋给gdtr寄存器*/
lgdt GdtPtr
//打开地址线A20
// inb $0x92,%al
// or $0x02,%al
// outb %al,$0x92
call empty_8042//测试8042状态寄存器,等待输入缓冲区空
movb $0xd1,%al//0xd1表示要写数据岛8042的P2端口,P2端口的位1用于A20线的选通
out %al,$0x64
call empty_8042//数据要写到0x60端口
movb $0xdf,%al
out %al,$0x60
call empty_8042
call set_8259A
//设置cr0寄存器,切换到保护模式
// movl %cr0,%eax
// or $1,%eax
// movl %eax,%cr0
mov $0x0001,%ax
lmsw %ax
/*真正进入保护模式,执行此命令后CS=0x8,IP=LABEL_SEG_CODE32的偏移地址*/
//ljmp $0x18,$(LABEL_SEG_CODE32)
ljmp $selector_Code32,$0
/*此时CS:IP=全局描述符表中第1(0x8>>3)项描述符给出的段基址+LABEL_SEG_CODE32的偏移地址*/
LABEL_SEG_CODE32:
.align 32
.code32
nop
nop
movw $0x10,%ax
movw %ax,%gs
movl $((80*11+79)*2),%edi/*第11行,79列*/
movb $0x0c,%ah/*高四位表示黑底,低四位表示红字*/
movb $'P',%al/*显示的字符*/
movw %ax,%gs:(%edi)
ljmp $0x18,$0
loop0:/*无限循环*/
jmp loop0
DispStr:
.align 16
.code16
mov $msg ,%ax
mov %ax ,%bp/*es:bp = 串地址*/
mov $10 ,%cx/*cs = 串长度*/
mov $0x1301,%ax/*ah=13是功能号表示显示字符串 ,al=01是显示输出方式*/
mov $0x000c,%bx/*bh=0是0页,bl=0ch高亮 黑底红字*/
mov $0x0 ,%dl/*0行0列*/
mov $0x1 ,%dh
int $0x10
ret
empty_8042:
.align 16
.code16
.word 0x00eb,0x00eb//这是两个跳转指令的机器码(跳的下一句),相当于延时操作。
in $0x64,%al//读AT键盘控制器状态寄存器
test $2,%al//测试位1,输入缓冲区满?
jnz empty_8042
ret
set_8259A:
movb $0x11,%al//0x11表示边沿触发,多片级联,需要ICW4
out %al,$0x20//主
.word 0x00eb,0x00eb
out %al,$0xA0//从
.word 0x00eb,0x00eb
//系统硬件终端号被设置成从0x20开始
movb $0x20,%al//
out %al,$0x21
.word 0x00eb,0x00eb
mov $0x28,%al
out %al,$0xA1
.word 0x00eb,0x00eb
movb $0x04,%al
out %al,$0x21
.word 0x00eb,0x00eb
movb $0x02,%al
out %al,$0xA1
.word 0x00eb,0x00eb
movb $0x01,%al
out %al,$0x21
.word 0x00eb,0x00eb
out %al,$0xA1
.word 0x00eb,0x00eb
movb $0xff,%al
out %al,$0x21
.word 0x00eb,0x00eb
out %al,$0xA1
ret
.org 0x7fe, 0x90 //让setup占4个扇区,共512*4字节
.word 0xaa55