如何编译汇编文件到原始的二进制(类似DOS的.COM)与GNU汇编格式()? [英] How to compile an assembly file to a raw binary (like DOS .com) format with GNU assembler (as)?
问题描述
我要编译在Windows这个源$ C $ C(这只是一个例子):
I want to compile this source code in Windows (It just an example):
start:
NOP
NOP
当我用NASM或FASM编译后,输出文件lenght为2个字节,但是当我用GNU汇编编译(AS)的输出中的文件lenght为292个字节!
When I compile it with nasm or fasm, ouput file lenght is 2 bytes but when I compile it with GNU assembler (as) the ouput file lenght is 292 bytes!
如何编译汇编文件到原始的二进制(类似DOS的.COM)与GNU汇编格式()?
How to compile an assembly file to a raw binary (like DOS .com) format with GNU assembler (as)?
我为什么要这样做呢?
我想要写我自己的简单的操作系统,我写我的codeS用C(不使用任何C标准库甚至stdio.h中或文件math.h),并将其转换为汇编:
I want to write my own simple OS, I write my codes with C (without using any C standard libraries even stdio.h or math.h) and convert it to assembly:
gcc -S my_os.c -o my_os.asm -masm=intel
然后,我编译汇编文件到原始的二进制:
Then, I compile assembly file to a raw binary:
as my_os.asm
然后我重新命名的a.out
(汇编程序的输出) my_os.flp
终于开始我用的操作系统VMWare的:)
Then I rename a.out
(output of assembler) to my_os.flp
and finally start my os with VMWare :)
推荐答案
- oformat二进制作为快速和肮脏的测试:
--oformat binary for quick and dirty tests:
as -o a.o a.S
ld --oformat binary -o a.out a.o
hd a.out
给出:
00000000 90 90 |..|
00000002
不幸的是这给出了一个警告:
Unfortunately this gives a warning:
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400000
这不会与多大意义的二进制
。这可能与被沉默:
.section .text
.globl start
start:
nop
nop
和
ld -e start --oformat binary -o a.out a.o
或者干脆:
ld -e 0 --oformat binary -o a.out a.o
它告诉 LD
的入口点不在 _start
,但在地址code 0
。
which tells ld
that the entry point is not _start
but the code at address 0
.
这是一个耻辱,无论是为
也不 LD
可以输入/从标准输入/输出ouptut,所以没有管道。
It is a shame that neither as
nor ld
can take input / ouptut from stdin / stdout, so no piping.
正确引导扇区
如果你要的东西更严重,最好的方法是,产生一个干净最小的链接脚本。 linker.ld
:
If you are going to to something more serious, the best method is to generate a clean minimal linker script. linker.ld
:
SECTIONS
{
. = 0x7c00;
.text :
{
*(.*)
. = 0x1FE;
SHORT(0xAA55)
}
}
在这里我们也把魔术字节的链接脚本。
Here we also place the magic bytes with the linker script.
链接器脚本首先要搬迁后控制输出的地址是很重要的。了解更多关于在搬迁: http://stackoverflow.com/a/30507725/895245
The linker script is important above all to control the output addresses after relocation. Learn more about relocation at: http://stackoverflow.com/a/30507725/895245
使用它作为:
as -o a.o a.S
ld --oformat binary -o a.img -T linker.ld a.o
然后你就可以引导为:
And then you can boot as:
qemu-system-i386 -hda a.img
在这个仓库工作的例子:<一href=\"https://github.com/cirosantilli/x86-bare-metal-examples/blob/d217b180be4220a0b4a453f31275d38e697a99e0/Makefile\" rel=\"nofollow\">https://github.com/cirosantilli/x86-bare-metal-examples/blob/d217b180be4220a0b4a453f31275d38e697a99e0/Makefile
Working examples on this repository: https://github.com/cirosantilli/x86-bare-metal-examples/blob/d217b180be4220a0b4a453f31275d38e697a99e0/Makefile
测试上的Binutils 2.24,Ubuntu的14.04。
Tested on Binutils 2.24, Ubuntu 14.04.
这篇关于如何编译汇编文件到原始的二进制(类似DOS的.COM)与GNU汇编格式()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!