输出字库中的字符

现在在内存中有字库了,我们就可以任意显示英文和中文了。

 

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:;跳转到 kernelloader 处执行

times 510-($-$$) db 0

db 0x55

db 0xAA

 

 

kernelloader.asm源码:

 

[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_sel equ    8h        ;1,gdt,rpl=00

os_data32_sel equ    10h       ;2,gdt,rpl=00

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

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 ) 16位色

  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            ;es:bx 0x1000:0050=0x10050 内容为 0xe0000000

   

  pop               es

  ret

read_charpic:                          ;读入字库及图片

  push              es

  mov               ah , 2              ;磁盘读

  mov               dl , 0              ;驱动器号

  .rc1:

  mov               bx , 0x1000   ;asc16 所在的段基址           

  mov               es , bx

  mov               bx , 0x0400         ;写入到内存0x1000:0400 物理地址=0x10400

  mov               dh , 0               ;磁头

  mov               ch , 0               ;磁道

  mov               cl , 4               ;开始扇区

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

  int               0x13   

  jc                .rc1

 

  add               bx , 15*512          ;读入地址增加

  mov               di , 6                ;读入磁道数

  mov               dh , 1                ;磁头

  mov               ch , 0                ;磁道 

  call              readtrack

 

  .rc2:

  mov               ah , 2               ;磁盘读 

  mov               dh , 1               ;磁头

  mov               ch , 3               ;磁道

  mov               cl , 1               ;开始扇区

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

  int               0x13   

  jc                .rc2

;开始0x20000 

  mov               ah , 2               ;磁盘读

  mov               dl , 0               ;驱动器号

  .rc3:

  mov               bx , 0x2000         ;所在的段基址           

  mov               es , bx

  mov               bx , 0x0000     ;写入到内存物理地址0x20000

  mov               dh , 1               ;磁头

  mov               ch , 3               ;磁道

  mov               cl , 4               ;开始扇区

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

  int               0x13   

  jc                .rc3

 

  add               bx , 15*512         ;读入地址增加

 

  mov               di , 6               ;读入磁道数

  mov               dh , 0               ;磁头

  mov               ch , 4               ;磁道

  call              readtrack

 

  .rc4:

  mov               ah , 2               ;磁盘读

  mov               dh , 0               ;磁头

  mov               ch , 7               ;磁道

  mov               cl , 1               ;开始扇区

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

  int               0x13   

  jc                .rc4

 

;开始0x30000 

  mov               ah , 2               ;磁盘读

  mov               dl , 0               ;驱动器号

  .rc5:

  mov               bx , 0x3000         ;所在的段基址           

  mov               es , bx

  mov               bx , 0x0000    ;写入到内存物理地址0x30000

  mov               dh , 0               ;磁头

  mov               ch , 7               ;磁道

  mov               cl , 6               ;开始扇区

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

  int               0x13   

  jc                .rc5

 

  add               bx , 13*512          ;读入地址增加

 

  mov               di , 6               ;读入磁道数

  mov               dh , 1               ;磁头

  mov               ch , 7               ;磁道

  call              readtrack

 

  .rc6:

  mov               ah , 2               ;磁盘读

  mov               dh , 1               ;磁头

  mov               ch , 10              ;磁道

  mov               cl , 1               ;开始扇区

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

  int               0x13   

  jc                .rc6 

 

;开始0x40000 

  mov               ah , 2               ;磁盘读

  mov               dl , 0               ;驱动器号

  .rc7:

  mov               bx , 0x4000         ;所在的段基址           

  mov               es , bx

  mov               bx , 0x0000     ;写入到内存物理地址0x40000

  mov               dh , 1               ;磁头

  mov               ch , 10              ;磁道

  mov               cl , 8               ;开始扇区

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

  int               0x13   

  jc                .rc7

 

  add               bx , 11*512         ;读入地址增加

 

  mov               di , 6               ;读入磁道数

  mov               dh , 0               ;磁头

  mov               ch , 11              ;磁道

  call              readtrack

 

  .rc8:

  mov               ah , 2               ;磁盘读

  mov               dh , 0               ;磁头

  mov               ch , 14              ;磁道

  mov               cl , 1               ;开始扇区

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

  int               0x13   

  jc                .rc8

 

;开始0x50000 

  mov               ah , 2               ;磁盘读

  mov               dl , 0               ;驱动器号

  .rc9:

  mov               bx , 0x5000         ;所在的段基址           

  mov               es , bx

  mov               bx , 0x0000    ;写入到内存物理地址0x50000

  mov               dh , 0               ;磁头

  mov               ch , 14              ;磁道

  mov               cl , 10              ;开始扇区

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

  int               0x13   

  jc                .rc9

 

  add               bx , 9*512          ;读入地址增加

 

  mov               di , 6               ;读入磁道数

  mov               dh , 1               ;磁头

  mov               ch , 14              ;磁道

  call              readtrack

 

  .rc10:

  mov               ah , 2               ;磁盘读

  mov               dh , 1               ;磁头

  mov               ch , 17              ;磁道

  mov               cl , 1               ;开始扇区

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

  int               0x13   

  jc                .rc10

 

;开始0x60000 

  mov               ah , 2               ;磁盘读

  mov               dl , 0               ;驱动器号

  .rc11:

  mov               bx , 0x6000         ;所在的段基址           

  mov               es , bx

  mov               bx , 0x0000    ;写入到内存物理地址0x60000

  mov               dh , 1               ;磁头

  mov               ch , 17              ;磁道

  mov               cl , 12              ;开始扇区

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

  int               0x13   

  jc                .rc11

 

  add               bx , 7*512          ;读入地址增加

 

  mov               di , 6               ;读入磁道数

  mov               dh , 0               ;磁头

  mov               ch , 18              ;磁道

  call              readtrack

 

  .rc12:

  mov               ah , 2               ;磁盘读

  mov               dh , 0               ;磁头

  mov               ch , 21              ;磁道

  mov               cl , 1               ;开始扇区

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

  int               0x13   

  jc               .rc12

 

;开始0x70000 

  mov               ah , 2               ;磁盘读

  mov               dl , 0               ;驱动器号

  .rc13:

  mov               bx , 0x7000         ;所在的段基址           

  mov               es , bx

  mov               bx , 0x0000     ;写入到内存物理地址0x70000

  mov               dh , 0                ;磁头

  mov               ch , 21               ;磁道

  mov               cl , 14               ;开始扇区

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

  int               0x13   

  jc                .rc13

 

  pop es

  ret 

 

;读入几个磁道子程序

;di  读入的磁道总数

;dh  磁头

;ch  开始磁道

readtrack:

  .r_1:

  mov               ah , 2       ;功能号 2 表示读磁盘扇区

  mov               cl , 1                       ;启始扇区1扇区

  mov               al , 18                      ;读入扇区数

  int               0x13

  jc                .r_1

  add               bx , 18 * 512               ;读入地址增加

  dec               di

  cmp               di , 0

  je                .r_2

 

  xor               dh , 1                        ;反转磁头

  cmp               dh , 0      ;如果反转值为0,磁道数要加1

  jne               .r_1

  inc               ch

  jmp               .r_1

 

  .r_2:

  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               dl , 0               ;驱动器号

  mov               dh , 0               ;磁头0

  mov               ch , 26              ;磁道

  mov               cl , 1               ;1个扇区开始

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

  int               0x13   

  jc                .rk

 

  pop               es

  ret

 

main:

mov ax,1000h

mov ds,ax

;设置显卡模式

call              set_video_mode

;读入 kernel

call              read_kernel

;读入字库及图片

call read_charpic

;打开 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.asm

[BITS 32]

[GLOBAL start]      ;我们必须导出start这个入口,以便让链接器识别 ,

[EXTERN _ya_main]   ;用到本文件外定义的函数 在kernel.c

jmp                 start

start:

call _ya_main       ;调用C

jmp $

 

 

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_chars(  250, 50, "YA长大了" , 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 )

{

    unsigned char *english_font = ( unsigned char * )(0x10400 + addr_in_font * 16) ;

    int ta = y ;

    int tb = x ;

    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 )

{

    unsigned char *chinese_font = ( unsigned char * )(0x11400 + addr_in_font * 32) ;

    int ta = y ;

    int tb = x ;

    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;

    }

}

 

// 显示字符串

void ya_draw_chars( int x, int y, char *chars , unsigned short color)

{

    while (*chars){

    char ch = *chars++ ;

    if (ch & 0x80){ //是中文

        char cl = *chars++ ;

        cl -= 0xa1;

        ch -= 0xa1;

        int addr = 94*ch + cl ; 

        ya_draw_chinese( x , y , addr , color);

        x += 16 ;

    }

    else{ //是英文

        int addr = ch ; 

        ya_draw_english(x , y , addr , color);

        x += 8 ;

    }  

    }

}

 

 

 

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 pos_in_font , unsigned short color ) ;

 

// 显示汉字

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

 

// 显示字符串

void ya_draw_chars( int x, int y, char *chars , 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 E F G H

#开始对各部分编译,注意不是空格是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

# 写入磁盘位置仿偏移量起姿卿个扇匿12字节

B:

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

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

# boot.bin已经占用亿12字节,写入磁盘位置仿12偏移量起姿卿个扇匿024字节

C:

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

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

# boot.bin+kernelloader.bin已经占用亿12+1024 = 1536字节,写入磁盘位置仿536偏移量起始,卿个扇匿12字节

D:

    out/write_in_img.exe final/a.img charpic/asc16 1536

E:

    out/write_in_img.exe final/a.img charpic/hzk16f 5632

F:

    out/write_in_img.exe final/a.img charpic/ya.bmp 267776

G:

    out/write_in_img.exe final/a.img charpic/faya.bmp 361984

H:

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

 

 

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

 

 

运行bochs,显示如下:

 

wps6B.tmp 

 

 

 

posted on 2015-12-12 19:40  ya20151015  阅读(235)  评论(0编辑  收藏  举报