Http://blog.csdn.net/b2b160/archive/2009/09/18/4567036.aspx
How to write the device driver under DOS (2)
This article was temporarily added when I wrote the DOS device driver, or it may be helpful to you.
In order to better understand the device driver program, in this article, I plan to introduce a program for you. This program can list the device drivers under DOS one by one, you can see clearly
To the entry address of each driver, device properties, the entry address of the next device, the entry address of strategy and interrupt, and the name of the device driver, you can
With a clear understanding of the one-way linked list of the device driver, if you want to, you can go deep into any driver to see its device header structure, view its strategy and
Interrupt code. In short, I hope you will have a better understanding of the DOS device driver before we actually write the driver.
We have said that the device driver in DOS uses a one-way linked list. The first device is NUL, so long as we find the NUL entry address, theoretically, we can find
All the device drivers, but how to find the NUL device driver? What dos can find indicates that it must have saved this address, and all the places where it is saved will be OK.
To get this address, we have to use a non-public dos call with 52 h function. Here we only need to provide a description of what we need. If the reader needs a complete description of this function, you can contact me.
Int 21 H function 52 h:
Entry: Ah = 52 h Exit: Es: BX pointing to important data List table
In the list returned by the DOS call, the device header of the NUL device is stored at a 22h offset, with a total of 18 bytes. Remember that the device header is not the address of the device header, the task can be completed from the device header.
This is the first time I have published the assembly language code in my blog. So I have to talk about my environment. First, this code is in the 16-bit real-world mode.
6.11 compiling and connecting is A. com file format. If it is difficult to see it, you can easily track it under Debug. Because the program is too simple, there is basically no comment. I do not know
There seems to be nothing to explain.
The following is a program list:
Code segment
Assume Cs: code, DS: code, ES: code, SS: Code
Org 100 h
Main proc
JMP begin
DDH struc
Ddh_nextoff DW? ; Next device driver after this
Ddh_nextseg DW?
Ddh_attribute DW? ; Attribute of Device
Ddh_strategy DW? ; Address of strategy routine
Ddh_interrupt DW? ; Address of Interrupt Routine
Ddh_devname dB 8 DUP (?) ; 8 bytes device driver name
DDH ends
Msg1 dB 0dh, 0ah, 0ah, 0ah, 'device driver entry: $'
Msg2 dB 0dh, 0ah, 0ah, 'next Device Driver: $'
Msg3 dB 0dh, 0ah, 'device attribute: $'
Msg4 dB 0dh, 0ah, 'device strategy offset: $'
Msg5 dB 0dh, 0ah, 'device interrupt offset: $'
Msg6 dB 0dh, 0ah, 'device driver name: $'
Msg7 dB 8 DUP (?)
Db' $'
Begin:
MoV ah, 52 h
Int 21 h
Add Bx, 22 h; + 34 bytes. pointer to DDH
Next_ddh:
MoV ax, ES: [BX]. ddh_nextoff
CMP ax, 0 ffffh
JZ finish
MoV dx, offset msg1
MoV ah, 09 h
Int 21 h
MoV ax, es
Call disphex
MoV DL ,':'
MoV ah, 02 h
Int 21 h
MoV ax, BX
Call disphex
MoV dx, offset msg2
MoV ah, 09 h
Int 21 h
MoV ax, ES: [BX]. ddh_nextseg
Call disphex
MoV DL ,':'
MoV ah, 02 h
Int 21 h
MoV ax, ES: [BX]. ddh_nextoff
Call disphex
MoV dx, offset msg3
MoV ah, 09 h
Int 21 h
MoV ax, ES: [BX]. ddh_attribute
Call disphex
MoV dx, offset msg4
MoV ah, 09 h
Int 21 h
MoV ax, ES: [BX]. ddh_strategy
Call disphex
MoV dx, offset msg5
MoV ah, 09 h
Int 21 h
MoV ax, ES: [BX]. ddh_interrupt
Call disphex
MoV dx, offset msg6
MoV ah, 09 h
Int 21 h
MoV Si, BX
Add Si, 10
MoV Di, offset msg7
MoV CX, 8
Push es
PUSH DS
Pop es
Pop DS
Rep movsb
Push es
PUSH DS
Pop es
Pop DS
MoV dx, offset msg7
MoV ah, 09 h
Int 21 h
MoV ax, ES: [BX]. ddh_nextseg
PUSH AX
MoV ax, ES: [BX]. ddh_nextoff
MoV BX, ax
Push BX
MoV ah, 00 h
Int 16 h
Pop BX
Pop es
JMP next_ddh
Finish:
RET
Main endp
;************************************
; * Display a hex digit
; * Input: AX = digit output: None
;************************************
Disphex proc
Push BX
Push es
MoV CX, 4
Xchg ah, Al
MoV BL, Al
SHR Al, Cl
Shl bl, Cl
Or Al, BL
MoV BL, ah
SHR ah, Cl
Shl bl, Cl
Or ah, BL
Again:
Push CX
PUSH AX
And Al, 0fh
CMP Al, 9
Ja disphex1
Add Al, 30 h
JMP disphex2
Disphex1:
Add Al, 37 h
Disphex2:
MoV DL, Al
MoV ah, 02 h
Int 21 h
Pop ax
MoV cl, 4
SHR ax, Cl
Pop CX
Loop again
MoV DL, 'H'
MoV ah, 02 h
Int 21 h
Pop es
Pop BX
RET
Disphex endp
Code ends
End main