home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / ddjmag / ddj8606.arc / D16BIT.JUN < prev    next >
Text File  |  1986-06-30  |  8KB  |  235 lines

  1.         Listing One
  2.  
  3.  
  4.         mov     dx,seg fname    ; open a file for I/O
  5.         mov     ds,dx           ; DS:DX = ASCIIZ filespec
  6.         mov     dx,offset fname ; function 3DH = open,
  7.         mov     ax,3d02h        ; use mode 2 (read/write)
  8.         int     21h
  9.         jc      error           ; jump if open failed
  10.         mov     my_file,ax      ; save handle for file
  11.                         
  12.         .
  13.         .                       ; further file 
  14.         .                       ; processing here
  15.         .
  16.                 
  17.                                 ; now use DUP and CLOSE
  18.                                 ; to update the directory...
  19.         mov     bx,my_file      ; get handle for file
  20.         mov     ah,45h          ; function 45H = DUP handle,
  21.         int     21h
  22.         jc      error           ; jump if DUP failed
  23.         mov     bx,ax           ; now close the DUP'd handle
  24.         mov     ah,3eh          ; function 3EH = close file
  25.         int     21h             ; transfer to MS-DOS
  26.         jc      error           ; jump if close failed
  27.                                 ; otherwise directory is 
  28.                                 ; updated, continue processing 
  29.         .
  30.         .
  31. error:  .
  32.         .
  33.         .
  34.  
  35. my_file dw      0               ; handle from previous "open"
  36.  
  37. fname   db      'MYFILE.DAT',0  ; ASCIIZ filespec
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. Listing Two
  49.  
  50. page 60, 120
  51. title Redirected I/O example
  52.  
  53. ;
  54. ; REDIRECT.ASM --- An illustration of 
  55. ; I/O redirection under MS-DOS 2.x or 3.x
  56. ; Copyright (C) 1985 by Jerry Jankura
  57. ; Created:   6 November 1985
  58. ; Modified:  9 November 1985
  59. ;
  60. ; Abstract: This routine demonstrates redirection of I/O
  61. ; from the console to a line printer.  The method may be
  62. ; used to redirect I/O from any device to any other device.
  63. ; Requires Microsoft MS-DOS 2.X or 3.X, or DRI Concurrent 
  64. ; DOS version 4.1.
  65. ;
  66.  
  67. STD_IN          EQU     0       ; Standard input handle
  68. STD_OUT         EQU     1       ; Standard output handle
  69. STD_ERR         EQU     2       ; Standare error handle
  70. STD_AUX         EQU     3       ; Standard Auxiliary handle
  71. STD_LST         EQU     4       ; Standard printer handle
  72.  
  73. C_WRITESTR      EQU     9       ; Write string to STD_OUT
  74.  
  75. F_DUP           EQU     45H     ; Duplicate handle
  76. F_CDUP          EQU     46H     ; Force duplicate handle
  77. F_CLOSE         EQU     3EH     ; Close file handle
  78. F_WRITE         EQU     40H     ; Write to file or dev
  79.  
  80. P_TERM          EQU     4CH     ; Terminate a program
  81.  
  82. MS_DOS          EQU     21H     ; MS-DOS service request
  83.  
  84. CR              EQU     0DH     ; Carriage return
  85. LF              EQU     0AH     ; Line feed
  86.  
  87. datasg          segment para    'data'
  88.  
  89.  
  90. msg1            db      CR, LF, 'Redirected I/O example....'
  91.                 db      CR, LF
  92.                 db      CR, LF, 'This example was written using the'
  93.                 db      CR, LF, 'File I/O system services, with the'
  94.                 db      CR, LF, 'file handle being set to STD_OUT.'
  95.                 db      CR, LF, 'STD_OUT normally defaults to the'
  96.                 db      CR, LF, 'video screen, so you are reading'
  97.                 db      CR, LF, 'this message on the screen.'
  98.                 db      CR, LF
  99.  
  100. msg2            db      CR, LF, 'However, we may direct STD_OUT to'
  101.                 db      CR, LF, 'another device, such as the printer.'
  102.                 db      CR, LF, 'This message is still written to'
  103.                 db      CR, LF, 'STD-OUT, but is appears at the printer.'
  104.                 db      CR, LF, 'Again, the operating system provides'
  105.                 db      CR, LF, 'the facility to allow one file to mimic'
  106.                 db      CR, LF, 'and track another.  The Command processor'
  107.                 db      CR, LF, 'normally implements this redirection'
  108.                 db      CR, LF, 'of standard devices.'
  109.                 db      CR, LF
  110.  
  111. msg3            db      CR, LF, 'This message is written on the'
  112.                 db      CR, LF, 'Video screen, demonstrating that'
  113.                 db      CR, LF, 'a message may be redirected to the'
  114.                 db      CR, LF, 'normal STD_OUT device in the same'
  115.                 db      CR, LF, 'manner that was used to redirect'
  116.                 db      CR, LF, 'it to the printer.'
  117.                 db      CR, LF
  118.                 db      CR, LF, 'Note also that the initialized data is'
  119.                 db      CR, LF, 'stored in the data segment, rather'
  120.                 db      CR, LF, 'than in the code segment.'
  121.                 db      CR, LF
  122.                 db      CR, LF, 'Also, the messages are written using'
  123.                 db      CR, LF, 'block I/O, so a minimum number of DOS'
  124.                 db      CR, LF, 'system services are requested.'
  125.  
  126. msg4            db      0
  127.  
  128. dup_handle      dw      ?
  129. orig_handle     dw      ?
  130.  
  131. datasg          ends
  132.  
  133. stacksg         segment para stack 'stack'
  134.  
  135. mystack         db      512 dup (?)
  136.  
  137. stacksg         ends
  138.  
  139. code            segment para 'code'
  140.  
  141. assume          cs: code
  142. assume          ds: datasg
  143. assume          ss: stacksg
  144. assume          es: nothing
  145.  
  146. test_redirect   proc    far
  147.  
  148. ; Initialize stack pointer and data segment register
  149. ; to the correct values.  The stack pointer is set
  150. ; to the top of the stack segment.  The data segment
  151. ; is set to the segment of the first variable.  Note
  152. ; that at this point in time, the DS register does
  153. ; not point to the PSP.
  154.  
  155.                 mov     sp, 513 ; set up user stack
  156.                 mov     ax, seg msg1
  157.                 mov     ds, ax
  158.  
  159. ; First, write a sign-on message to the screen.  We
  160. ; will attempt to write this message to the standard
  161. ; output device.
  162.  
  163. ;       ah:     INT 21H function id.
  164. ;       bx:     file handle
  165. ;       cx:     # of bytes to transfer
  166. ;       DS:dx   points to message
  167.  
  168.                 mov     ah, F_WRITE
  169.                 mov     dx, offset msg1
  170.                 mov     bx, STD_OUT
  171.                 mov     cx, msg2-msg1
  172.                 int     MS_DOS
  173.  
  174. ; Now, we wish to redirect the output to the
  175. ; printer.  Before we force the redirection,
  176. ; we must make a copy of the standard output
  177. ; file handle and store it in the field
  178. ; orig_handle.
  179.  
  180.                 mov     bx, STD_OUT
  181.                 mov     ah, F_DUP
  182.                 int     MS_DOS
  183.                 mov     word ptr orig_handle, ax
  184.  
  185.                 mov     bx, STD_LST
  186.                 mov     ah, F_DUP
  187.                 int     MS_DOS
  188.                 mov     word ptr dup_handle, ax
  189.  
  190. ; Then, the STD_LST handle is set to track
  191. ; the STD_OUT file.
  192.  
  193.                 mov     bx, ax
  194.                 mov     cx, STD_OUT
  195.                 mov     ah, F_CDUP
  196.                 int     MS_DOS
  197.  
  198. ; Let's write a message out and try it.
  199. ; Note that we are still writing information to
  200. ; the STD_OUT device.
  201.  
  202.                 mov     ah, F_WRITE
  203.                 mov     bx, STD_OUT
  204.                 mov     cx, msg3-msg2
  205.                 mov     dx, offset msg2
  206.                 int     MS_DOS
  207.  
  208. ; Now, let's clean up and return everything
  209. ; back to its original condition.
  210.  
  211.                 mov     bx, word ptr dup_handle
  212.                 mov     ah, F_CLOSE
  213.                 int     MS_DOS
  214.  
  215.                 mov     bx, word ptr orig_handle
  216.                 mov     cx, STD_OUT
  217.                 mov     ah, F_CDUP
  218.                 int     MS_DOS
  219.  
  220.                 mov     ah, F_WRITE
  221.                 mov     bx, STD_OUT
  222.                 mov     cx, msg4-msg3
  223.                 mov     dx, offset msg3
  224.                 int     MS_DOS
  225.  
  226.                 mov     ah, P_TERM
  227.                 int     MS_DOS
  228.  
  229. test_redirect   endp
  230.  
  231. code            ends
  232.  
  233.                 end     test_redirect
  234.