Help in building an 16 bit os

落爺英雄遲暮 提交于 2019-12-06 06:27:26

问题


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?


回答1:


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.)




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!