AT&T汇编改写linux-0.00-rh9的boot.s
# boot.s
#
# It then loads the system at 0x10000, using BIOS interrupts. Thereafter
# it disables all interrupts, changes to protected mode, and calls the
BOOTSEG = 0x07c0
SYSSEG = 0x1000 # system loaded at 0x10000 (65536).
SYSLEN = 17 # sectors occupied.
.code16 #编译16进制,需要使用添加.code16
.text
.global start
start:
ljmp $BOOTSEG,$go
go: movw %cs,%ax
movw %ax,%ds
movw %ax,%ss
movw $0x400,%sp # arbitrary value >>512
# ok, we've written the message, now
load_system:
movw $0x0000,%dx
movw $0x0002,%cx
movw $SYSSEG,%ax
movw %ax,%es
xorw %bx,%bx
movw $0x200+SYSLEN,%ax
int $0x13
jnc ok_load
die: jmp die
# now we want to move to protected mode ...
ok_load:
cli # no interrupts allowed
movw $SYSSEG,%ax
movw %ax,%ds
xor %ax, %ax
movw %ax,%es
movw $0x1000,%cx
xor %si,%si
xor %di,%di
cld
rep movsw
movw $BOOTSEG,%ax
movw %ax,%ds
lidt idt_48 # load idt with 0,0
lgdt gdt_48 # load gdt with whatever appropriate
# absolute address 0x00000, in 32-bit protected mode.
movw $0x0001,%ax
lmsw %ax # This is it!
ljmp $8,$0 # jmp offset 0 of segment 8 (cs)
gdt: .word 0,0,0,0 # dummy
.word 0x07FF # 8Mb - limit=2047 (2048*4096=8Mb)
.word 0x0000 # base address=0x00000
.word 0x9A00 # code read/exec
.word 0x00C0 # granularity=4096, 386
.word 0x07FF # 8Mb - limit=2047 (2048*4096=8Mb)
.word 0x0000 # base address=0x00000
.word 0x9200 # data read/write
.word 0x00C0 # granularity=4096, 386
idt_48: .word 0 # idt limit=0
.word 0,0 # idt base=0L
gdt_48: .word 0x7ff # gdt limit=2048, 256 GDT entries
.word 0x7c00+gdt,0 # gdt base = 07xxx
.org 510
.word 0xAA55
# Makefile for the simple example kernel.
AS =as --32
LD =ld -m32
LDFLAGS =-m elf_i386 -Ttext 0 -e startup_32 -s -x -M
all: Image
Image: boot system
cp boot Image
objcopy -O binary system head
cat head >> Image
head.o: head.s
system: head.o
$(LD) $(LDFLAGS) head.o -o system > System.map
boot.o: boot.s
boot: boot.o
objcopy -O binary boot.o boot
clean:
rm -f Image System.map core boot head *.o system
注意点:
1、编译16进制,需要添加 .code16
2、使用ljmp 替代jmpi 长跳转
3、使用rep movsw
4、由于编译器是64位,使用as --32 和ld -m32