From 3ce167123a6059e174047d4580edce56c5019fb9 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 12 Jul 2018 14:07:24 +0200 Subject: [PATCH] Add way to print int --- vga.c | 33 ++++++++++++++++++++++++++------- vga.h | 8 +++++--- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/vga.c b/vga.c index 0925e02..0dd48be 100644 --- a/vga.c +++ b/vga.c @@ -1,6 +1,6 @@ #include "vga.h" -void clearScreen(int bgColor) +void clearScreen(uint bgColor) { volatile short *vga = (short *)VGA_ADDR; long int colorAttr = bgColor << 12; @@ -9,18 +9,37 @@ void clearScreen(int bgColor) } } -void printChar(const char str, int color, int bgColor, int startX, int startY) +void printInt(int integer, uint color, uint bgColor, int startX, int startY) { - volatile short *vga = (short *)VGA_ADDR; - long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8; + char num[sizeof(int) * + 3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3) = + // 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3) + int x = startX; + int i = 0, k = 0; + if (integer < 0) { + printChar('-', color, bgColor, x++, startY); + integer = -integer; + } + while (integer != 0) { + num[i++] = integer % 10; + integer = integer / 10; + } + for (k = i - 1; k >= 0; k--) { + printChar(num[k] + '0', color, bgColor, x++, startY); + } +} + +void printChar(const char str, uint color, uint bgColor, int startX, int startY) +{ + volatile short *vga = (short *)VGA_ADDR; + long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8; vga[80 * startY + startX] = colorAttr | str; } -void printString(const char *str, int color, int bgColor, int startX, - int startY) +void printString(const char *str, uint color, uint bgColor, int startX, int startY) { volatile short *vga = (short *)VGA_ADDR; - int i = 0; + int i = 0; long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8; while (*str) { vga[80 * startY + startX + i] = colorAttr | *str; diff --git a/vga.h b/vga.h index d32d6a3..e426fb9 100644 --- a/vga.h +++ b/vga.h @@ -1,4 +1,5 @@ #pragma once +#include "types.h" #define BLACK 0x00 #define BLUE 0x01 @@ -14,6 +15,7 @@ #define VGA_WIDTH 80 #define VGA_HEIGHT 15 -void clearScreen(int bgColor); -void printChar(const char str, int color, int bgColor, int startX, int startY); -void printString(const char *str, int color, int bgColor, int startX, int startY); +void clearScreen(uint bgColor); +void printInt(int integer, uint color, uint bgColor, int startX, int startY); +void printChar(char str, uint color, uint bgColor, int startX, int startY); +void printString(const char *str, uint color, uint bgColor, int startX, int startY);