0

I am writing an OS in assembly and cpp. I encountered a problem regarding char pointers in cpp: for example, the following code:

#define VIDEO_MEMORY 0xB8000
#define COLUMNS 80
#define ROWS 25
#include "screen.h"
#include <stdint.h>

const char scancodes[] = {'\0','%','1','2','3','4','5','6','7','8','9','0',
'-','=','~','\t','Q','W','E','R','T','Y','U','I','O','P','[',']','\e','\0','A', 'S','D',
'F','G','H','J','K','L',';','\0','`',
'\0','\\','Z','X','C','V','B','N','M',',','.',
'/','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',
'\0','\0','\0','\0','\0','\0','\0','\0','-','\0','\0','\0','+','\0','\0','\0','\0','~',};

void print_keyboard(int code)
{
    set_char_at_video_memory(scancodes[code], 0);
}


void main() {
    clear_screen();
    set_char_at_video_memory('h', 0);


    while(1)
    {
        __asm__("hlt");
    }
}



works perfectly but when I slightly change it so that now I use char pointers (strings) the code no longer seems to work. It is as though the condition turns out false.

#define VIDEO_MEMORY 0xB8000
#define COLUMNS 80
#define ROWS 25
#include "screen.h"
#include <stdint.h>

const char scancodes[] = {'\0','%','1','2','3','4','5','6','7','8','9','0',
'-','=','~','\t','Q','W','E','R','T','Y','U','I','O','P','[',']','\e','\0','A', 'S','D',
'F','G','H','J','K','L',';','\0','`',
'\0','\\','Z','X','C','V','B','N','M',',','.',
'/','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',
'\0','\0','\0','\0','\0','\0','\0','\0','-','\0','\0','\0','+','\0','\0','\0','\0','~',};

void print_keyboard(int code)
{
    set_char_at_video_memory(scancodes[code], 0);
}


void main() {
    clear_screen();
    const char* c = "hello";
    if (c[0] == 'h')
        set_char_at_video_memory('h', 0);


    while(1)
    {
        __asm__("hlt");
    }
}


it doesnt work at all

this is my linker script:

ENTRY(main)
OUTPUT_FORMAT(binary)

SECTIONS
{
    . = 0x1000; /* Set the starting address of the kernel */

    .text : { *(.text) } /* Code section */

    .rodata : { *(.rodata) } /* Read-only data section */

    .data : { *(.data) } /* Initialized data section */

    .bss : { *(.bss) } /* Uninitialized data section */

    /DISCARD/ : { *(.eh_frame) } /* Discard unnecessary sections */
}

I've tried altering my linker file to no success. If anyone could help me solve this issue I would be very grateful.

4
  • This is the part where you turn to a debugger and step through your code instruction by instruction. In what section did the compiler put your "hello" string? What address did it put in the code? Is it still present in your final binary? Is that address still valid after the linker processed your program? Is the string located at the expected address when you step into your code? You cannot write an OS without knowing how to check these low-level details. Commented Oct 12, 2023 at 12:46
  • @Botje, thanks man, just downloaded GDB and getting to work Commented Oct 12, 2023 at 16:02
  • Why do you have a halt instruction in a while loop? It's giving me a headache. Do you expect execution to come back from a halt instruction? Commented Oct 12, 2023 at 20:06
  • Your program is not working for me because I can't get it to compile. Please edit the code to a minimal reproducible example. Commented Oct 12, 2023 at 20:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.