编写一个简单的引导加载程序HelloWorld - 错误函数打印字符串 [英] Write a simple Bootloader HelloWorld - Error function print string

查看:205
本文介绍了编写一个简单的引导加载程序HelloWorld - 错误函数打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着创建一个打印hello world的简单引导程序。



当我调用一个只打印hello world的函数时,我可以做到这一点,但当我调用一个函数打印一个特定的字符串时,什么都没有发生。



对于它,我使用两个文件。第一个是boot.ld,第二个是boot.cpp(它也可以在C中用boot.c工作)。



首先,我从我的磁盘创建软盘终端:

lockquote
dd if = / dev / zero of = floppy.img bs = 512 count = 2880

其次,我编译代码(boot.cpp和boot.ld):


gcc -c -g -Os -m64 -wreestanding -Wall -Werror boot.cpp -o boot.o

ld -static -Tboot.ld -nostdlib --nmagic -o boot.elf boot.o
$ b $ objcopy -O binary boot.elf boot.bin

最后,我将boot.bin添加到floppy.img中:


if if = boot.bin of = floppy.img

现在我们只需要从VirtualBox的存储面板添加软盘并启动我们的虚拟机。





源代码

from:

有问题的源代码



boot.cpp(或boot.c)

  void cout(const char * str); 

void main()
{
cout(hello world);
}

void cout(const char * str)
{
while(* str)
{
__asm__ __volatile__(int $ 0 x10::a(0x0e00 | * str),b(0x0007));
++ str;
}
}

输出:





为什么输出为空?



我的函数出了什么问题?

我忘记了什么?

感谢您的帮助。

解决方案

感谢@


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.

解决方案

Thanks to @MichaelPetch for this answer.

Source code:

boot.ld

ENTRY(main);
SECTIONS
{
    . = 0x7C00;
    .text : AT(0x7C00)
    {
        *(.text);
    }
    .sig : AT(0x7DFE)
    {
        SHORT(0xaa55);
    }
}

boot.cpp

Also here: http://pastebin.com/6NV3UMjE

asm(".code16gcc");
__asm__("jmpl $0x0000, $main\n");

void cout(const char* str);

void main()
{
    __asm__ __volatile__ ("xor %ax, %ax\n");
    __asm__ __volatile__ ("mov %ax, %ds\n");
    cout("Hello World");
    __asm__ __volatile__("cli\n");
    __asm__ __volatile__("hlt\n");
}

void cout(const char* str)
{
    while(*str)
    {
        __asm__ __volatile__("int $0x10" : : "a"(0x0e00 | *str), "b"(0x0007));
        ++str;
    }
}

Compile:

gcc -c -g -O0 -m32 -ffreestanding -Wall -Werror boot.cpp -o boot.o

ld -melf_i386 -static -Tboot.ld -nostdlib --nmagic -o boot.elf boot.o

objcopy -O binary boot.elf boot.bin

dd if=boot.bin of=floppy.img conv=notrunc

Output:

这篇关于编写一个简单的引导加载程序HelloWorld - 错误函数打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆