I am trying to build an old 16 bit DOS like OS.
My example kernel code:
asm(".code16\n");
void putchar(char);
int main()
{
putchar('A');
return 0;
}
void putchar(char val)
{
asm("movb %0, %%al\n"
"movb $0x0E, %%ah\n"
"int $0x10\n"
:
:"r"(val)
) ;
}
This is how I compile it :
nasm -f bin -o ./bin/boot.bin ./source/boot.asm
gcc -nostdinc -fno-builtin -I./include -c -o ./bin/kernel.o ./source/kernel.c
ld -Ttext=0x9000 -o ./bin/kernel.bin ./bin/kernel.o -e 0x0
dd if=/dev/zero of=./bin/empty.bin bs=1440K count=1
cat ./bin/boot.bin ./bin/kernel.bin ./bin/empty.bin|head -c 1440K > ./bin/os
rm ./bin/empty.bin
and I run it in a virtual machine.
When I make the putchar function ( in kernel code ) for constant value ....i.e like this:
void putchar()
{
char val = 'A';
asm("movb %0, %%al\n"
"movb $0x0E, %%ah\n"
"int $0x10\n"
:
:"r"(val)
) ;
}
then it works fine. But when I pass argument to it (That is in the previous code), then it prints a space for any character. What should I do?
If you're trying to make gcc generate 16-bit code, you need to use .code16gcc
rather than .code16
, in order to get the stack manipulation right for function parameters: see "Writing 16-bit Code" in the gas documentation.
(Also, depending on which gcc version you're using, you may need to compile your kernel.c
with either the -fno-unit-at-a-time
or -fno-toplevel-reorder
option, to ensure that the asm()
directive really is encountered by the assembler before any compiled code - otherwise the compiler may shuffle things around.)
I find another compiler to solve the problem: bcc
This is a C-compiler for 8086 cpus which is important for the development of boot loaders or BIOS related 8086 code.
来源:https://stackoverflow.com/questions/2713340/help-in-building-an-16-bit-os