How to read input in C? (Kernel)

Viewed 180

I want to read input in C (Kernel). Reading input is so easy in C with standard library, but in C without standard library, I just found this link to get input. But I don't know how can I use this code in my kernel.

Here is my codes:

boot/boot.asm:

    BITS 32

global k_start
extern k_main

dd 0x1BADB002
dd 0x00
dd - (0x1BADB002 + 0x00)

k_start:
    call k_main

    hlt

include/clr.h:

#ifndef _CLR_H
# define _CLR_H
#endif

void clr()
{
    move_cursor(0, 0);
    int i = 0;
    while (video[i])
    {
        video[i] = '\0';
        i++;
    }
}

include/cursor.h:

#ifndef _CURSOR_H
# define _CURSOR_H
#endif

void move_cursor(int x, int y)
{
    int cursor_position = y * 80 + x;
    outb(0x3D4, 14);
    outb(0x3D5, cursor_position >> 8);
    outb(0x3D4, 15);
    outb(0x3D5, cursor_position);
}

include/io.h:

#ifndef _IO_H
# define _IO_H
#endif

void outb(unsigned short port, unsigned char value)
{
    asm("outb %0, %1"
        :
        :"a"(value),
        "Nd"(port));
}

void printcol_char(char c, unsigned char fg, unsigned char bg, int x, int y)
{
     unsigned short attrib = (bg << 4) | (fg & 0x0F);
     unsigned short *position;
     position = (unsigned short *)0xB8000 + (y * 80 + x) ;
     *position = c | (attrib << 8);
}

void printcol_str(char *str, unsigned char fg, unsigned char bg, int x, int y)
{
    int i = 0;
    while (str[i])
    {
        printcol_char(str[i], fg, bg, x + i, y);
        i++;
    }
}

include/kernel/kernel.h:

#ifndef _KERNEL_H
# define _KERNEL_H
#endif

int i;
char *video = (char *)0xB8000;

include/time.h:

#ifndef _TIME_H
# define _TIME_H
#endif

void wait(int time_count)
{
    while (1)
    {
        asm("nop");
        time_count--;
        if (time_count <= 0)
        {
            break;
        }
    }
}

void sleep(int timer_count)
{
    wait(timer_count * 0x02FFFFFF);
}

kernel/kernel.c:

#include "../include/kernel/kernel.h"
#include "../include/io.h"
#include "../include/time.h"
#include "../include/cursor.h"
#include "../include/clr.h"

int k_main()
{
    clr();
    printcol_str("Booting kernel...", 15, 0, 0, 0);
    move_cursor(14, 0);
    sleep(3);
    move_cursor(15, 0);
    sleep(3);
    move_cursor(16, 0);
    sleep(3);
    move_cursor(15, 0);
    sleep(3);
    move_cursor(14, 0);
    sleep(3);
    move_cursor(15, 0);
    sleep(3);
    move_cursor(16, 0);
    clr();
    printcol_str(">  ", 9, 0, 0, 0);
    move_cursor(2, 0);
}

link.ld:

OUTPUT_FORMAT(elf32-i386)
ENTRY(k_start)
SECTIONS
{
    . = 1M;
    .text BLOCK(4K) : ALIGN(4K)
    {
         *(.text)
    }
    .data : { *(.data) }
    .bss  : { *(.bss) }
    /DISCARD/:
    {
        *(.comment)
        *(.eh_frame)
    }
}

Makefile:

ASM=nasm
CC=gcc
LD=ld

C_SOURCES= \
    kernel/kernel.c

OBJECTS= \
    boot/boot.o \
    kernel/kernel.o

all: KGKernel1

KGKernel1: $(OBJECTS)
    $(LD) -m elf_i386 -T link.ld $(OBJECTS) -o KGKernel1

boot/boot.o: boot/boot.asm
    $(ASM) -f elf32 boot/boot.asm -o boot/boot.o

kernel/kernel.o: kernel/kernel.c
    $(CC) -m32 -c $(C_SOURCES) -o kernel/kernel.o

clean:
    rm boot/boot.o kernel/kernel.o

Sorry about long code.

0 Answers
Related