分頁這一次又做了一次變換,似乎是成功了。
圖下面的程式碼,那個sprintf是剽別人的各位還是自己從網上下載吧
kernel.c
#include "sprintf.h"
void myhlt(void); void setup_page(void); void change_page(void); void change0(void); void fillbox(int color, int x0, int y0, int x1, int y1); void myfillbox(int color, int x0, int y0, int x1, int y1); void makefont16 (int color, int x, int y, short *font); void print_gb2312(int color, int x, int y, unsigned char *str); void copy_str(char *str, char *mystr);
int mymain(int eax, int ebx) {
int *myfb = (int *)0xe0000000; fillbox(0x001e90ff, 0, 0, 799, 599); myfillbox(0x00000000, 0, 0, 799, 599); unsigned char valuebuf[64]; unsigned char *str = "系統的名字叫做什麼呢,暫定為SnailOS吧,真的佩服自己的速度,像蝸牛爬樹!"; print_gb2312(0xffffffff, 80 , 0, str); str = "我的系統開始可以列印中文了,不過還不能換行,螢幕也是越界的,好在還是正確的顯示了!"; print_gb2312(0xffffffff, 80, 80, str); str = "蝸牛(Snial)"; print_gb2312(0xffffffff, 80 , 96, str); str = "該不該擱下重重的殼"; print_gb2312(0xffffffff, 80 , 112, str); str = "尋找到底哪裡有藍天"; print_gb2312(0xffffffff, 80 , 128, str); str = "隨著輕輕的風輕輕的飄"; print_gb2312(0xffffffff, 80 , 144, str); str = "歷經的傷都不感覺疼"; print_gb2312(0xffffffff, 80 , 160, str); str = "我要一步一步往上爬"; print_gb2312(0xffffffff, 80 , 176, str); str = "等待陽光靜靜看著它的臉"; print_gb2312(0xffffffff, 80 , 192, str); str = "小小的天有大大的夢想"; print_gb2312(0xffffffff, 80 , 208, str); str = "重重的殼裹著輕輕的仰望"; print_gb2312(0xffffffff, 80 , 224, str); str = "我要一步一步往上爬"; print_gb2312(0xffffffff, 80 , 240, str); str = "在最高點乘著葉片往前飛"; print_gb2312(0xffffffff, 80 , 256, str); str = "讓風吹乾流過的淚和汗"; print_gb2312(0xffffffff, 80 , 272, str); str = "總有一天我有屬於我的天"; print_gb2312(0xffffffff, 80 , 288, str);
str = "Hd#蝸牛先生#¥%……來&)(*_*)})。感謝Internet,感謝CSDN,感謝蒼老師^_^^_^"; print_gb2312(0xffffffff, 80 , 336, str); sprintf (valuebuf, "eax 等於 0x%x", eax); print_gb2312(0x00ff8c00, 80, 384, valuebuf); sprintf (valuebuf, "ebx 等於 0x%x", ebx); print_gb2312(0x00ffffff , 80, 368, valuebuf); char * addr0 = (char *) 0x7c00;
char *mystr = "在進行分頁實驗!"; copy_str(addr0, mystr); fillbox(0x001e90ff, 0, 0, 799, 599); myfillbox(0x00000000, 0, 0, 799, 599); str = "又做了一些改動,把第一頁對映到第七頁,借用小米的一句話,\"永遠相信美好的事情即將發生\""; print_gb2312(0xffffffff, 0, 0, str); print_gb2312(0xffffffff, 80, 80, addr0); setup_page(); print_gb2312(0x00ff8c00, 80, 96, addr0); mystr = "上面弄錯了,是把第0頁的對映到7頁,這次決定把7頁對映到0頁"; copy_str((char *)0x0, mystr); change_page(); print_gb2312(0x00ff0000, 80, 112, (char *)0xc00); char *addr1 = (char *)0xc00; *addr1 = 0xce; *(addr1 + 1) = 0xd2; print_gb2312(0x0000ff00, 80, 128, addr1); print_gb2312(0xffffffff, 80, 144, addr1);
print_gb2312(0xffffffff, 80, 160, (char *)0x7000); mystr = "地址變換後改變地址0處的內容,向0x7000寫入本段字元,則是向地址0處寫入"; copy_str((char *)0x7000, mystr); change0(); print_gb2312(0xffffffff, 80, 176, (char *)0); mystr = "再進行一次地址變換則向0x7000寫入和向0處寫入是一樣的"; copy_str((char *)0x7000, mystr); print_gb2312(0xffffffff, 80, 192, (char *)0); while(1) { myhlt(); } }
void myprintf(char * str) { return; }
void fillbox(int color, int x0, int y0, int x1, int y1) { int *myfb = (int *)0xe0000000; int xsize = 800; int x, y; for (y = 0; y <= y1 - y0; y++) { for (x = 0; x <= x1 - x0; x++) { myfb[x0 + x + (y0 + y) * xsize] = color; } } }
void myfillbox(int color, int x0, int y0, int x1, int y1) { int *myfb = (int *)0xe0000000; int xsize = 800; int x, y; if (x0 < 80 || y0 < 80 || x1 > 719 || y1 > 519) { x0 = 80; y0 = 80; x1 = 719; y1 = 519; } for (y = 0; y <= y1 - y0; y++) { for (x = 0; x <= x1 - x0; x++) { myfb[x0 + x + (y0 + y) * xsize] = color; } } }
void print_ascii (int color, int x, int y, char *font) { int *myfb = (int *)0xe0000000; int xsize = 800; char d; int *p; int i; for (i = 0; i < 16; i++){ p = myfb + (y + i) * xsize + x; d = font[i]; if ((d & 0x80) != 0) {p[0] = color;} if ((d & 0x40) != 0) {p[1] = color;} if ((d & 0x20) != 0) {p[2] = color;} if ((d & 0x10) != 0) {p[3] = color;} if ((d & 0x08) != 0) {p[4] = color;} if ((d & 0x04) != 0) {p[5] = color;} if ((d & 0x02) != 0) {p[6] = color;} if ((d & 0x01) != 0) {p[7] = color;} } return; }
void makefont16 (int color, int x, int y, short *font) { int *myfb = (int *)0xe0000000; int xsize = 800; short d; int *p; int i; for (i = 0; i < 16; i++){ p = myfb + (y + i) * xsize + x; d = font[i]; if ((d & 0x80) != 0) {p[0] = color;} if ((d & 0x40) != 0) {p[1] = color;} if ((d & 0x20) != 0) {p[2] = color;} if ((d & 0x10) != 0) {p[3] = color;} if ((d & 0x08) != 0) {p[4] = color;} if ((d & 0x04) != 0) {p[5] = color;} if ((d & 0x02) != 0) {p[6] = color;} if ((d & 0x01) != 0) {p[7] = color;} if ((d & 0x8000) != 0) {p[8] = color;} if ((d & 0x4000) != 0) {p[9] = color;} if ((d & 0x2000) != 0) {p[10] = color;} if ((d & 0x1000) != 0) {p[11] = color;} if ((d & 0x800) != 0) {p[12] = color;} if ((d & 0x400) != 0) {p[13] = color;} if ((d & 0x200) != 0) {p[14] = color;} if ((d & 0x100) != 0) {p[15] = color;} } return; }
void print_gb2312(int color, int x, int y, unsigned char *str) { extern short font[0x8000]; extern char myfont[0x1000]; int i = 0, j = 0; unsigned char a, b; unsigned int offset; while (str[i] != 0) { if (str[i] < 0x80) { a = str[i]; print_ascii (color, x + j, y, myfont + a * 16); if (str[i + 1] == 0) break; i++; j += 8; } else { a = str[i] - 0xa0; b = str[i + 1] - 0xa0; offset = ((a - 1) * 94 + (b - 1)) * 16; makefont16 (color, x + j , y, font + offset); i += 2; j += 16; } } return; }
void copy_str(char *str, char *mystr) { int i = 0; for (; i <= strnlen(mystr, 0); i++) *(str + i) = *(mystr + i); return; }
boot.s
MULTIBOOT2_HEADER_MAGIC = 0xe85250d6 GRUB_MULTIBOOT_ARCHITECTURE_I386 = 0x0 MULTIBOOT_HEADER_TAG_ADDRESS = 0x2 MULTIBOOT_HEADER_TAG_OPTIONAL = 0x1 MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 0x3 MULTIBOOT_HEADER_TAG_END = 0x0 MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 0x5
.globl _start, _myhlt, _setup_page, _change_page, _change0 .extern _mymain, _myprintf
_start: jmp multiboot_entry .align 8
multiboot_header: .int MULTIBOOT2_HEADER_MAGIC .int GRUB_MULTIBOOT_ARCHITECTURE_I386 .int multiboot_header_end - multiboot_header .int -(MULTIBOOT2_HEADER_MAGIC + GRUB_MULTIBOOT_ARCHITECTURE_I386 + (multiboot_header_end - multiboot_header))
.align 8 address_tag_start: .short MULTIBOOT_HEADER_TAG_ADDRESS .short MULTIBOOT_HEADER_TAG_OPTIONAL .int address_tag_end - address_tag_start .int multiboot_header .int _start .int 0x0 .int 0x0 address_tag_end:
.align 8
entry_address_tag_start: .short MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS .short MULTIBOOT_HEADER_TAG_OPTIONAL .int entry_address_tag_end - entry_address_tag_start .int multiboot_entry entry_address_tag_end:
.align 8
framebuffer_tag_start: .short MULTIBOOT_HEADER_TAG_FRAMEBUFFER .short MULTIBOOT_HEADER_TAG_OPTIONAL .int framebuffer_tag_end - framebuffer_tag_start .int 800 .int 600 .int 32 framebuffer_tag_end:
.align 8
undefined_tag: .short MULTIBOOT_HEADER_TAG_END .short 0 .int 8 multiboot_header_end: .align 8 multiboot_entry:
movl $stack, %esp pushl $0 popf pushl %ebx pushl %eax call _mymain pushl $str call _myprintf 1: hlt jmp 1b
str: .string "idealOS is halt!\n"
.align 0x1000 .fill 0xf000, 1, 0 stack:
_gdtr: .short 3 * 8 - 1 .int _gdt .align 8 _gdt: .quad 0x0000000000000000 .quad 0x00c092000000ffff .quad 0x00c09a000000ffff _myhlt: cli hlt ret
_load_gdtr: # void load_gdtr (int limit, int addr); mov 4(%esp), %ax mov %ax, 6(%esp) lgdt 6(%esp) mov $0x08, %eax mov %eax, %ds mov %eax, %es mov %eax, %fs mov %eax, %gs
jmpl $0x10, $mystart mystart: ret _load_idtr: # void load_idtr (int limit, int addr); mov 4(%esp), %ax mov %ax, 6(%esp) lidt 6(%esp) ret
.align 8 _setup_page: movl $0x400, %ecx movl $0x1000007, %eax movl $0xfff000, %edi cld 1: stosl addl $0x1000, %eax loop 1b
movl $0x100000, %ecx movl $0x7, %eax movl $0x1000000, %edi 1: stosl addl $0x1000, %eax loop 1b
movl $0xfff000, %eax movl %eax, %cr3 movl %cr0, %eax orl $0x80000000, %eax movl %eax, %cr0 ret _change_page: mov $0x7007, %eax mov $0x1000000, %edi stosl mov $0x0007, %eax mov $(0x1000000 + 7 * 4), %edi stosl movl $0xfff000, %eax movl %eax, %cr3 ret _change0: mov $0x0007, %eax mov $0x1000000, %edi stosl movl $0xfff000, %eax movl %eax, %cr3 ret