.code16
.global start
start:
movw %cs, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %ss
movw $0x7d00, %ax
movw %ax, %sp # setting stack pointer to 0x7d00
pushw $13 # pushing the size to print into stack
pushw $message # pushing the address of message into stack
callw displayStr # calling the display function
loop:
jmp loop
message:
.string "Hello, World!\n\0"
displayStr:
pushw %bp
movw 4(%esp), %ax
movw %ax, %bp
movw 6(%esp), %cx
movw $0x1301, %ax
movw $0x000c, %bx
movw $0x0000, %dx
int $0x10
popw %bp
ret
$gcc -c -m32 mbr.s -o mbr.o
$ld -m elf_i386 -e start -Ttext 0x7c00 mbr.o -o mbr.elf
$ls -al
...
-rwxr-xr-x 1 abc abc 3588 2月 15 19:50 mbr.elf
-rw-r--r-- 1 abc abc 656 2月 15 19:46 mbr.o
-rw-r--r-- 1 abc abc 594 2月 15 19:43 mbr.s
不管是i386还是i386之前的芯片,在加电后的第一条指令都是跳转到BIOS固件进行开机自检,然 后将磁盘的主引导扇区(Master Boot Record, MBR ;0号柱面,0号磁头,0号扇区对应的扇区, 512字节,末尾两字节为魔数 0x55 和 0xaa )加载到0x7c00。
$ objcopy -S -j .text -O binary mbr.elf mbr.bin
$ ls -al mbr.bin
-rwxr-xr-x 1 kingxu kingxu 65 2月 15 20:03 mbr.bin
#!/usr/bin/perl
open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
$n = sysread(SIG, $buf, 1000);
if($n > 510){
print STDERR "ERROR: boot block too large: $n bytes (max 510)\n";
exit 1;
}
print STDERR "OK: boot block is $n bytes (max 510)\n";
$buf .= "\0" x (510-$n);
$buf .= "\x55\xAA";
open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
print SIG $buf;
$./genboot.pl mbr.bin
OK: boot block is 65 bytes (max 510)
$ls -al mbr.bin
-rwxr-xr-x 1 kingxu kingxu 512 2月 15 20:11 mbr.bin
$qemu-system-i386 mbr.bin