MS-DOS Device Driver >> Assembly >> Advanced >> Char
Hello, world!
[Github] https://github.com/steward-fu/gh_driver.git
第一個練習的Char驅動程式,當然還是使用經典的Hello, world!來當作範例,這個驅動程式僅在載入時,顯示一個字串,其餘命令都不處理,相當簡單。
編輯程式:
新增c:\main.asm
;//=========================================================================== ;// Copyright (c) 2016 by Steward Fu ;// All rights reserved ;//=========================================================================== cseg segment para public 'code' ChrHello proc far assume cs:cseg, es:cseg, ds:cseg Header: NextDriver dd -1 Attribute dw 8000h Strategy dw MyStrategy Interrupt dw MyInterrupt DriverName db 'ChrHello' 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: ChrHello endp cseg ends end
這個Char驅動程式很簡單,在收到Command 0命令時,呼叫DOS INT 21H(顯示字串),它的功能應該不需要司徒詳細說明,記得練習寫一下,熟悉一下如何寫出一個Char驅動程式
接著使用如下指令將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指令查看驅動程式的名稱