2012-03-07 12 views
3

在我努力理解如何使用GNU binutils构建一个简单的引导装载程序时使用了 gas我遇到过这个问题,你如何告诉链接器将数据放在哪里,放在一个使用.org的文件中,以将文件大小保持在512字节的同时前进位置计数器。我似乎无法找到办法做到这一点。在.data部分使用.org指令来处理数据:关于ld

,试图做到这一点的汇编程序是:

# Author: Matthew Hoggan 
# Date Created: Tuesday, Mar 6, 2012 

.code16      # Tell assembler to work in 16 bit mode 
.section .data 
msg:       # Message to be printed to the screen 
    .asciz "hello, world" 

.section .text 
.globl _start    # Help linker find start of program 
_start: 
    xor %ax, %ax    # Zero out ax register (ah used to specify bios function to Video Services) 
    movw %ax, %ds    # Since ax is 0x00 0x00 use it to set data segment to 0 
    mov $msg, %si    # Use source index as pointer to string 

loop: 
    movb %ds:(%si), %al  # Pass data to BIOS function in low nibble of ax 
    inc %si     # Advance the pointer 
    or %al, %al    # If byte stored in al is equal to zero then... 
    jz _hang     # Zero signifies end of string 
    call print_char   # Print current char in al 
    jmp loop     # Repeat 

# 
# Function that uses bios function calls 
# to print char to screen 
# 
print_char: 
    movb $0x0e, %ah    # Function to print a character to the screen 
    movb $0x07, %bl    # color/style to use for the character 
    int $0x10     # Video Service Request to Bios 
    ret 

# 
# Function used as infinite loop 
# 
_hang: 
    jmp _hang 

.org 510 
.byte 0x55, 0xAA 

.end 

UPDATE 使用以下命令我得到以下错误:

[email protected]:~/Code/svn_scripts/assembly/bootloader/gas$ ld --oformat binary -o string string.o 
string.o: In function `_start': 
(.text+0x5): relocation truncated to fit: R_X86_64_16 against `.data' 
+0

生成此错误的最小示例及其含义:http://stackoverflow.com/a/32639540/895245具有工作引导扇区和BIOS示例的存储库:https://github.com/cirosantilli/x86-bare- metal-examples – 2015-09-17 20:49:10

回答

4

对于这方面的工作你应该写你自己的linker script。只需将它传递给-T选项的链接器即可。

我对几乎相同的问题脚本是:

SECTIONS 
{ 
    . = 0x1000; 
    bootsec : 
    { 
     *(.text) 
     *(.data) 
     *(.rodata) 
     *(.bss) 
     endbootsec = .; 
     . = 0x1FE; 
     SHORT(0xAA55) 
    } 
    endbootsecw = endbootsec/2; 
    image = 0x1200; 
} 

的那套把戏你不活动需要把0x55 0xAA在汇编!

0x1000开头:其实所有的跳跃是相对的,这样就不会使用,但以后需要的跳转进入保护模式...

endbootsecwendbootsecimage的符号在其他地方使用在代码中。

+0

我得到:'a.ld:11无法使用Binutils 2.24上的链接器脚本向后移动位置计数器(从0000000000001200到00000000000011fe)。 – 2015-09-06 16:49:06

+0

”正在做我的链接器技巧和.org的东西在同一时间“DOH! – 2015-09-07 14:16:16

相关问题