3

I've just created a basic operating system that prints text onto the screen, based on this youtube tutorial. The method used in the tutorial is fairly convoluted (I assume) because it uses C++ instead of C. Upon selecting the OS in the Grub menu, the menu closes but the screen stays purple. No text is printed to the screen. I've double checked that my code matches the tutorial but it still doesn't work the same way as it does on his machine.

Relevant Hardware:

CPU: Ryzen 5 2600
MOBO: Asrock B450-M HDV

The files are fairly small, included below.

kernel.cpp:

void printf(char* str) {
    unsigned short* VideoMemory = (unsigned short*)0xb8000;

    for(int i = 0; str[i] != '\0'; ++i) {
        VideoMemory[i] = (VideoMemory[i] & 0xFF00) | str[i];
    }
}


extern "C" void kernelMain(void* multiboot_structure, unsigned int magicnumber) {
    printf("Hello World!");

    while(1);
}

loader.s: (since C++ expects the stack pointer to be set before running)

.set MAGIC, 0x1badb002
.set FLAGS, (1<<0 | 1<<1)
.set CHECKSUM, -(MAGIC + FLAGS)

.section .multiboot
    .long MAGIC
    .long FLAGS
    .long CHECKSUM

.section .text
.extern kernelMain
.global loader

loader:
    mov $kernel_stack, %esp
    push %eax
    push %ebx
    call kernelMain

_stop:
    cli
    hlt
    jmp _stop

.section .bss
.space 2*1024*1024; # 2 MB
kernel_stack:

linker.ld:

ENTRY(loader)
OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386:i386)

SECTIONS {
    . = 0x0100000;

    .text : {
        *(.multiboot)
        *(.text*)
        *(.rodata)
    }

    .data : {
        start_ctors = .;
        KEEP(*(.init_array));
        KEEP(*(SORT_BY_INIT_PRIORITY( .init_array.* )))
        end_ctors = .;

        *(.data)
    }

    .bss : {
        *(.bss)
    }

    /DISCARD/ : {
        *(.fini_array*)
        *(.comment)
    }
}

Makefile:

GPPPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore
ASPARAMS = --32
LDPARAMS = -melf_i386
objects = loader.o kernel.o

%.o: %.cpp
    g++ $(GPPPARAMS) -o $@ -c $<

%.o: %.s
    as $(ASPARAMS) -o $@ $<


mykernel.bin: linker.ld $(objects)
    ld $(LDPARAMS) -T $< -o $@ $(objects)

install: mykernel.bin
    sudo cp $< /boot/mykernel.bin
v1n
  • 31
  • 3
  • 2
    Possibly the BIOS is not leaving your graphics device in VGA text mode. – prl Sep 15 '20 at 15:49
  • Is there any way to do that through the code or as a compiler flag or something like that? Thanks! – v1n Sep 16 '20 at 02:12
  • 1
    I think grub has directives to set the graphics mode. In order to do it in your own code, you would need to switch to real mode so you can use int 10h, which is fairly complicated. – prl Sep 16 '20 at 04:09
  • 1
    Any chance this is on some strange hardware like an x86 chromebook? – Michael Petch Sep 16 '20 at 17:56
  • I don't think so, just a standard PC i put together a while ago. I should definitely include hardware details though. – v1n Sep 18 '20 at 00:37
  • As an experiment can you reduce the stack size from 2*1024*1024 to something smaller like 16*1024 and see what happens. – Michael Petch Sep 19 '20 at 15:18
  • 1
    @v1n Could you find the answer because I am having the same problem. – Ankit Chawla Mar 29 '21 at 14:35
  • @AnkitChawla nope, tried a whole bunch of stuff and couldn't find anything. Gave up and started learning C instead. – v1n Apr 10 '21 at 01:54

0 Answers0