home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advmsdos / chap14 / driver.asm next >
Encoding:
Assembly Source File  |  1988-10-01  |  7.9 KB  |  379 lines

  1.         title   DRIVER.ASM Device Driver Skeleton
  2.         page    55,132
  3.  
  4. ;
  5. ; DRIVER.ASM   MS-DOS device driver skeleton
  6. ;
  7. ; The driver command code routines are stubs only and have
  8. ; no effect but to return a non-error "Done" status.
  9. ; Copyright (C) 1988 Ray Duncan
  10. ;
  11. ; Build:   MASM DRIVER;
  12. ;          LINK DRIVER;
  13. ;          EXE2BIN DRIVER.EXE DRIVER.SYS
  14. ;          DEL DRIVER.EXE
  15. ; Usage:   DEVICE=DRIVER.SYS  (in file CONFIG.SYS)
  16. ;
  17.  
  18. _TEXT    segment word public 'CODE'
  19.  
  20.         assume  cs:_TEXT,ds:_TEXT,es:NOTHING
  21.  
  22.         org     0
  23.  
  24. MaxCmd    equ    24        ; maximum allowed command code
  25.                 ; 12 for MS-DOS 2.x
  26.                                 ; 16 for MS-DOS 3.0-3.1
  27.                                 ; 24 for MS-DOS 3.2-3.3
  28.  
  29. cr      equ     0dh             ; ASCII carriage return
  30. lf      equ     0ah             ; ASCII line feed
  31. eom     equ     '$'             ; end of message signal
  32.  
  33.  
  34. Header:                ; device driver header
  35.     dd      -1              ; link to next device driver
  36.         dw      0c840h        ; device attribute word
  37.         dw      Strat           ; "Strategy" routine entry point
  38.         dw      Intr            ; "Interrupt" routine entry point
  39.         db      'SKELETON'    ; logical device name
  40.  
  41.  
  42. RHPtr    dd     ?        ; pointer to request header, passed
  43.                 ; by MS-DOS kernel to Strategy routine
  44.  
  45.  
  46. Dispatch:            ; Interrupt routine command code
  47.                 ; dispatch table.
  48.         dw      Init        ; 0  = initialize driver
  49.         dw      MediaChk    ; 1  = media check
  50.         dw      BuildBPB    ; 2  = build BIOS parameter block
  51.         dw      IoctlRd        ; 3  = I/O control read 
  52.         dw      Read        ; 4  = read
  53.         dw      NdRead        ; 5  = non-destructive read 
  54.         dw      InpStat        ; 6  = input status
  55.         dw      InpFlush    ; 7  = flush input buffers
  56.         dw      Write        ; 8  = write
  57.         dw      WriteVfy    ; 9  = write with verify
  58.         dw      OutStat        ; 10 = output status
  59.         dw      OutFlush    ; 11 = flush output buffers
  60.         dw      IoctlWt        ; 12 = I/O control write 
  61.         dw      DevOpen        ; 13 = device open       (MS-DOS 3.0+)
  62.         dw      DevClose    ; 14 = device close      (MS-DOS 3.0+)
  63.         dw      RemMedia    ; 15 = removeable media  (MS-DOS 3.0+)
  64.     dw    OutBusy        ; 16 = output until busy (MS-DOS 3.0+)    
  65.     dw    Error        ; 17 = not used
  66.     dw    Error        ; 18 = not used
  67.     dw    GenIOCTL    ; 19 = generic IOCTL     (MS-DOS 3.2+) 
  68.     dw    Error        ; 20 = not used
  69.     dw     Error        ; 21 = not used
  70.     dw    Error        ; 22 = not used
  71.     dw    GetLogDev    ; 23 = get logical device (MS-DOS 3.2+)
  72.     dw    SetLogDev    ; 24 = set logical device (MS-DOS 3.2+)
  73.  
  74.     
  75. Strat   proc    far         ; device driver Strategy routine,
  76.                 ; called by MS-DOS kernel with
  77.                 ; ES:BX = address of request header
  78.  
  79.                                 ; save pointer to request header
  80.         mov     word ptr cs:[RHPtr],bx
  81.         mov     word ptr cs:[RHPtr+2],es
  82.  
  83.         ret                     ; back to MS-DOS kernel
  84.  
  85. Strat   endp
  86.  
  87.  
  88. Intr    proc  far        ; device driver Interrupt routine,
  89.                 ; called by MS-DOS kernel immediately 
  90.                 ; after call to Strategy routine
  91.  
  92.         push    ax              ; save general registers 
  93.         push    bx
  94.         push    cx
  95.         push    dx
  96.         push    ds
  97.         push    es
  98.         push    di
  99.         push    si
  100.         push    bp
  101.  
  102.         push    cs              ; make local data addressable
  103.         pop     ds        ; by setting DS = CS
  104.  
  105.         les     di,[RHPtr]    ; let ES:DI = request header
  106.  
  107.                                 ; get BX = command code
  108.         mov     bl,es:[di+2]
  109.         xor     bh,bh
  110.         cmp     bx,MaxCmd    ; make sure it's legal
  111.     jle    Intr1        ; jump, function code is ok
  112.     call    Error        ; set error bit, "Unknown Command" code
  113.     jmp    Intr2
  114.  
  115. Intr1:    shl     bx,1            ; form index to dispatch table
  116.                                 ; and branch to command code routine
  117.         call    word ptr [bx+Dispatch]
  118.  
  119.     les    di,[RHPtr]    ; ES:DI = addr. of request header
  120.  
  121. Intr2:    or    ax,0100h    ; merge 'Done' bit into status, and
  122.         mov     es:[di+3],ax    ; store status into request header 
  123.  
  124.         pop     bp              ; restore general registers
  125.         pop     si
  126.         pop     di
  127.         pop     es
  128.         pop     ds
  129.         pop     dx
  130.         pop     cx
  131.         pop     bx
  132.         pop     ax
  133.         ret                     ; back to MS-DOS kernel
  134.  
  135.  
  136. ; Command code routines are called by the Interrupt routine
  137. ; via the Dispatch table with ES:DI pointing to the request
  138. ; header.  Each routine should return AX=0 if function was
  139. ; completed successfully, or AX = 8000h + error code if
  140. ; function failed.
  141.  
  142.  
  143. MediaChk proc    near        ; function 1 = Media Check
  144.     
  145.     xor    ax,ax
  146.     ret
  147.  
  148. MediaChk endp
  149.  
  150.  
  151. BuildBPB proc     near        ; function 2 = Build BPB
  152.  
  153.     xor    ax,ax
  154.     ret
  155.  
  156. BuildBPB endp
  157.  
  158.  
  159. IoctlRd    proc    near        ; function 3 = I/O Control Read
  160.  
  161.     xor    ax,ax
  162.     ret
  163.  
  164. IoctlRd    endp
  165.  
  166.  
  167. Read    proc    near         ; function 4 = Read (Input)
  168.  
  169.     xor    ax,ax
  170.     ret
  171.  
  172. Read    endp
  173.  
  174.  
  175. NdRead    proc    near        ; function 5 = Non-Destructive Read
  176.  
  177.     xor    ax,ax
  178.     ret
  179.  
  180. NdRead    endp
  181.  
  182.  
  183. InpStat    proc    near        ; function 6 = Input Status
  184.  
  185.     xor    ax,ax
  186.     ret
  187.  
  188. InpStat    endp
  189.  
  190.  
  191. InpFlush proc    near        ; function 7 = Flush Input Buffers
  192.  
  193.     xor    ax,ax
  194.     ret
  195.  
  196. InpFlush endp
  197.  
  198.  
  199. Write    proc    near        ; function 8 = Write (Output)
  200.  
  201.     xor    ax,ax
  202.     ret
  203.  
  204. Write    endp
  205.  
  206.  
  207. WriteVfy proc    near        ; function 9 = Write with Verify
  208.  
  209.     xor    ax,ax
  210.     ret
  211.  
  212. WriteVfy endp
  213.  
  214.  
  215. OutStat    proc    near        ; function 10 = Output Status
  216.  
  217.     xor    ax,ax
  218.     ret
  219.  
  220. OutStat    endp
  221.  
  222.  
  223. OutFlush proc    near        ; function 11 = Flush Output Buffers
  224.  
  225.     xor    ax,ax
  226.     ret
  227.  
  228. OutFlush endp
  229.  
  230.  
  231. IoctlWt proc    near        ; function 12 = I/O Control Write
  232.  
  233.     xor    ax,ax
  234.     ret
  235.  
  236. IoctlWt endp
  237.  
  238.  
  239. DevOpen proc    near        ; function 13 = Device Open
  240.  
  241.     xor    ax,ax
  242.     ret
  243.  
  244. DevOpen endp
  245.     
  246.  
  247. DevClose proc    near        ; function 14 = Device Close
  248.  
  249.     xor    ax,ax
  250.     ret
  251.  
  252. DevClose endp
  253.  
  254.  
  255. RemMedia proc    near        ; function 15 = Removable Media
  256.  
  257.     xor    ax,ax
  258.     ret
  259.  
  260. RemMedia endp
  261.  
  262.  
  263. OutBusy proc    near        ; function 16 = Output Until Busy
  264.  
  265.     xor    ax,ax
  266.     ret
  267.  
  268. OutBusy endp         
  269.  
  270.  
  271. GenIOCTL proc    near        ; function 19 = Generic IOCTL
  272.  
  273.     xor    ax,ax
  274.     ret
  275.  
  276. GenIOCTL endp
  277.  
  278.  
  279. GetLogDev proc    near        ; function 23 = Get Logical Device
  280.  
  281.     xor    ax,ax
  282.     ret
  283.  
  284. GetLogDev endp
  285.  
  286.  
  287. SetLogDev proc    near        ; function 24 = Set Logical Device
  288.  
  289.     xor    ax,ax
  290.     ret
  291.  
  292. SetLogDev endp
  293.  
  294.  
  295. Error    proc    near        ; bad command code in request header
  296.  
  297.     mov    ax,8003h    ; error bit + "Unknown Command" code
  298.     ret
  299.  
  300. Error    endp
  301.  
  302.  
  303. Init    proc    near        ; function 0 = initialize driver
  304.  
  305.         push    es              ; save address of request header 
  306.         push    di
  307.  
  308.     mov    ax,cs        ; convert load address to ASCII
  309.     mov    bx,offset Ident1
  310.     call    hexasc
  311.  
  312.     mov    ah,9        ; display driver sign-on message
  313.         mov     dx,offset Ident
  314.         int     21h
  315.  
  316.         pop     di              ; restore request header address
  317.         pop     es
  318.  
  319.                                 ; set addr. of free memory 
  320.                 ; above driver (break address)
  321.         mov     word ptr es:[di+14],offset Init
  322.         mov     word ptr es:[di+16],cs
  323.  
  324.     xor    ax,ax        ; return status 
  325.     ret
  326.  
  327. Init    endp
  328.  
  329.  
  330. hexasc    proc    near        ; converts word to hex ASCII
  331.                 ; call with AX = value,
  332.                 ; DS:BX = address for string
  333.                 ; returns AX, BX destroyed
  334.  
  335.     push    cx        ; save registers 
  336.     push    dx
  337.  
  338.     mov    dx,4        ; initialize character counter
  339.  
  340. hexasc1:
  341.     mov    cx,4        ; isolate next four bits
  342.     rol    ax,cl
  343.     mov    cx,ax
  344.     and    cx,0fh
  345.     add    cx,'0'        ; convert to ASCII 
  346.     cmp    cx,'9'        ; is it 0-9?
  347.     jbe    hexasc2        ; yes, jump
  348.     add    cx,'A'-'9'-1    ; add fudge factor for A-F
  349.  
  350. hexasc2:            ; store this character
  351.     mov    [bx],cl
  352.     inc    bx        ; bump string pointer
  353.  
  354.     dec    dx        ; count characters converted
  355.     jnz    hexasc1     ; loop, not four yet
  356.  
  357.     pop    dx        ; restore registers
  358.     pop    cx
  359.     ret            ; back to caller
  360.  
  361. hexasc    endp
  362.  
  363.  
  364. Ident   db      cr,lf,lf
  365.         db      'Advanced MS-DOS Example Device Driver'
  366.     db    cr,lf
  367.     db    'Device driver header at: '
  368. Ident1    db    'XXXX:0000'
  369.         db      cr,lf,lf,eom
  370.  
  371. Intr    endp
  372.  
  373. _TEXT    ends
  374.  
  375.     end
  376.  
  377.