I try to create a simple bootloader which print "hello world".
I can do it when I call a function which only print "hello world", but when I call a function to print a specific string, nothing is happening.
For it, I use two files. The first one is boot.ld and the second is boot.cpp (it also work in C with boot.c).
Firstly, I create the floppy disk from my terminal:
dd if=/dev/zero of=floppy.img bs=512 count=2880
Secondly, I compile the code (boot.cpp and boot.ld):
gcc -c -g -Os -m64 -ffreestanding -Wall -Werror boot.cpp -o boot.o
ld -static -Tboot.ld -nostdlib --nmagic -o boot.elf boot.o
objcopy -O binary boot.elf boot.bin
Lastly, I add boot.bin into floppy.img:
dd if=boot.bin of=floppy.img
Now we just need to add the floppy from the storage panel of VirtualBox and launch our Virtual Machine.
The source code
from: http://www.codeproject.com/Articles/664165/Writing-a-boot-loader-in-Assembly-and-C-Part
boot.ld
ENTRY(main);
SECTIONS
{
. = 0x7C00;
.text : AT(0x7C00)
{
*(.text);
}
.sig : AT(0x7DFE)
{
SHORT(0xaa55);
}
}
boot.cpp (or boot.c)
void cout();
void main()
{
cout();
}
void cout()
{
__asm__ __volatile__("movb $'h' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'e' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'l' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'l' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'o' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $' ' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'w' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'o' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'r' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'l' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
__asm__ __volatile__("movb $'d' , %al\n");
__asm__ __volatile__("movb $0x0e, %ah\n");
__asm__ __volatile__("int $0x10\n");
}
Output:
The bugged source code
boot.cpp (or boot.c)
void cout(const char* str);
void main()
{
cout("hello world");
}
void cout(const char* str)
{
while(*str)
{
__asm__ __volatile__ ("int $0x10" : : "a"(0x0e00 | *str), "b"(0x0007));
++str;
}
}
Output:
Why the output is empty?
What is wrong in my function?
I have forget something?
Thanks for your help.