'After initializing a dynamic char array, all signs are equal to zeros [closed]

I'm trying to write the kernel in C, without stdlib and other libraries, all dynamic char arrays written in advance and without using calloc or malloc, here is an example code:

char* d="hello world!!!";
print_char(d[0],-1,-1,RED_ON_WHITE);

int print_char(char c, int col, int row, char attr) {
    unsigned char *vidmem = (unsigned char*) VIDEO_ADDRESS;
    int offset;
    if (col >= 0 && row >= 0) offset = get_offset(col, row);
    else offset = get_cursor_offset();

    vidmem[offset] = c;
    vidmem[offset+1] = attr;
    offset += 2;

    set_cursor_offset(offset);
    return offset;
}

int get_cursor_offset() {
    port_byte_out(REG_SCREEN_CTRL, 14);
    int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
    port_byte_out(REG_SCREEN_CTRL, 15);
    offset += port_byte_in(REG_SCREEN_DATA);
    return offset * 2; /* Position * size of character cell */
}

void set_cursor_offset(int offset) {
    /* Similar to get_cursor_offset, but instead of reading we write data */
    offset /= 2;
    port_byte_out(REG_SCREEN_CTRL, 14);
    port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset >> 8));
    port_byte_out(REG_SCREEN_CTRL, 15);
    port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset & 0xff));
}

int get_offset(int col, int row) { return 2 * (row * MAX_COLS + col); }
int get_offset_row(int offset) { return offset / (2 * MAX_COLS); }

It does not print anything, but what is generated in a non-dynamic array prints.

Compile on Windows:

gcc -g -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -c kernel/kernel.c -o kernel/kernel.o -std=c99
gcc -g -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -c kernel/util.c -o kernel/util.o -std=c99
gcc -g -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -c drivers/ports.c -o drivers/ports.o -std=c99
gcc -g -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -c drivers/screen.c -o drivers/screen.o -std=c99
nasm -fbin boot/bootsect.asm -o boot/bootsect.bin
nasm -felf boot/kernel-entry.asm -o boot/kernel-entry.o
ld -mi386pe -o kernel.elf -Ttext 0x1000 boot/kernel-entry.o kernel/kernel.o kernel/util.o drivers/ports.o drivers/screen.o
objcopy -O binary kernel.elf kernel.bin
type boot\\bootsect.bin kernel.bin > os-image.bin
qemu-system-i386 -fda os-image.bin
c


Solution 1:[1]

You have specified -nostartfiles making you responsible for the C runtime environment start-up and initialisation. Part of that initialisation is to implement the static initialisers. Without that:

char* d = "hello world!!!" ;

will do nothing. It is not clear which if any of the object modules you have linked if any contain the start-up code. By convention there would be a crt0.o for that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1