该程序集引导加载程序代码如何工作? [英] How does this assembly bootloader code work?

查看:85
本文介绍了该程序集引导加载程序代码如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件(kernel.asm)中具有以下代码:

I have the following code in a file (kernel.asm):

bits 32
section .text
        ;multiboot spec
        align 4
        dd 0x1BADB002               ;magic
        dd 0x00                     ;flags
        dd - (0x1BADB002 + 0x00)    ;checksum. m+f+c should be zero

global start
extern k_main                       ;this is defined in the c file

start:
    cli                             ;block interrupts
    mov esp, stack_space            ;set stack pointer
    call k_main
    hlt                             ;halt the CPU

section .bss
resb 8192                           ;8KB for stack
stack_space:


align 4
dd 0x1BADB002               ;magic
dd 0x00                     ;flags
dd - (0x1BADB002 + 0x00)    ;checksum. m+f+c should be zero

我已经测试了代码,并将其与内核链接,并且可以正常工作.

I have tested the code, linked it with the kernel and it works fine.

align 4是什么意思?我认为这与记忆有关.

What does align 4 mean? I think it has something to do with memory.

如果dd 0x1BADB002行定义了一个十六进制地址,表示错误引导",那么如果操作系统加载正常,为什么会出现在该地址中?

If the line dd 0x1BADB002 defines a hexadecimal address meaning 'bad boot', why is it there if the operating system loads fine?

下一行dd 0x00,我假设将所有标志都设置为0?

The next line dd 0x00, I'm assuming sets all of the flags to 0?

dd - (0x1BADB002 + 0x00):似乎与第二行在地址0xBADB002上加0的行为相似.括号之前的减号是什么意思?减号是否表示要减去某物?如果是这样,如果没有什么可以减去的,该如何减去呢?另外,为什么将0添加到0xBADB002?与0xBADB002是不是同一回事?是否添加它会有所不同吗?

dd - (0x1BADB002 + 0x00): seems to be doing a similar thing to the second line adding 0 to the address 0xBADB002. What does the minus mean before the brackets? Does the minus mean that something is being subtracted? If so, how can something be subtracted if there is nothing to subtract it from? Also, why is 0 being added to 0xBADB002? Isn't it the same thing as 0xBADB002? Does it make a difference if it is added or not?

我也很困惑为什么要这样做,因为它是32位并且计算机以16位实模式启动.计算机只是在执行32位代码并调用内核吗?

I'm also confused as to why this works, because it's in 32 bit and the computer starts in 16 bit real mode. Is the computer just executing the 32 bit code and calling the kernel?

预先感谢

推荐答案

有一个使用引导加载程序来加载各种x86内核的标准.称为Multiboot规范.

There is a standard for loading various x86 kernels using a boot loader; called as Multiboot specification.

GRUB仅在符合Multiboot规范的情况下才会加载我们的内核.

GRUB will only load our kernel if it complies with the Multiboot spec.

根据规范,内核必须在其前8 KB中包含一个标头(称为Multiboot标头).

According to the spec, the kernel must contain a header (known as Multiboot header) within its first 8 KiloBytes.

此外,此Multiboot标头必须包含4个字节对齐的3个字段,即:

Further, This Multiboot header must contain 3 fields that are 4 byte aligned namely:

a magic field: containing the magic number 0x1BADB002, to identify the header.
a flags field: We will not care about this field. We will simply set it to zero.
a checksum field: the checksum field when added to the fields ‘magic’ and ‘flags’ must give zero.

这篇关于该程序集引导加载程序代码如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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