在我努力了解如何使用 GNU binutils 来构建一个简单的引导加载程序时,使用gas我遇到过这样的问题,如何告诉链接器将数据放置在使用 .org 推进位置计数器的文件中,同时将文件大小保持在 512 字节。我似乎找不到办法做到这一点。
尝试执行此操作的汇编程序是:
# 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使用以下命令我收到以下错误:
mehoggan@mehoggan-laptop:~/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'
对于此类工作,您应该自己编写链接描述文件。只需将其传递给链接器即可-T
option.
我针对几乎相同问题的脚本是:
SECTIONS
{
. = 0x1000;
bootsec :
{
*(.text)
*(.data)
*(.rodata)
*(.bss)
endbootsec = .;
. = 0x1FE;
SHORT(0xAA55)
}
endbootsecw = endbootsec / 2;
image = 0x1200;
}
有了这个技巧,你甚至不需要把0x55 0xAA
在汇编器中!
The 0x1000
在开头:实际上所有的跳转都是相对的,所以没有使用它,但是稍后跳转到保护模式时需要它......
endbootsecw
, endbootsec
and image
是代码中其他地方使用的符号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)