初始化动态char数组后,所有符号都等于零



我正在尝试用C编写内核,不使用stdlib和其他库,所有动态char数组都是提前编写的,不使用calloc或malloc,下面是一个示例代码:

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); }

它不打印任何内容,但打印非动态数组中生成的内容。

在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

您已指定-nostartfiles,使负责C运行时环境的启动和初始化。初始化的一部分是实现静态初始化程序。没有这个:

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

什么都不会做。目前尚不清楚您链接的对象模块中的哪一个(如果有的话(包含启动代码。按照惯例,会有一个crt0.o。

最新更新