AT & T , - :
.global _start
.text
.code16
_start:
jmp .
.space 510-(.-_start)
.word 0xaa55
.
- . .
_start
, .
GCC ( LD), , :
gcc -Wl,--oformat=binary -Wl,-Ttext=0x7c00 -Wl,--build-id=none \
-nostartfiles -nostdlib -m32 -o boot.bin boot.s
-Wl,--oformat=binary
, . -Wl,-Ttext=0x7c00
, 0x07c00. -Wl,--build-id=none
, , GCC. 0x7c00 - , , , . C runtime, -nostartfiles -nostdlib
, . script. , .
a > . . 0x7c00, , , , DS 0. , , , .
GNU , . .code16
, 16- . .code32
32- , .code64
64- . as
.code16
.
, , . script, Origin 0x7c00 . , - , . script, , .text
, .data
, .rodata
. , :
bootloader.ld
OUTPUT_FORMAT("elf32-i386");
ENTRY(_start);
SECTIONS
{
. = 0x7C00;
.text : SUBALIGN(0) {
*(.text.bootentry);
*(.text)
}
.rodata : SUBALIGN(0) {
*(.rodata)
}
.data : SUBALIGN(0) {
*(.data)
}
.sig : AT(0x7DFE) {
SHORT(0xaa55);
}
/DISCARD/ : {
*(.eh_frame);
*(.comment);
*(.note*);
}
}
boot.s
, :
.section .text.bootentry
.code16
.global _start
_start:
xor %ax, %ax
mov %ax, %ds
mov %ax, %ss
mov $_start, %sp
cld
mov $0x20, %al
xor %cx, %cx
mov $boot_msg, %dx
call print_str
cli
.endloop:
hlt
jmp .endloop
.section .rodata
boot_msg: .asciz "My bootloader is running"
aux.s
:
.global print_str
.section .text
.code16
print_str:
push %di
push %si
mov $0xb800, %di
mov %di, %es
mov %cx, %di
mov %dx, %si
mov %al, %ah
jmp .testchar
.nextchar:
stosw
.testchar:
lodsb
test %al, %al
jne .nextchar
pop %si
pop %di
ret
boot.bin
, - :
as --32 aux.s -o aux.o
as --32 boot.s -o boot.o
ld -melf_i386 --oformat=binary -Tlink.ld -nostartfiles -nostdlib \
aux.o boot.o -o boot.bin
.text.bootentry
script. , , 0x7c00. script VMA () 0x7dfe (0xaa55). 0x7dfe - 2 512 . .
.