2

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
sbh
  • 65
  • 1
  • 9
  • 1
    [The OSDev wiki](https://wiki.osdev.org/Main_Page) should have all the information you might need. – Some programmer dude Apr 14 '21 at 09:16
  • 2
    With that said, none of the code you show is really relevant to the problem you're trying to solve. Please take some time to take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And when creating a [mcve], please remember the *minimal* bit. – Some programmer dude Apr 14 '21 at 09:17
  • @Someprogrammerdude "I don't know how can I use this code in my kernel." – sbh Apr 14 '21 at 09:19
  • @Someprogrammerdude I want to know how can I use this code (GitHub link) in my code. – sbh Apr 14 '21 at 09:21
  • 1
    Please don't fall into the [cargo cult](https://en.wikipedia.org/wiki/Cargo_cult_programming) trap. Read the wiki, and figure out how to do it yourself. That will teach you much more, and also make it easier to customize it for your own specific needs. – Some programmer dude Apr 14 '21 at 09:22
  • 1
    That is highly dependent on your hardware and what exactly you want to read input from. There are some BIOS routines that are fairly easy to use that allow you to read keystrokes, and show output on a console, but those are basically legacy artifacts from a long time ago. There's no reason to use those anymore. There are excellent books about how the Linux kernel works. I recommend reading a few instead of copy-pasting code you found. – Emanuel P Apr 14 '21 at 10:29

0 Answers0