MS-DOS Device Driver >> Assembly >> Advanced >> Block
Hello, world!
[Github] https://github.com/steward-fu/gh_driver.git
作為第一個Block驅動程式的範例,司徒一樣使用傳統的Hello, world!作為教學,而程式碼其實跟Char驅動程式幾乎是一樣,差別僅在於檔頭的宣告不一樣。
編輯程式:
新增c:\main.asm
;//=========================================================================== ;// Copyright (c) 2016 by Steward Fu ;// All rights reserved ;//=========================================================================== cseg segment para public 'code' BlkHello proc far assume cs:cseg, es:cseg, ds:cseg Header: NextDriver dd -1 Attribute dw 0000h Strategy dw MyStrategy Interrupt dw MyInterrupt DriverName db 'BlkHello' RhOffset dw ? RhSegment dw ? bootmsg db 'Steward MS-DOS Driver Tutorial, Hello, world!', 0dh, 0ah, '$' CmdTable dw Init MyStrategy: mov cs:RhSegment, es mov cs:RhOffset, bx ret MyInterrupt: cld push ds push es push ax push bx push cx push dx push di push si mov al, es:[bx]+2 cmp al, 0 jnz SkipCurRequest rol al, 1 lea di, CmdTable mov ah, 0 add di, ax jmp word ptr[di] Init: lea dx, bootmsg mov ah, 9 int 21h lea ax, Exit mov es:[bx]+0eh, ax push cs pop ax mov es:[bx]+10h, ax mov es:word ptr 3[bx], 0100h jmp CompleteCmd SkipCurRequest: mov es:word ptr 3[bx], 8103h jmp CompleteCmd CompleteCmd: mov bx, cs:RhOffset mov es, cs:RhSegment pop si pop di pop dx pop cx pop bx pop ax pop es pop ds ret Exit: BlkHello endp cseg ends end
程式就跟Char驅動程式一樣,收到Command 0時,印出輸出訊息
接著使用如下指令將main.asm放到DOS Image裡面:
$ sudo fdisk -lu msdos.img Disk msdos.img: 41 MB, 41803776 bytes 16 heads, 63 sectors/track, 81 cylinders, total 81648 sectors Units = sectors of 1 * 512 = 512 bytes Device Boot Start End Blocks Id System msdos.img1 * 63 81647 40792+ 6 FAT16 $ sudo losetup -o $((63*512)) /dev/loop0 $ sudo mount /dev/loop0 /mnt $ sudo cp main.asm /mnt $ sudo umount /mnt $ sudo losetup -d /dev/loop0
編譯程式:
接著使用Bochs進入DOS系統,將編譯的路徑加到c:\config.sys中
接著使用如下指令編譯
c:\> masm main.asm c:\> link main.obj c:\> exe2bin main.exe
安裝程式:
將device=c:\main.bin加到c:\config.sys的最後一行
接著將pause加到autoexec.bat的最後一行,避免開機訊息過多而看不到驅動程式的文字
最後使用shutdown指令重開機即可看到訊息
使用mem /d | more指令查看剛剛載入的驅動程式,發現並沒有BlkHello驅動程式的名稱,這是因為掛載Block驅動程式等同於掛載DISK一樣,當系統無法正確取得該裝置的資訊時,便會無法掛載成功。