0x05_My-OS显示字符串和任意参数
2024-10-21 11:29:00
先看看效果:
要解决两个问题,第一个如何显示字符串,printf?我之前已经说了所有的头文件都要自己写,printf是stdio里的可是我们没有stdio
我们要通过画像素点的方式显示字符串,有点像我的世界的红石系统,用红石点亮一个方块,第二个问题是如何调用类似sprintf的
功能
显示字符串
把class02改名为class03
复制kernel中font文件夹到你的class03
graphic.c
void putfont8(char *vram, int xsize, int x, int y, char c, char *font)
{
int i;
char *p, d /* data */;
for (i = 0; i < 16; i++) {
p = vram + (y + i) * xsize + x;
d = font[i];
if ((d & 0x80) != 0) { p[0] = c; }
if ((d & 0x40) != 0) { p[1] = c; }
if ((d & 0x20) != 0) { p[2] = c; }
if ((d & 0x10) != 0) { p[3] = c; }
if ((d & 0x08) != 0) { p[4] = c; }
if ((d & 0x04) != 0) { p[5] = c; }
if ((d & 0x02) != 0) { p[6] = c; }
if ((d & 0x01) != 0) { p[7] = c; }
}
return;
} void putfonts8_asc(char *vram, int xsize, int x, int y, char c, unsigned char *s)
{
extern char font[4096];
/* C语言中,字符串都是以0x00结尾 */
for (; *s != 0x00; s++) {
putfont8(vram, xsize, x, y, c, font + *s * 16);
x += 8;
}
return;
}
在graphic里面增加这些函数,第一个函数是用来读取字符串然后表示成像素点,字符串的形式类似于
**......
.*......
.*......
.*......
.*......
.*.**...
.**..*..
.*....*.
.*....*.
.*....*.
.*....*.
.*....*.
.*....*.
***...**
........
........
这是个h,他读取就是读*遍历到就显示像素点为指定颜色
head.h
/*naskfunc.asm*/
void io_stihlt();
void io_hlt(void);
void io_cli(void);
void io_sti(void);
int io_get8(int port);
void io_set8(int port, int data);
void write_mem8(int addr, int data);
int io_load_eflags(void);
void io_store_eflags(int eflags); /* asmhead.nas */
struct BOOTINFO { /* 0x0ff0-0x0fff */
char cyls; /* 启动区读磁盘读到此为止 */
char leds; /* 启动时键盘的LED的状态 */
char vmode; /* 显卡模式为多少位彩色 */
char reserve;
short scrnx, scrny; /* 画面分辨率 */
char *vram;
};
#define ADR_BOOTINFO 0x00000ff0 /*graphic.c*/
void init_palette(void);
void set_palette(int start, int end, unsigned char *rgb);
void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1);
void putfont8(char *vram, int xsize, int x, int y, char c, char *font);
void putfonts8_asc(char *vram, int xsize, int x, int y, char c, unsigned char *s); /*font*/
extern char font[4096];
增加的地方我都加粗了
main.c
#include "include/head.h"
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; void Main(void){
int i;
init_palette();
boxfill8(binfo->vram, binfo->scrnx, 0,0,0,binfo->scrnx, binfo->scrny);
putfonts8_asc(binfo->vram, binfo->scrnx, 8, 8, 7, "hello,world"); for (;;) {
io_hlt();
}
}
最后一步,
makefile里面增加这个font.obj
实现输出参数
haribote作者已经帮我们实现了
main.c
#include "include/head.h"
#include <string.h> struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; void Main(void){
int i;char s[256];
init_palette();
boxfill8(binfo->vram, binfo->scrnx, 0,0,0,binfo->scrnx, binfo->scrny);
sprintf(s, "scrnx = %d", binfo->scrnx);
putfonts8_asc(binfo->vram, binfo->scrnx, 8, 8, 7, s); for (;;) {
io_hlt();
}
}
运行:
cd class02
..\z_tools\make.exe run
自制操作系统合集
原文地址:https://www.cnblogs.com/Frank-dev-blog/category/2249116.html
项目github地址rick521/My-OS (github.com)给我点颗star
最新文章
- XSD文件生成C#VO实体类
- static成员变量与返回对象的引用
- Android ViewPager 用法
- 使用IOS7原生API进行二维码条形码的扫描
- CSS3轻松实现清新 Loading 效果
- HDU 4638 Group 树状数组 + 思路
- hdu 2204 Eddy&#39;s爱好
- lightoj 1011 (状态压缩dp)
- LogMiner学习笔记
- Jdk1.6 JUC源码解析(13)-LinkedBlockingQueue
- Hacking Bsides Vancouver 2018 walkthrough
- 关于EL表达式随笔记录
- 虚幻4引擎角色蓝图Character的Movement组件学习
- RxJS--Subject
- Python基础(10)——类进阶(静态方法、类方法、属性方法)
- Vivado+FPGA:如何使用Debug Cores(ILA)在线调试(烧录到flash里可以直接启动)
- RabbitMQ--Publish/Subscribe(五)
- “Hello World!”团队第六周的第一次会议
- xcode恢复语法高亮
- STM32基础分析——PWM配置