- forormat binary code for quick and dirty tests:
as -o ao aS ld --oformat binary -o a.out ao hd a.out
gives:
00000000 90 90 |..| 00000002
Unfortunately, this gives a warning:
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400000
which doesn't make much sense with binary . It can be disabled:
.section .text .globl start start: nop nop
and
ld -e start
or just using:
ld -e 0
which tells ld that the entry point is not _start , but the code at address 0 .
It is a pity that neither as nor ld can accept input / output data from stdin / stdout, so there are no pipelines.
The correct boot sector
If you are going to do something more serious, the best way is to create a clean minimal script linker. linker.ld :
SECTIONS { . = 0x7c00; .text : { *(.*) . = 0x1FE; SHORT(0xAA55) } }
Here we also put magic bytes with a script linker.
The script component is important primarily for managing output addresses after moving. Learn more about moving ... fooobar.com/questions/4539 / ...
Use it as:
as -o ao aS ld --oformat binary -o a.img -T linker.ld ao
And then you can boot as:
qemu-system-i386 -hda a.img
Working examples in this repository: https://github.com/cirosantilli/x86-bare-metal-examples/blob/d217b180be4220a0b4a453f31275d38e697a99e0/Makefile
Tested on Binutils 2.24, Ubuntu 14.04.