【30天自制操作系统】day05:结构体、文字显示与 GDT/IDT 初始化

输出一个 16 行 8 列的点阵字符

void putfont8(char *vram, int xsize, int x, int y, char c, char *font)
{
	int i;
	char *p, d /* data */;
	for (i = 0; i < 16; i++) {
		p = vram + (y + i) * xsize + x;
		d = font[i];
		if ((d & 0x80) != 0) { p[0] = c; }
		if ((d & 0x40) != 0) { p[1] = c; }
		if ((d & 0x20) != 0) { p[2] = c; }
		if ((d & 0x10) != 0) { p[3] = c; }
		if ((d & 0x08) != 0) { p[4] = c; }
		if ((d & 0x04) != 0) { p[5] = c; }
		if ((d & 0x02) != 0) { p[6] = c; }
		if ((d & 0x01) != 0) { p[7] = c; }
	}
	return;
}

 

GDT 初始化

  • GDTR寄存器(48位):低16位表示段上限(0xffff - 64KB),高32位表示GDT的开始地址(0x270000)
_load_gdtr:		; void load_gdtr(int limit, int addr);
		MOV		AX,[ESP+4]		; limit
		MOV		[ESP+6],AX
		LGDT	[ESP+6]
		RET
  • 段信息(64位):20位段大小 + 32位段起始地址 + 12位段属性
struct SEGMENT_DESCRIPTOR {
	short limit_low, base_low;
	char base_mid, access_right;
	char limit_high, base_high;
};

void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar)
{
	if (limit > 0xfffff) {
		ar |= 0x8000; /* G_bit = 1 */
		limit /= 0x1000;
	}
	sd->limit_low    = limit & 0xffff;
	sd->base_low     = base & 0xffff;
	sd->base_mid     = (base >> 16) & 0xff;
	sd->access_right = ar & 0xff;
	sd->limit_high   = ((limit >> 16) & 0x0f) | ((ar >> 8) & 0xf0);
	sd->base_high    = (base >> 24) & 0xff;
	return;
}
  • 段属性(12位):GD000000xxxxxxxx
    • 0x00:未使用的记录表
    • 0x92:系统专用,可读、可写、不可执行(内核态)
    • 0x9a:系统专用,可执行、可读、不可写(内核态)
    • 0xf2:应用程序专用,可读、可写、不可执行(用户态)
    • 0xfa:应用程序专用,可执行、可读、不可写(用户态)

 

posted @ 2019-10-07 21:47  闪客sun  阅读(712)  评论(0编辑  收藏  举报