给系统绘制字体
1.简介
如果我们把字体的大小限定在一个8*16的长方形区域,那么我们在这个区域内,将特定位置的像素点设置成黑色,其他点设置成白色,那么我们就可以得到一个白底黑色的字体: 2.单字体实现
如果我们把8*16区域当做一个二维数组,白色的像素我们用0表示,黑色像素我们用1表示,
那么上图的字符A, 最顶层的一行全是白色,所以用8个0表示,
第二行,8个像素中,中间两个像素设置成黑色,
于是对应的二进制数就是000 11 000, 对应的16进制数就是0x18,
依次类推,这样对整个字符A, 我们就有可以设置一个对应的数组:
static char fontA[16] = { 0x00, 0x18, 0x18, 0x18,0x18,0x24,0x24,0x24, 0x24, 0x7e, 0x42, 0x42,0x42, 0xe7, 0x00, 0x00 };
拿到字体数组后,绘制时,把数组中的每一个数值取出来,看该数值的二进制形式中,哪一位设置成1,那么就给对应的像素赋予黑色,如果设置成0,就给对应的像素设置成白色,代码如下:
void showFont8(char *vram, int xsize, int x, int y, char c, char* font) { int i; char d; for (i = 0; i < 16; i++) { d = font[i]; if ((d & 0x80) != 0) {vram[(y+i)*xsize + x + 0] = c;} if ((d & 0x40) != 0) {vram[(y+i)*xsize + x + 1] = c;} if ((d & 0x20) != 0) {vram[(y+i)*xsize + x + 2] = c;} if ((d & 0x10) != 0) {vram[(y+i)*xsize + x + 3] = c;} if ((d & 0x08) != 0) {vram[(y+i)*xsize + x + 4] = c;} if ((d & 0x04) != 0) {vram[(y+i)*xsize + x + 5] = c;} if ((d & 0x02) != 0) {vram[(y+i)*xsize + x + 6] = c;} if ((d & 0x01) != 0) {vram[(y+i)*xsize + x + 7] = c;} }
接着 在write_vga_desktop.c的主函数中,在for(;;)死循环前加入一句:
showFont8(vram, xsize, 20, 20, COL8_FFFFFF, fontA);
对其进行一些列的编译、反汇编、汇编、java操作后
编译C文件
i386-elf-gcc -m32 -fno-asynchronous-unwind-tables -s -c -o write_vga_desktop_single_char.o write_vga_desktop_single_char.c
反汇编o文件
./objconv -fnasm write_vga_desktop_single_char.o write_vga_desktop_single_char.asm
删除无用部分
修改kernel
%include "write_vga_desktop_single_char.asm"
修改boot(直接放大一点)直接读了20个扇区 肯定够用了
mov AL, 20 ; AL 表示要练习读取几个扇区
编译boot
nasm -o boot.bat boot.asm
编译kernel
nasm -o kernel.bat kernel.asm
运行java
Load file boot.bat to floppy with cylinder: 0 and sector:1 Load file kernel.bat to floppy with cylinder: 1 and sector:2 Load file kernel.bat to floppy with cylinder: 1 and sector:3 Load file kernel.bat to floppy with cylinder: 1 and sector:4 Load file kernel.bat to floppy with cylinder: 1 and sector:5 Load file kernel.bat to floppy with cylinder: 1 and sector:6 Load file kernel.bat to floppy with cylinder: 1 and sector:7 Load file kernel.bat to floppy with cylinder: 1 and sector:8 Load file kernel.bat to floppy with cylinder: 1 and sector:9 Load file kernel.bat to floppy with cylinder: 1 and sector:10 Load file kernel.bat to floppy with cylinder: 1 and sector:11 Load file kernel.bat to floppy with cylinder: 1 and sector:12 Load file kernel.bat to floppy with cylinder: 1 and sector:13 Load file kernel.bat to floppy with cylinder: 1 and sector:14
运行: