views:

125

answers:

1

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  A: 

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

Matthew Slattery
Thank you. It worked. Now my putchar function is working properly.But a new problem came. the array and and pointer stuffs are not working.Example:char *str="Hello";putchar (str[0]).This is not working. Will you please give me some hint or links regarding this?
brainbarshan
Possibly some problem with the setup of the segment registers, and/or the code is not running at the address you think it is. The best way to debug this would be to run it in something which allows you to single-step through the code. I've heard that [Bochs](http://bochs.sourceforge.net/) and the debugging facilities it provides are supposed to be good for this sort of thing, although I haven't tried it myself.
Matthew Slattery