以前看了《一個操作系統(tǒng)的實現(xiàn)》這本書猪钮,使用了nasm匯編和bochs虛擬機來編寫一個“操作系統(tǒng)”鼠冕。做了清華大學ucore實驗后吨拍,我感覺使用at&t匯編和qemu虛擬機來實現(xiàn)一個“操作系統(tǒng)”更加容易調(diào)試操作系統(tǒng)席吴,因此改寫了成at&t版本夭织。
at&t匯編版本代碼如下:
.code16
.section .text
.global _start
_start:
movw %cs, %ax
movw %ax, %ds
movw %ax, %ss
call DispStr
loop1:
jmp loop1
DispStr:
movw $msg, %ax
movw %ax, %bp
movw $16, %cx
movw $0x1301, %ax
movw $0x000c, %bx
movb $0x00, %dl
int $0x10
ret
msg:
.ascii "Hello, OS world!"
.org 510
.word 0xAA55
接下來使用as匯編吭露、ld鏈接:
as -o boot.o boot.s
ld -Ttext=0x7c00 --oformat binary -o boot.bin boot.o
接下來制作一個512KB的虛擬硬盤,將上面生成的“操作系統(tǒng)”寫入第一個扇區(qū)尊惰。
dd if=/dev/zero of=boot.img count=1000
dd if=boot.bin of=boot.img conv=notrunc
接著啟動qemu虛擬機讲竿,如下,就是模擬插入boot.img硬盤弄屡,不需要配置文件题禀,比bochs更方便。
qemu-system-x86_64 -hda boot.img -monitor stdio
成功膀捷,如下圖:
qemu不用配置文件真的很方便迈嘹,加上啟動參數(shù)-S -s就能使用gdb單步調(diào)試了。