home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch10 / dasd.asm next >
Encoding:
Assembly Source File  |  1988-12-12  |  10.8 KB  |  273 lines

  1.         title   DASD --- direct disk access demo
  2.         page    55,132
  3.         .286
  4.  
  5. ; DASD.EXE  --- Demonstrates direct disk access under OS/2.
  6. ;               Reads the boot sector for the current drive
  7. ;               into a buffer, then writes it into a file
  8. ;               named \BOOT.BIN on the same drive.
  9. ;               The lock operation will fail if any files
  10. ;               are open on the current drive (you can't
  11. ;               use it on the boot drive, for example).
  12. ; Copyright (C) Ray Duncan October 1988
  13. ;
  14. ; Build:        MASM DASD;
  15. ;               LINK DASD,,,OS2,DASD;
  16. ;
  17. ; Usage:        DASD
  18.  
  19. stdin   equ     0                       ; standard input handle
  20. stdout  equ     1                       ; standard output handle
  21. stderr  equ     2                       ; standard error handle
  22.  
  23. cr      equ     0dh                     ; ASCII carriage return
  24. lf      equ     0ah                     ; ASCII line feed
  25.  
  26. secsize equ     512                     ; physical sector size
  27.  
  28.         extrn   DosChgFilePtr:far       ; references to OS/2 API
  29.         extrn   DosClose:far
  30.         extrn   DosDevIOCtl:far
  31.         extrn   DosExit:far
  32.         extrn   DosOpen:far
  33.         extrn   DosQCurDisk:far
  34.         extrn   DosRead:far
  35.         extrn   DosWrite:far
  36.  
  37. DGROUP  group   _DATA
  38.  
  39. _DATA   segment word public 'DATA'
  40.  
  41. dname   db      'X:',0                  ; logical drive identifier
  42. dhandle dw      ?                       ; receives drive handle
  43. daction dw      ?                       ; receives DosOpen action
  44.  
  45. drive   dw      0                       ; receives drive ID
  46. drvmap  dd      0                       ; receives drive bitmap
  47.  
  48. fname   db      '\BOOT.BIN',0           ; file to receive boot sector
  49. rlen    dw      ?                       ; actual bytes read
  50. wlen    dw      ?                       ; actual bytes written
  51.  
  52. bootsec db      secsize dup (?)         ; boot sector read here
  53.  
  54. parblk  db      0                       ; DosDevIOCtl dummy parameter
  55.  
  56. msg1    db      cr,lf
  57.         db      'DosQCurDisk failed.'
  58.         db      cr,lf
  59. msg1_len equ $-msg1
  60.  
  61. msg2    db      cr,lf
  62.         db      'Can''t open drive '
  63. msg2a   db      'X:'
  64.         db      cr,lf
  65. msg2_len equ $-msg2
  66.  
  67. msg3    db      cr,lf
  68.         db      'Can''t lock drive '
  69. msg3a   db      'X:'
  70.         db      cr,lf
  71. msg3_len equ $-msg3
  72.  
  73. msg4    db      cr,lf
  74.         db      'Read of boot sector failed.'
  75.         db      cr,lf
  76. msg4_len equ $-msg4
  77.  
  78. msg5    db      cr,lf
  79.         db      'Can''t create file BOOT.BIN'
  80.         db      cr,lf
  81. msg5_len equ $-msg5
  82.  
  83. msg6    db      cr,lf
  84.         db      'Can''t write file BOOT.BIN'
  85.         db      cr,lf
  86. msg6_len equ $-msg6
  87.  
  88. msg7    db      cr,lf
  89.         db      'Boot sector from drive '
  90. msg7a   db      'X: written into file '
  91. msg7b   db      'X:BOOT.BIN.'
  92.         db      cr,lf
  93. msg7_len equ $-msg7
  94.  
  95. _DATA   ends
  96.  
  97.  
  98. _TEXT   segment word public 'CODE'
  99.  
  100.         assume  cs:_TEXT,ds:DGROUP
  101.  
  102. main    proc    far
  103.  
  104.                                         ; get current drive...
  105.         push    ds                      ; receives drive code
  106.         push    offset DGROUP:drive
  107.         push    ds                      ; receives bitmap for
  108.         push    offset DGROUP:drvmap    ; available drives
  109.         call    DosQCurDisk             ; transfer to OS/2
  110.         or      ax,ax                   ; did function succeed?
  111.         jz      main1                   ; yes, proceed
  112.  
  113.         mov     dx,offset DGROUP:msg1   ; no, display error message
  114.         mov     cx,msg1_len             ; and exit
  115.         jmp     error
  116.  
  117. main1:  mov     ax,drive                ; convert drive code to
  118.         add     al,'A'-1                ; ASCII letter and store
  119.         mov     dname,al                ; into drive name string
  120.         mov     msg2a,al                ; and error messages    
  121.         mov     msg3a,al
  122.         mov     msg7a,al
  123.         mov     msg7b,al
  124.                                         ; open current drive for 
  125.                                         ; direct access ...
  126.         push    ds                      ; address of drive name
  127.         push    offset DGROUP:dname
  128.         push    ds                      ; receives drive handle
  129.         push    offset DGROUP:dhandle
  130.         push    ds                      ; receives DosOpen action
  131.         push    offset DGROUP:daction
  132.         push    0                       ; file allocation (N/A)
  133.         push    0
  134.         push    0                       ; file attribute (N/A)
  135.         push    01h                     ; action: open if exists,
  136.                                         ;         fail if doesn't
  137.         push    8012h                   ; mode: DASD, read/write, 
  138.                                         ;       deny all
  139.         push    0                       ; reserved DWORD 0
  140.         push    0
  141.         call    DosOpen                 ; transfer to OS/2
  142.         or      ax,ax                   ; was open successful?
  143.         jz      main2                   ; yes, proceed
  144.  
  145.         mov     dx,offset DGROUP:msg2   ; no, display error message
  146.         mov     cx,msg2_len             ; and exit
  147.         jmp     error
  148.  
  149. main2:                                  ; lock logical drive...
  150.         push    0                       ; data buffer address
  151.         push    0                       ; (not needed)
  152.         push    ds                      ; parameter buffer address
  153.         push    offset DGROUP:parblk
  154.         push    0                       ; function
  155.         push    8                       ; category
  156.         push    dhandle                 ; drive handle
  157.         call    DosDevIOCtl             ; transfer to OS/2
  158.         or      ax,ax                   ; was lock successful?
  159.         jz      main3                   ; yes, proceed
  160.  
  161.         mov     dx,offset DGROUP:msg3   ; no, display error message
  162.         mov     cx,msg3_len             ; and exit
  163.         jmp     error
  164.  
  165. main3:                                  ; now read boot sector...
  166.         push    dhandle                 ; drive handle
  167.         push    ds                      ; buffer address
  168.         push    offset DGROUP:bootsec
  169.         push    secsize                 ; buffer length
  170.         push    ds                      ; receives actual length
  171.         push    offset DGROUP:rlen
  172.         call    DosRead                 ; transfer to OS/2
  173.         or      ax,ax                   ; read successful?
  174.         jnz     main4                   ; no, read failed
  175.  
  176.         cmp     rlen,secsize            ; actual = expected size?
  177.         je      main5                   ; yes, proceed
  178.  
  179. main4:                                  ; read failed... 
  180.         mov     dx,offset DGROUP:msg4   ; display error message
  181.         mov     cx,msg4_len             ; and exit
  182.         jmp     error
  183.  
  184. main5:                                  ; unlock logical drive...
  185.         push    0                       ; data buffer address
  186.         push    0                       ; (not needed)
  187.         push    ds                      ; parameter buffer address
  188.         push    offset DGROUP:parblk
  189.         push    1                       ; function
  190.         push    8                       ; category
  191.         push    dhandle                 ; drive handle
  192.         call    DosDevIOCtl             ; transfer to OS/2
  193.  
  194.                                         ; close logical drive...
  195.         push    dhandle                 ; drive handle
  196.         call    DosClose                ; transfer to OS/2
  197.  
  198.                                         ; now open file BOOT.BIN...
  199.         push    ds                      ; address of filename
  200.         push    offset DGROUP:fname
  201.         push    ds                      ; receives file handle
  202.         push    offset DGROUP:dhandle
  203.         push    ds                      ; receives DosOpen action
  204.         push    offset DGROUP:daction
  205.         push    0                       ; file allocation (N/A)
  206.         push    0
  207.         push    0                       ; file attribute (N/A)
  208.         push    12h                     ; action: create or replace
  209.         push    11h                     ; mode: write-only, deny all
  210.         push    0                       ; reserved DWORD 0
  211.         push    0
  212.         call    DosOpen                 ; transfer to OS/2
  213.         or      ax,ax                   ; was open successful?
  214.         jz      main6                   ; yes, proceed
  215.  
  216.         mov     dx,offset DGROUP:msg5   ; display error message
  217.         mov     cx,msg5_len             ; and exit
  218.         jmp     error
  219.  
  220. main6:                                  ; write sector into BOOT.BIN
  221.         push    dhandle                 ; file handle
  222.         push    ds                      ; address of data
  223.         push    offset DGROUP:bootsec
  224.         push    secsize                 ; length of data
  225.         push    ds                      ; receives bytes written
  226.         push    offset DGROUP:wlen
  227.         call    DosWrite                ; transfer to OS/2
  228.         or      ax,ax                   ; did write succeed?
  229.         jnz     main7                   ; no, display error msg.
  230.         cmp     wlen,secsize            ; was write complete?
  231.         je      main8                   ; yes, jump
  232.  
  233. main7:                                  ; write failed...
  234.         mov     dx,offset DGROUP:msg6   ; display error message
  235.         mov     cx,msg6_len             ; and exit
  236.         jmp     error
  237.  
  238. main8:  push    dhandle                 ; close the file
  239.         call    DosClose                ; transfer to OS/2
  240.  
  241.                                         ; display success message...
  242.         push    stdout                  ; standard output handle
  243.         push    ds                      ; message address
  244.         push    offset DGROUP:msg7                      
  245.         push    msg7_len                ; message length
  246.         push    ds                      ; receives bytes written        
  247.         push    offset DGROUP:wlen
  248.         call    DosWrite                ; transfer to OS/2      
  249.  
  250.         push    1                       ; terminate all threads
  251.         push    0                       ; return success code
  252.         call    DosExit                 ; exit program
  253.  
  254. error:                                  ; display error message...
  255.         push    stderr                  ; standard error handle
  256.         push    ds                      ; message address
  257.         push    dx                      
  258.         push    cx                      ; message length
  259.         push    ds                      ; receives bytes written        
  260.         push    offset DGROUP:wlen
  261.         call    DosWrite                ; transfer to OS/2      
  262.  
  263.         push    1                       ; terminate all threads
  264.         push    1                       ; return error code
  265.         call    DosExit                 ; exit program
  266.  
  267. main    endp
  268.  
  269. _TEXT   ends
  270.  
  271.  
  272.         end     main
  273.