GNU GAS:标签未得到相对引用 [英] GNU GAS: Label is not relatively referenced

查看:116
本文介绍了GNU GAS:标签未得到相对引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个用于学习目的的引导程序.

I am writing a little bootsector for learning purpose.

这里是引导.S

.code16
.text
    movw    $0xB800, %ax    /* 0xB000 is the text screen video memory */
    movw    %ax, %es       /* set it as the %es segment */

    movb    label, %al
    movb    %al, %es:0x0    /* screen[0] = 'A' */
    movb    $0x07, %es:0x1  /* white on black */
    jmp .

label:  
    .byte 'A

    .=510
    .byte 0x55
    .byte 0xAA

这是我用来将其编译为原始二进制文件的Makefile

and here is the Makefile I use to compile it to a raw binary file

hdd.img: boot.S
    as $< -o boot.o
    ld --oformat binary -Ttext 0x7c00 boot.o -o hdd.img

我面临标签没有被相对引用的问题:当加载字节"A"时,它使用绝对地址,例如0x7c14).

I face the problem that the label is not relatively referenced: when loading the byte 'A', it uses the absolute address, say 0x7c14).

因此,我无法在运行时重定位此引导扇区(例如,通过将其进一步复制到内存中).如果仅通过与当前指令的偏移量来引用标签,那会更好.

So I cannot relocate this bootsector at runtime (for example by copying it further in memory). It would be better if the label was simply referenced trough an offset from the current instruction.

可以这样做吗?

推荐答案

当然可以重定位字符串.

Of course it is possible to relocate strings.

首先,您的-Ttext 0x7C00是正确的.请勿更改. 在引导程序开始时,应将段寄存器(%ds%es%fs%gs%ss)清零:

First of all, your -Ttext 0x7C00 is correct. Do not change it. At the beginning of your bootloader you should zero the segment registers (%ds, %es, %fs, %gs and %ss):

mov $0, %ax         // xor %ax, %ax would be more space efficient
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss

接下来,您应该设置一个堆栈.阅读:关于堆栈的osdev Wiki

Next, you should set up a stack. Read: osdev wiki about stacks

现在您的问题是: 通常以%ds:%si的形式使用分段来处理字符串.重新放置引导加载程序时,只需正确更改%ds.
假设您调用字符串label:

Now to your question: It is usual to address strings using segmentation in the form of %ds:%si. When relocating the bootloader, just change %ds properly.
Assuming you call your string label:

mov $label, %si     // Always prefix a label address with a $

您可以使用lodsb指令从字符串中获取字符( LO a D S 调整 B yte,它会自动为您增加%si:

You can get a character from a string using the lodsb instruction (LOaD String Byte, which automatically increments %si for you:

lodsb               // Character is stored in `%al` now

另一建议是不要手动处理视频存储器.使用 BIOS中断0x10 .

Another recommendation is not to address the video memory manually. Use BIOS Interrupt 0x10.

引导加载程序祝你好运!

Good luck with your bootloader!

这篇关于GNU GAS:标签未得到相对引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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