Hier ist nochmal mein ganzer Code:
#ifndef INCLUDE_include_H
#define INCLUDE_include_H
#include "include.h"
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
#define TABULATOR_WIDTH 4
#define TABULATOR_HEIGHT 4
static int printf_res;
static char* video = (char*) 0xb8000;
static int cursor_x = 0;
static int cursor_y = 0;
static int color = 0xF0;
inline void outb(uint16_t port, uint8_t data)
{
asm volatile ("outb %0, %1" : : "a" (data), "Nd" (port));
}
void scrolldown(int times)
{
int n;
int p;
for (p = 0; p < times; p++)
{
for (n = 0; n < 160; n++)
video[n] = ' ';
for (n = 0; n < 3839; n++)
video[n] = video[n + 160];
for (n = 3840; n < 4000; n++)
video[n] = ' ';
}
cursor_y = cursor_y - times;
}
void checkcursor()
{
if(cursor_y == 25)
{
scrolldown(1);
}
if(cursor_x == 79)
{
cursor_x = 0;
cursor_y++;
}
movecursor(cursor_x, cursor_y);
}
void putc(char c)
{
if(c == '\n')
{
cursor_y++;
cursor_x = -1;
checkcursor();
}
else if(c == '\t')
{
if(cursor_x > SCREEN_WIDTH - TABULATOR_WIDTH)
{
cursor_x = -1;
cursor_y++;
checkcursor();
}
else
{
cursor_x = cursor_x + TABULATOR_WIDTH;
}
}
else if (c == '\b')
{
video[(cursor_y * SCREEN_WIDTH) + cursor_x * 2] = ' ';
cursor_x--;
}
else if (c == '\r')
{
cursor_x = -1;
}
else if (c == '\v')
{
if(cursor_y > SCREEN_HEIGHT - TABULATOR_HEIGHT)
{
scrolldown(SCREEN_HEIGHT - cursor_y);
}
else
{
cursor_x = -1;
cursor_y = cursor_y + TABULATOR_HEIGHT;
}
}
else if (c == '\f')
{
cls();
}
else
{
cursor_x++;
video[(cursor_y * SCREEN_WIDTH) + cursor_x * 2] = c;
video[(cursor_y * SCREEN_WIDTH) + cursor_x * 2 + 1] = color;
checkcursor();
}
printf_res++;
}
void puts(const char* s)
{
while (*s) {
putc(*s++);
}
}
void putn(unsigned long x, int base)
{
char buf[65];
const char* digits = "0123456789abcdefghijklmnopqrstuvwxyz";
char* p;
if (base > 36) {
return;
}
p = buf + 64;
*p = '\0';
do {
*--p = digits[x % base];
x /= base;
} while (x);
puts(p);
}
int printf(const char* fmt, ...)
{
va_list ap;
const char* s;
unsigned long n;
va_start(ap, fmt);
printf_res = 0;
while (*fmt) {
if (*fmt == '%') {
fmt++;
switch (*fmt) {
case 's':
s = va_arg(ap, char*);
puts(s);
break;
case 'd':
case 'u':
n = va_arg(ap, unsigned long int);
putn(n, 10);
break;
case 'x':
case 'p':
n = va_arg(ap, unsigned long int);
putn(n, 16);
break;
case '%':
putc('%');
break;
case '\0':
goto out;
default:
putc('%');
putc(*fmt);
break;
}
} else {
putc(*fmt);
}
fmt++;
}
out:
va_end(ap);
return printf_res;
}
void cls()
{
int u;
cursor_x = 0;
cursor_y = 0;
for(u = 0; u < SCREEN_WIDTH * SCREEN_HEIGHT; u++)
{
cursor_x++;
video[(cursor_y * SCREEN_WIDTH) + cursor_x * 2] = 0;
video[(cursor_y * SCREEN_WIDTH) + cursor_x * 2 + 1] = color;
}
cursor_x = 0;
}
void setcolor(int colorcode)
{
color = colorcode;
}
void movecursor(uint8_t col, uint8_t row)
{
uint16_t tmp;
tmp = (row * SCREEN_WIDTH + col) * 2;
outb(0x3D4, 14);
outb(0x3D5, tmp >> 8);
outb(0x3D4, 15);
outb(0x3D5, tmp);
}
#endif
printf_res zählt die Zeichen. Wenn man wissen möchte wieviele Zeichen der User eingegeben hat, ist das nützlich.
MrTom3715