操作系统与网络实现 之十四

 

今天是2016117日,乙未年腊八节,给大家上新的一章

显示图片

掌握上面的知识,编写画图程序就简单了:

 

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

 

 

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

os_data32_selequ   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) ;

  ya_draw_4bit_bmp(500 , 20 , 0x68400) ;

 

}

 

 

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 ;

   }  

   }

}

 

 

 

void ya_draw_4bit_bmp(unsigned int x ,unsigned int y ,unsigned int addr)

{

   int color[16] = {0x0,0x8000,0x400,0x8400,0x10,0x8010,0x410,0x8410,0xc618,0xf800,0x7e0,0xffe0,0x1f,0xf81f,0x7ff,0xffff} ;//调色板值

   int lx= x;                         //每行要从这里开始,保留这个值

   int * ta = (int *)(addr+0xa) ;       //位图数据开始偏移量 在第0xa字节共四字节

   int * tb = (int *)(addr+0x22) ;      //位图数据长 在第0x22字节共四字节

   int * width = (int *)(addr+0x12) ;   //位图宽  在第0x12字节共四字节

   int * height = (int *)(addr+0x16) ;  //位图高 在第0x16字节共四字节

   int td; //一行要读的字节数

   int te; //补几个字节

   int tf; //假设图宽

    tf= * width;

   switch(tf%8){  //计算一行要读的字节数td 要补的字节数te

       case 0:

        td= tf/2 ; 

        te= 0 ;

       break ;

       case 1:

        td= (tf+1)/2 ; 

        te= 3 ;

       break ;

       case 2:

        td= tf/2 ; 

        te= 3 ;

       break ;

       case 3:

        td= (tf+1)/2 ; 

        te= 2 ;

       break ;

       case 4:

        td= tf/2 ; 

        te= 2 ;

       break ;

       case 5:

        td= (tf+1)/2 ; 

        te= 1 ;

       break ;

       case 6:

        td= tf/2 ; 

        te= 1 ;

       break ;

       case 7:

        td= (tf+1)/2 ; 

        te= 0 ;

       break ;

   }

   

   int tc= td+ te;

   char * string = (char *)(addr+*ta+*tb-tc) ; //数据从最末端-32字节处开始

   for(int i=0 ; i< *height;i++){

       for(int j=0 ; j<td;j++){

           unsigned char ch = *string;   //一个字符有八位,要分别取得其高四位和低四位

           unsigned char c1 = ch>>4;      //取得高四位

           unsigned int pa = color[c1] ;  //取得调色板的对应颜色值

           if(pa!=0xffff){                //如果是白色,跳过不显示

            draw_dot(x, y, pa) ;            

           }

           unsigned char c2 = ch& 0xf;   //取得低四位

            pa= color[c2] ;               //取得调色板的对应颜色值

           if(pa!=0xffff){

            draw_dot(x, y, pa) ;            

           }

            string++ ;

            x++ ;

       }

        string= string+ te-tc*2;  //必须是四个字节宽,X个字节,然后后退XX字节

        x= lx;

        y++;

   }

}

 

 

 

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) ;

 

 

//显示四位bmp图片

void ya_draw_4bit_bmp( unsigned int x , unsigned int y ,unsigned int addr) ;

 

#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

#写入磁盘位置从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写入代码,内容是asc16

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

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

 

 

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

 

 

 

 

 

 

显示4位共十六色的bmp图片

wps39.tmp 

 

 

显示1664Kbmp图片,具体程序请亲们自己练习编写。

      wps3A.tmp

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值