操作系统与网络实现 之十

 

显示字母与汉字

现在我们明白了字符是怎么存在的,是怎么被显示出来的,下面我们将实践这个过程。

一些准备工作。

新建目录D:\GX\ya\include,这里将存放头文件。

 

boot.asm源码不变:

[BITS 16]               ;编译成16位的指令

[ORG 0x7C00]

jmp                 main

 

read_kernelloader:          ;读入kernelloader 程序

 push             es

 

  .rkl:

 mov              ax , 0x1000  ;kernelloader.bin所在的段基址           

 mov              es , ax

 mov              bx , 0   ;写入到内存0x1000:0000

 mov              ah , 2

 mov              dl , 0   ;驱动器号

 mov              ch , 0   ;磁道

 mov              cl , 2   ; 2个扇区开始

 mov              al , 2   ;读入扇区数,每个扇区为512B

 int              0x13 

 jc                .rkl

 

 pop              es

 ret

 

main:        ;主程序         

 mov              ax , 0x0   ;boot.asm程序的段基址

 mov              ds , ax

 

 call              read_kernelload  ;读入kernelloader 程序 

 

 jmp dword        0x1000:0  ;跳转到kernelloader 处执行

times 510-($-$$) db 0

db 0x55

db 0xAA

 

 

 

修改kernelloader.asm,存放三个字符信息,并将其起始地址存入指定地址:0x10054,0x10058,0x1005c,源码如下:

[BITS 16]  

jmp                 main

 

gdt_entries   equ   3          ;共有三个段描述符:null,os code32,os data32

 

pe           equ   1          ;bit PE in CR0

null         equ   0h

os_code32_selequ   8h         ;1,gdt,rpl=00

os_data32_selequ   10h        ;2,gdt,rpl=00

VESA:       times 256 db 0     ;分配一块区域存放vesa 返回的信息,大小256,我们只需要其中的一个32位值

;汉字32个字节点阵信息

char01      db 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x09,0x20,0x19,0x10,0x21,0x08,0x41,0x0C,0x81,0x04,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00 

;英文“Y”16个字节点阵信息

char02      db 0x00,0x00,0x66,0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00

;英文“A”16个字节点阵信息

char03      db 0x00,0x00,0x10,0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00

pdescr      times 6 db 0

gdt_table   times (gdt_entries*8) db 0

 

 

set_video_mode:            ;设置显卡模式

 push             es

 

 ;设置显卡模式

 mov              ax , 0x4f02

 mov              bx , 0x4114     ;800X600 ( 5:6:5 )

 int              0x10

 ;取得该模式下显卡地址

 mov              bx , 0x1000

 mov              es , bx

 mov              di , VESA  ;es:di指向256空间,int 10h将在此填写数据

 mov              ax , 0x4f01

 mov              cx , 0x114

 int              0x10

 ;40个字节开始存有显卡地址0xe0000000,将此地址再存入指定的地址0x10050

 mov              eax , [ es:VESA+ 40 ]

 ;将此地址再存入指定的地址0x10050,因为实际运行中,在0x10040-0x10120都没有填入数据,都是可以利用的

 mov              [ es:0x50 ] , eax           ;eax内容为0xe0000000

   

 pop              es

 ret

 

set_char_addr:                                            ;将字符地址存入指定地方

 push             es

 mov              bx , 0x1000

 mov              es , bx

 mov              eax , char01 + 0x10000  ;第一个字符地址

 mov              [ es:0x54 ] , eax        ;将此地址再存入指定的地址0x10054

 mov              eax , char02 + 0x10000  ;第二个字符地址

 mov              [ es:0x58 ] , eax        ;将此地址再存入指定的地址0x10058

 mov              eax , char03 + 0x10000  ;第三个字符地址

 mov              [ es:0x5c ] , eax        ;将此地址再存入指定的地址0x1005c

 pop              es

 ret

 

read_kernel:                                              ;读入kernel 程序

 push             es

 

  .rk:

 mov              ax , 0x8000                           ;kernel.bin所在的段基址           

 mov              es , ax

 mov              bx , 0                                 ;写入到内存0x8000:0000物理地址=0x80000

 mov              ah , 2

 mov              dh , 0                                 ;磁头

 mov              dl , 0                                 ;驱动器号

 mov              ch , 0                                 ;磁道0

 mov              cl , 4                                 ;4个扇区开始

 mov              al , 5     ;读入扇区数,每个扇区为512B

 int              0x13   

 jc                .rk

 

 pop              es

 ret

 

main:

mov ax,1000h

mov ds,ax

;设置显卡模式

call              set_video_mode

;将字符地址存入指定地方

call set_char_addr

;读入kernel

call              read_kernel

;打开A 20 地址线

mov              ax , 0x2401

int              0x15 

;[1]built up GDT table

cli

mov eax,gdt_table

;item 0:null descriptor,

mov dword[eax],0

mov dword[eax+4],0

add eax,8

;item 1,OS code32 descriptor,

;Base=00000000h,limit=0ffh,G=1,D=1,type=a,dpl=0

mov word[eax],0ffh

mov word[eax+2],0

mov byte[eax+4],00h

mov byte[eax+5],09ah

mov byte[eax+6],0c0h

mov byte[eax+7],00h

add eax,8

;item 2,OS data32 descriptor

;Base=00000000h,Limit=0fffffh,G=1,D=1,Type=2,DPL=0

mov word[eax],0ffffh

mov word[eax+2],0000h

mov byte[eax+4],00h

mov byte[eax+5],092h

mov byte[eax+6],0cfh   ;高四位是G D 0 AVL,此处为1100 = c ,低四位是limit bit 16-19此处为f

mov byte[eax+7],00h

add eax,8

;[2]built false GDT descriptor

mov word[pdescr+0],(gdt_entries*8)

mov dword[pdescr+2],gdt_table+00010000h

lgdt [pdescr]

;[3]enter into protected mode

;刷新CR0

mov eax,cr0

or eax,pe

mov cr0,eax

jmp flush

flush:

mov ax,os_data32_sel

mov ds,ax

mov es,ax

mov ss,ax

mov fs,ax

mov gs,ax

jmp dword os_code32_sel:0x80000 ;跳转到0x8000:0000保护模式  物理地址0x80000

 

 

修改kernel.c,源码如下:

#include "..\include\graph.h"

unsigned short color ;

void ya_main()

{

//写字的颜色

  color= rgb_mix( 0 , 255 , 255 ) ;

 unsigned int x = 250 ;

 unsigned int y = 50 ;

  ya_draw_chinese(  x ,  y, 0x54 , color ) ;

  ya_draw_english(  x + 16 , y + 2 , 0x58 , color ) ; 

  ya_draw_english(  x + 24 , y + 2 , 0x5c , color ) ;

 

}

 

 

增加graph.c,源码如下:

#include "..\include\graph.h"

unsigned int * addr = (int *)0x10050 ;

 

 

//颜色合成函数

unsigned short rgb_mix( unsigned char r , unsigned char g , unsigned char b )

{

 union{

   unsigned int color ;

   struct{

     unsigned char b : 5 ;

     unsigned char g : 6 ;

     unsigned char r : 5 ;

   }sa;

 }ua;

  ua.sa.r= r>> 3 ;

  ua.sa.g= g>> 2 ;

  ua.sa.b= b>> 3 ;

 return ua.color;

}

 

//画点函数

void draw_dot( unsigned int x , unsigned int y , unsigned short color )

{

 // 取得显卡地址

 //unsigned short *video_addr ;

 unsigned int mid = *addr;

 unsigned short * video_addr = (unsigned int *)mid;

 

 // 计算点的偏移量

 unsigned int offset = y* 800 + x ; 

//  *( video + offset ) = color ;

*( video_addr+ offset) = color ;

}

 

//显示英文

void ya_draw_english( unsigned int x , unsigned int y , unsigned int addr_in_font , unsigned short color )

{

   int ta= y;

   int tb= x;

   // addr_in_fontkernelloader.asm中字符指定存入的地址的偏移量

   unsigned int *addr= ( unsigned int * )(0x10000 + addr_in_font); 

   unsigned int mid = *addr;

   unsigned char * english_font = (int *)mid;

   unsigned char font_char ;

   for (int tc = 0; tc < 16; tc++) { //一个英文 16

       for (int td = 7; td >= 0; td--) { //一行一个字节 八位

            tb++ ;

           if ((font_char= english_font[ tc ] & (1 << td))) {

                draw_dot(tb, ta, color);

           }

       }

        ta++; //显示下一行

        tb= x;

   }

}

 

//显示汉字

void ya_draw_chinese( unsigned int x , unsigned int y , unsigned int addr_in_font , unsigned short color )

{

   int ta= y;

   int tb= x;

   unsigned int *addr= ( unsigned int * )(0x10000 + addr_in_font); 

   unsigned int mid = *addr;

   unsigned char * chinese_font = (int *)mid;

   unsigned char font_char ;

   for (int tc = 0; tc < 16; tc++) { //一个汉字16

       for (int te =0 ; te < 2; te++){ // 一行两个字节 十六位

           for (int td = 7; td >= 0; td--) {

                tb++ ;

               if ((font_char= chinese_font[ tc*2 + te ] & (1 << td))) {

                draw_dot(tb, ta, color);

               }

           }

       }

        ta++; //显示下一行

        tb= x;

   }

}

 

boot.asm

[BITS 16]                                                 ;编译成16位的指令

[ORG 0x7C00]

jmp                 main

 

read_kernelloader:                                       ;读入kernelloader 程序

 push             es

 

  .rkl:

 mov              ax , 0x1000                           ;kernelloader.bin所在的段基址           

 mov              es , ax

 mov              bx , 0                                 ;写入到内存0x1000:0000

 mov              ah , 2

 mov              dl , 0                                 ;驱动器号

 mov              ch , 0                                 ;磁道

 mov              cl , 2                                 ; 2个扇区开始

 mov              al , 2                                 ;读入扇区数,每个扇区为512B

 int              0x13 

 jc                .rkl

 

 pop              es

 ret

 

main:                                                      ;主程序         

 mov              ax , 0x0                               ;boot.asm程序的段基址

 mov              ds , ax

 

 call              read_kernelloader                             ;读入kernelloader 程序 

 

 jmp dword        0x1000:0                               ;跳转到kernelloader 处执行

times 510-($-$$) db 0

db 0x55

db 0xAA

 

 

 

 

在目录D:\GX\ya\include增加graph.h,源码如下:

#ifndef _GRAPH_H_

#define _GRAPH_H_

 

//色调合成函数

unsigned short rgb_mix( unsigned char r , unsigned char g , unsigned char b ) ;

 

//画点函数

void draw_dot( unsigned int x , unsigned int y , unsigned short color ) ;

 

//显示英文

void ya_draw_english( unsigned int x , unsigned int y , unsigned int addr_in_font, unsigned short color ) ;

 

//显示汉字

void ya_draw_chinese( unsigned int x , unsigned int y , unsigned int addr_in_font, unsigned short color ) ;

 

#endif

 

修改makefile

######################

#声明要编译的所有组成,这里的ya是本工程名称,可以取任何名字,这里就用ya

######################

ya:out/boot.bin out/kernelloader.bin out/kernel.asmo out/kernel.o out/graph.o out/kernel.ld  out/kernel.bin out/creat_img.exe out/write_in_img.exe A B C D

#开始对各部分编译,注意不是空格是Tab

out/boot.bin:code/boot.asm

    nasm code/boot.asm -o out/boot.bin

out/kernelloader.bin:code/kernelloader.asm

    nasm code/kernelloader.asm -o out/kernelloader.bin

#编译asm文件,生成中间文件

out/kernel.asmo:code/kernel.asm

    nasm -f aout code/kernel.asm -o out/kernel.asmo

#编译C文件,生成中间文件

out/kernel.o:code/kernel.c

    gcc -fpack-struct -std=c99 -c code/kernel.c -o out/kernel.o

out/graph.o:code/graph.c

    gcc -fpack-struct -std=c99 -Wno-packed-bitfield-compat -c code/graph.c -o out/graph.o

#链接内核

out/kernel.ld:out/kernel.asmo out/kernel.o out/graph.o

    ld  -Ttext 0x80000 -e start -o out/kernel.ld out/kernel.asmo out/kernel.o out/graph.o

#生成可执行代码文件

out/kernel.bin:out/kernel.ld

    objcopy -R .note -R .comment -S -O binary out/kernel.ld out/kernel.bin

#制作内核映象文件

out/creat_img.exe:code/creat_img.c

    gpp code/creat_img.c -o out/creat_img.exe

#执行dos命令,在final目录下生成a.img文件

A:

    out/creat_img.exe final/a.img

 

#写入文件,argv[1]=目标文件argv[2]=源文件 argv[3]=写入偏移量  

#DOS下用法: write.exe a.img kernelloader.bin 512

out/write_in_img.exe:code/write_in_img.c

    gpp code/write_in_img.c -o out/write_in_img.exe

#执行dos命令,向a.img写入代码,内容是boot.bin

#写入磁盘位置从0偏移量起始,1个扇区512字节

B:

    out/write_in_img.exe final/a.img out/boot.bin 0

#执行dos命令,向a.img写入代码,内容是kernelloader.bin

# boot.bin已经占用了512字节,写入磁盘位置从512偏移量起始,2个扇区1024字节

C:

    out/write_in_img.exe final/a.img out/kernelloader.bin 512

#执行dos命令,向a.img写入代码,内容是kernel.bin

# boot.bin+kernelloader.bin已经占用了512+1024 = 1536字节,写入磁盘位置从1536偏移量起始,占1个扇区512字节

D:

    out/write_in_img.exe final/a.img out/kernel.bin 1536

   

######################

 

 

运行模拟器,结果显示如图:

wps23.tmp 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值