操作系统:使用AT&T实现引导扇区

参考学习于渊的书箱时,里面都是用nasm来写的,而自己更熟悉和使用AT&T的语法,心想用AT&T来实现一下,这个过程是十分漫长与痛苦的,但也收获颇丰。


1. 引导扇区代码

.code16
.section .text
.globl _start
_start:
    movw %cs, %ax
    movw %ax, %ds
    movw %ax, %es
    call DispStr
loop1:
    jmp loop1

DispStr:
    movw $BootMessage, %ax
    movw %ax, %bp
    movw $0x10, %cx
    movw $0x1301, %ax
    movw $0xc, %bx
    movb $0, %dl
    int $0x10
    ret
BootMessage:
    .ascii "Hello, MN world!"
    .org 510
    .word 0xAA55


如果理解有些难,请先参考我的另一篇文章《操作系统:实现引导扇区》 http://blog.csdn.net/furzoom/article/details/52485090

首先.code16含义为16位运行模式,参考http://web.mit.edu/gnu/doc/html/as_18.html#SEC209

While GAS normally writes only "pure" 32-bit i386 code, it has limited support for writing code to run in real mode or in 16-bit protected mode code segments. To do this, insert a `.code16' directive before the assembly language instructions to be run in 16-bit mode. You can switch GAS back to writing normal 32-bit code with the `.code32' directive.

2. 编译

as -o boot.o boot.s 
ld -Ttext=0x7c00 --oformat binary -o boot.bin boot.o
dd if=/dev/zero of=emptydisk.img bs=512 count=2880
dd if=boot.bin of=Finix.img bs=512 count=1
dd if=emptydisk.img of=Finix.img skip=1 seek=1 bs=512 count=2879


3. 运行

VirtualBox运行结果:

Bochs运行结果:


4. 参考

于渊《自己动手写操作系统》

关于.code16指令:http://web.mit.edu/gnu/doc/html/as_18.html#SEC209


原文地址:https://www.cnblogs.com/furzoom/p/7710230.html