From 3b2c40e7f742b043803c97fbc11043b1f50b40f4 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Wed, 6 May 2020 08:43:09 +0200 Subject: clean up + docu --- 01_exercise/bootloader.c | 112 +++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 53 deletions(-) diff --git a/01_exercise/bootloader.c b/01_exercise/bootloader.c index 0500ce4..fcdc0f4 100644 --- a/01_exercise/bootloader.c +++ b/01_exercise/bootloader.c @@ -2,46 +2,43 @@ asm(".code16gcc;" "jmp $0, $main"); -/* - * 0 black - * 1 blue - * 2 green - * 3 cyan - * 4 red - * 5 magenta - * 6 brown - * 7 light gray - * 8 dark gray - * 9 light blue - * A light green - * B light cyan - * C light red - * D light magenta - * E yellow - * F white - */ +#define BLACK 0x0 +#define BLUE 0x1 +#define GREEN 0x2 +#define CYAN 0x3 +#define RED 0x4 +#define MAGENTA 0x5 +#define BROWN 0x6 +#define L_GRAY 0x7 +#define GRAY 0x8 +#define L_BLUE 0x9 +#define L_GREEN 0xA +#define L_CYAN 0xB +#define L_RED 0xC +#define L_MAGENTA 0xD +#define YELLOW 0xE +#define WHITE 0xF -#define BG_COLOR 0x0 -#define FG_COLOR 0x4 -#define TEXT_COLOR (BG_COLOR << 4 | FG_COLOR) - -void putc(char c) { - asm volatile("mov $0x0E, %%ah;" - "int $0x10;" ::"al"(c)); -} +#define BG_COLOR BLACK +#define FG_COLOR MAGENTA +#define COLOR(FG, BG) (BG << 4 | FG) +#define TEXT_COLOR COLOR(FG_COLOR, BG_COLOR) void putc_color(char c, char color) { - asm volatile("mov $0x09, %%ah;" // using Int 10h AH=09 for attributes - "and $0x00FF, %%bx;" // erase higher bits of bx - "mov $0x0001, %%cx;" // write the char once - "int $0x10;" // we need to manually move the cursor - "mov $0x0300, %%eax;" // get cursor position - "int $0x10;" - "add $0x1, %%dl;" // raise column by one - "mov $0x0200, %%eax;" // set cursor position - "int $0x10;" ::"al"(c), - "bl"(color) - : "cx", "dx"); + asm volatile( + "mov $0x09, %%ah;" // using Int 10h AH=09 for attributes + "and $0x00FF, %%bx;" // erase higher bits of bx + "mov $0x0001, %%cx;" // write the char once + "int $0x10;" // we need to manually move the cursor + "mov $0x0300, %%ax;" // get cursor position + "int $0x10;" + "inc %%dl;" // raise column by one (normally we would need to check for end of + // line; respective end of screen here - for simplicity (and binary + // size) sake, leave that out - will cause bugs) + "mov $0x0200, %%ax;" // set cursor position + "int $0x10;" ::"al"(c), + "bl"(color) + : "cx", "dx"); } void print(char const *const str) { @@ -51,30 +48,34 @@ void print(char const *const str) { } void new_line() { - putc('\n'); - putc('\r'); + // using Int10h AH=0E (teletype print char) here, because it does evaluate \n and \r + // (and others) + asm volatile("mov $0x0E0a, %%ax;" // \n + "int $0x10;" + "mov $0x0E0d, %%ax;" // \r + "int $0x10;" :: + : "ax"); } char getc() { char ret; asm volatile( ".no_key:" - "xor %%eax, %%eax;" + "xor %%ax, %%ax;" "mov $0x0100, %%ax;" // check for keystroke: "int $0x16;" // http://www.ctyme.com/intr/rb-1755.htm "jz .no_key;" // wait for a key - ".done:" - "xor %%eax, %%eax;" + "xor %%ax, %%ax;" "mov $0x1000, %%ax;" // get keystroke: http://www.ctyme.com/intr/rb-1754.htm "int $0x16;" - "mov %%al, %%bl;" // temp store char in bl - "mov $0400, %%ax;" // clear keyboard buffer: - "int $0x16;" // http://www.ctyme.com/intr/rb-1759.htm + "mov %%al, %%bl;" // temp store char in bl + "mov $0x0400, %%ax;" // clear keyboard buffer: + "int $0x16;" // http://www.ctyme.com/intr/rb-1759.htm : "=b"(ret) : - : "eax"); + : "ax"); return ret; } @@ -94,8 +95,9 @@ void main(void) { : "ax"); while (1) { - char buf[9]; - for (unsigned char i = 0; i < 8; i++) { + char buf[9]; // 8 chars + \0 + unsigned char i = 0; + for (; i < 8; i++) { // read at most 8 chars char c = getc(); if (c == '\r') { if (i == 0) { // empty entry @@ -114,12 +116,16 @@ void main(void) { break; } } - buf[i] = c; - buf[i + 1] = 0; - putc_color('*', 0x02); // hard coded green on black + buf[i] = c; // buffer the char + buf[i + 1] = 0; // set the following to \0 + putc_color('*', COLOR(GREEN, BLACK)); // hard coded green on black + } + + if (i == 8) { + while (getc() != '\r') + ; // wait for enter press, ignoring other keys } - while (getc() != '\r') - ; + new_line(); print(buf); new_line(); -- cgit v1.2.3-54-g00ecf