简体   繁体   中英

How can I use C char arrays under real mode

I'm developing a bare-bone operating system in real mode. I have written my bootloader under assembly. But I want to write my kernel with C language. So far everything was nice but I notice that when I use char arrays (not pointers) for function parameters, they are not working. For example when I use:

puts("Hello, World!");

or,

char *ptr = "Hello, World!";
puts(ptr);

these are working. But when I do this:

char s[]="Hello, World!";
puts(s);

It's give me nothing. I want to know, why this happens and how can I use char arrays in this way? Also this is my "puts" function:

void puts(char* s){
for(; *s!=0; s++)
    asm volatile ("int $0x10": : "a"(0x0e00 | *s));
}

EDIT: I'm compiling the using GCC with:

gcc -std=gnu99 -Os -nostdlib -m16 -march=i386 -ffreestanding -o kernel.bin -Wl,--nmagic,--script=raw_image.ld kernel.c

And this is the linker script:

OUTPUT_FORMAT(binary);
ENTRY(main);
SECTIONS
{
. = 0x0000;
.text :
{
    *(.text);
}
.data :
{
    *(.data);
    *(.bss);
    *(.rodata);
}
}

Also these are the web sites I used:

Building DOS COM files using GCC

Writing Bootloader in Assembly and C

You have to look at disassembler output for the resulting binary file. It might be that GCC targeting freestanding 386 did something unexpected with segments or whatever.

If that doesn't help much, you still have an option of using eg Bochs to run your OS there and use Bochs' integrated debugger to find out what actually happens when the code runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM