用bios中断在屏幕上显示字符串

汇编代码,用的是nasm汇编器,是Intel的语法(还有gun as汇编器,用的是AT&T的语法,两者区别)。

写一小段汇编(正好是512字节,模拟操作系统的引导程序),机器启动后放在内存0x7c00处,开始执行,执行过程是调用bios中断,在屏幕上显示一串字符,然后在汇编最后死循环。

汇编源码boot.asm

org 0x7c00

mov ax, cs
mov ds, ax
mov es, ax

call dispstr
jmp $

dispstr:
    mov ax, 0x1301
    mov bx, 0x000c
    mov cx, 10
    mov dx, 0x1010
    mov bp, BootMessage
    int 0x10
    ret

BootMessage: db "hello os!", 0

times 510-($-$$) db 0
dw 0xaa55

汇编命令:

nasm boot.asm -o boot.bin

生成软盘映像(bochs中的工具):

bximage //bochs的工具,用于生成软盘映像,选fd(floppy disk)

用dd命令,将二进制代码写入软盘映像中(dd是Linux的命令,我用的mac也有这个命令):

dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc

if: input file
of: output file
conv=notrunc 因为boot.bin比a.img小,用这个参数说明不要截断

bochs的配置文件bochsrc:

megs: 32
floppya: 1_44="a.img", status=inserted
boot: floppy

启动bochs:

bochs -f bochsrc

bochs是可以调试的,启动后并没有直接运行,而是等待输入,可以设置断点,一步步执行:

b 0x7c00 // 设断点
c // continue
dump_cpu
x /64xb 0x7c00
trace-reg on
n

运行结果:
这里写图片描述

bochsrc调试命令:
这里写图片描述

btw,还写了一个Makefile来方便操作:

.PHONY: clean
clean:
    @-rm -f a.img boot.bin

make clean

参考:于渊 《Orange’S 一个操作系统的实现》
《跟我一起写Makefile》
《BIOS中断大全》
王爽《汇编语言》

github上的源码

参考资料:
链接:https://pan.baidu.com/s/1yf18dTK106YpYgYsDQCvAg 密码:typ5


下面是更好的写法:

;bios中断
;int 0x10 显示服务
;ah=0x13 在Teletype模式下显示字符串

BOOTSEG equ 0x7c0

section .text vstart=0

jmp BOOTSEG:start

start:
    mov ax, cs
    mov ss, ax
    mov ds, ax
    mov es, ax    

    mov bp, msg    ;入口参数:
        mov ax, 0x1301 ; ah=0x13 al=显示输出方式 01表示字符串只含有显示字符,其属性在BL中。显示后光标位置改变
        mov bx, 0x0002 ; bh页码 bl属性(若al=0或1)
    mov cx, 3      ; cx显示的字符串长度
        mov dx, 0x0101 ; dh行 dl列
        int 0x10       ; es:bp显示字符串的地址
    jmp $

msg: db "ok", 0

times 510-($-$$) db 0
dw 0xaa55

猜你喜欢

转载自blog.csdn.net/familyshizhouna/article/details/81004577