0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wslで、最小のカーネルをコンパイル その5

Posted at

概要

wslで、kernel.elfのコンパイル、やってみた。
cで、書いたカーネルで、九九、fizzbuzz、zundoko、実行してみた。

写真

image.png

サンプルコード

#include "kernel.h"

uint32 vga_index;
static uint32 next_line_index = 1;
uint8 g_fore_color = WHITE,
	g_back_color = BLUE;
int digit_ascii_codes[10] = {
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39
};
uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color) {
	uint16 ax = 0;
	uint8 ah = 0,
		al = 0;
	ah = back_color;
	ah <<= 4;
	ah |= fore_color;
	ax = ah;
	ax <<= 8;
	al = ch;
	ax |= al;
	return ax;
}
void clear_vga_buffer(uint16 ** buffer, uint8 fore_color, uint8 back_color) {
	uint32 i;
	for(i = 0; i < BUFSIZE; i++)
	{
		(* buffer) [i] = vga_entry(NULL, fore_color, back_color);
	}
	next_line_index = 1;
	vga_index = 0;
}
void init_vga(uint8 fore_color, uint8 back_color) {
	vga_buffer = (uint16 *) VGA_ADDRESS;
	clear_vga_buffer(&vga_buffer, fore_color, back_color);
	g_fore_color = fore_color;
	g_back_color = back_color;
}
void print_new_line() {
	if (next_line_index >= 55)
	{
		next_line_index = 0;
		clear_vga_buffer(&vga_buffer, g_fore_color, g_back_color);
	}
	vga_index = 80 * next_line_index;
	next_line_index++;
}
void print_char(char ch) {
	vga_buffer[vga_index] = vga_entry(ch, g_fore_color, g_back_color);
	vga_index++;
}
uint32 strlen(const char * str) {
	uint32 length = 0;
	while(str[length])
		length++;
	return length;
}
uint32 digit_count(int num) {
	uint32 count = 0;
	if (num == 0)
		return 1;
	while(num > 0)
	{
		count++;
		num = num / 10;
	}
	return count;
}
void itoa(int num, char * number) {
	int dgcount = digit_count(num);
	int index = dgcount - 1;
	char x;
	if (num == 0 && dgcount == 1)
	{
		number[0] = '0';
		number[1] = '\0';
	}
	else
	{
		while(num != 0)
		{
			x = num % 10;
			number[index] = x + '0';
			index--;
			num = num / 10;
		}
		number[dgcount] = '\0';
	}
}
void print_string(char * str) {
	uint32 index = 0;
	while(str[index])
	{
		print_char(str[index]);
		index++;
	}
}
void print_int(int num) {
	char str_num[digit_count(num) + 1];
	itoa(num, str_num);
	print_string(str_num);
}
static int randseed = 12345;
int rand(void) {
	return (randseed = randseed * 12345 + 17);
}
void kernel_entry() {
	int i,
		j,
		k;
	uint8 r = 243;
	init_vga(WHITE, BLACK);
	print_string("0 Hello World!");
	print_new_line();
	print_int(123456789);
	print_new_line();
	print_string("4 Goodbye World!");
	print_new_line();
	for (i = 1; i < 10; i++)
	{
		for (j = 1; j < 10; j++)
		{
			k = i * j;
			print_int(k);
			print_string(" ");
		}
		print_new_line();
	}
	for (i = 1; i < 101; i++)
	{
		if (i % 15 == 0)
		{
			print_string("FizzBuzz");
			print_new_line();
		}
		else if (i % 3 == 0)
		{
			print_string("Fizz ");
		}
		else if (i % 5 == 0)
		{
			print_string("Buzz ");
		}
		else
		{
			print_int(i);
			print_string(" ");
		}
	}
	i = 0;
	while (i < 4)
	{
		r = r * 5;
		r = r - 1;
		if (r < 127)
		{
			print_string("zun ");
			i++;
		}
		else
		{
			print_string("doko ");
			i = 0;
		}
	}
	print_string("doko kiyosi!!");
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?