《1個人開發操作系統之界面與字體》一文中有遺留一個困擾我3周的問題。既字符串和數組編譯後屬於.data section,使用objcopy,將obj文件轉換爲binary文件後,程序不能正確地找到這些.data section的地址。其實問題不在objcopy,而是LD。靜態數組和字符串指針地址是.data section在內存中的相對地址。所謂相對地址指相對於內存中.text section地址。在沒有制定ld連接器.text section地址時,ld默認爲.text section地址爲0x1000。這樣,.data section的地址是根據0x1000計算出來的。而我的ColimasMain函數是從0x280000開始運行的,所以數組與字符串的地址是錯的。要改正.data section的地址非常簡單,只需要告訴LD我的text section的正確地址。
我在Makefile文件里加入LD的Flags LDFLAGS =-Ttext 0x280000。修改bootpack.c,增加數據組變量,用於設置16種顏色。
static unsigned char table_rgb[16 * 3] = {
0x00, 0x00, 0x00,/* 0:黒 */
0xff, 0x00, 0x00, /* 1:正紅 */
0x00, 0xff, 0x00, /* 2:正綠 */
0xff, 0xff, 0x00, /* 3:正黃 */
0x00, 0x00, 0xff, /* 4:正青 */
0xff, 0x00, 0xff, /* 5:正紫 */
0x00, 0xff, 0xff, /* 6:正藍 */
0xff, 0xff, 0xff, /* 7:白 */
0xc6, 0xc6, 0xc6, /* 8:灰 */
0x84, 0x00, 0x00, /* 9:暗紅 */
0x00, 0x84, 0x00, /* 10:暗綠 */
0x84, 0x84, 0x00, /* 11:暗黃 */
0x00, 0x00, 0x84, /* 12:暗青 */
0x84, 0x00, 0x84, /* 13:暗紫 */
0x00, 0x84, 0x84, /* 14:暗藍 */
0x84, 0x84, 0x84 /* 15:暗灰 */
};
修改set_palette函數
void set_palette(unsigned char *rgb)
{
int i, eflags;
eflags = io_load_eflags();
io_cli();
io_out8(0x03c8, 0);
for (i = 0; i <= 15; i++) {
io_out8(0x03c9, rgb[0] / 4);
io_out8(0x03c9, rgb[1] / 4);
io_out8(0x03c9, rgb[2] / 4);
rgb += 3;
}
io_store_eflags(eflags);
return;
}
編譯後和前文結果一樣。
《OS自作入門》給出一套完整的字體對象文件,該obj文件可以和我的程序連接在一起,因此就不需要font.h和font.c文件。顯示字符串函數爲:
void drawstring(char *vram, int xsize, int x, int y, char c, unsigned char *s)
{
extern char hankaku[4096]; /*日文,半角的意思。是字體對象文件內的全局數組*/
for (; *s != 0x00; s++) {
putfont8(vram, xsize, x, y, c, hankaku + *s * 16);
x += 8;
}
return;
}
在界面上顯示字符串:
void ColimasMain(void)
{
struct BOOTINFO *binfo = (struct BOOTINFO *) 0x0ff0;
init_palette();
init_screen(binfo->vram, binfo->scrnx, binfo->scrny);
char *str="Hello world";
drawstring(binfo->vram, binfo->scrnx, 18, 10, COL8_FFFFFF, str);
str="Start";
drawstring(binfo->vram, binfo->scrnx,10, binfo->scrny-20, COL8_000000, str);
for (;;) {
io_hlt();
}
}
運行結果: