home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
1986
/
06
/
d16bit.jun
< prev
next >
Wrap
Text File
|
1986-06-30
|
8KB
|
235 lines
Listing One
mov dx,seg fname ; open a file for I/O
mov ds,dx ; DS:DX = ASCIIZ filespec
mov dx,offset fname ; function 3DH = open,
mov ax,3d02h ; use mode 2 (read/write)
int 21h
jc error ; jump if open failed
mov my_file,ax ; save handle for file
.
. ; further file
. ; processing here
.
; now use DUP and CLOSE
; to update the directory...
mov bx,my_file ; get handle for file
mov ah,45h ; function 45H = DUP handle,
int 21h
jc error ; jump if DUP failed
mov bx,ax ; now close the DUP'd handle
mov ah,3eh ; function 3EH = close file
int 21h ; transfer to MS-DOS
jc error ; jump if close failed
; otherwise directory is
; updated, continue processing
.
.
error: .
.
.
my_file dw 0 ; handle from previous "open"
fname db 'MYFILE.DAT',0 ; ASCIIZ filespec
Listing Two
page 60, 120
title Redirected I/O example
;
; REDIRECT.ASM --- An illustration of
; I/O redirection under MS-DOS 2.x or 3.x
;
; Copyright (C) 1985 by Jerry Jankura
; Created: 6 November 1985
; Modified: 9 November 1985
;
; Abstract: This routine demonstrates redirection of I/O
; from the console to a line printer. The method may be
; used to redirect I/O from any device to any other device.
; Requires Microsoft MS-DOS 2.X or 3.X, or DRI Concurrent
; DOS version 4.1.
;
STD_IN EQU 0 ; Standard input handle
STD_OUT EQU 1 ; Standard output handle
STD_ERR EQU 2 ; Standare error handle
STD_AUX EQU 3 ; Standard Auxiliary handle
STD_LST EQU 4 ; Standard printer handle
C_WRITESTR EQU 9 ; Write string to STD_OUT
F_DUP EQU 45H ; Duplicate handle
F_CDUP EQU 46H ; Force duplicate handle
F_CLOSE EQU 3EH ; Close file handle
F_WRITE EQU 40H ; Write to file or dev
P_TERM EQU 4CH ; Terminate a program
MS_DOS EQU 21H ; MS-DOS service request
CR EQU 0DH ; Carriage return
LF EQU 0AH ; Line feed
datasg segment para 'data'
msg1 db CR, LF, 'Redirected I/O example....'
db CR, LF
db CR, LF, 'This example was written using the'
db CR, LF, 'File I/O system services, with the'
db CR, LF, 'file handle being set to STD_OUT.'
db CR, LF, 'STD_OUT normally defaults to the'
db CR, LF, 'video screen, so you are reading'
db CR, LF, 'this message on the screen.'
db CR, LF
msg2 db CR, LF, 'However, we may direct STD_OUT to'
db CR, LF, 'another device, such as the printer.'
db CR, LF, 'This message is still written to'
db CR, LF, 'STD-OUT, but is appears at the printer.'
db CR, LF, 'Again, the operating system provides'
db CR, LF, 'the facility to allow one file to mimic'
db CR, LF, 'and track another. The Command processor'
db CR, LF, 'normally implements this redirection'
db CR, LF, 'of standard devices.'
db CR, LF
msg3 db CR, LF, 'This message is written on the'
db CR, LF, 'Video screen, demonstrating that'
db CR, LF, 'a message may be redirected to the'
db CR, LF, 'normal STD_OUT device in the same'
db CR, LF, 'manner that was used to redirect'
db CR, LF, 'it to the printer.'
db CR, LF
db CR, LF, 'Note also that the initialized data is'
db CR, LF, 'stored in the data segment, rather'
db CR, LF, 'than in the code segment.'
db CR, LF
db CR, LF, 'Also, the messages are written using'
db CR, LF, 'block I/O, so a minimum number of DOS'
db CR, LF, 'system services are requested.'
msg4 db 0
dup_handle dw ?
orig_handle dw ?
datasg ends
stacksg segment para stack 'stack'
mystack db 512 dup (?)
stacksg ends
code segment para 'code'
assume cs: code
assume ds: datasg
assume ss: stacksg
assume es: nothing
test_redirect proc far
; Initialize stack pointer and data segment register
; to the correct values. The stack pointer is set
; to the top of the stack segment. The data segment
; is set to the segment of the first variable. Note
; that at this point in time, the DS register does
; not point to the PSP.
mov sp, 513 ; set up user stack
mov ax, seg msg1
mov ds, ax
; First, write a sign-on message to the screen. We
; will attempt to write this message to the standard
; output device.
; ah: INT 21H function id.
; bx: file handle
; cx: # of bytes to transfer
; DS:dx points to message
mov ah, F_WRITE
mov dx, offset msg1
mov bx, STD_OUT
mov cx, msg2-msg1
int MS_DOS
; Now, we wish to redirect the output to the
; printer. Before we force the redirection,
; we must make a copy of the standard output
; file handle and store it in the field
; orig_handle.
mov bx, STD_OUT
mov ah, F_DUP
int MS_DOS
mov word ptr orig_handle, ax
mov bx, STD_LST
mov ah, F_DUP
int MS_DOS
mov word ptr dup_handle, ax
; Then, the STD_LST handle is set to track
; the STD_OUT file.
mov bx, ax
mov cx, STD_OUT
mov ah, F_CDUP
int MS_DOS
; Let's write a message out and try it.
; Note that we are still writing information to
; the STD_OUT device.
mov ah, F_WRITE
mov bx, STD_OUT
mov cx, msg3-msg2
mov dx, offset msg2
int MS_DOS
; Now, let's clean up and return everything
; back to its original condition.
mov bx, word ptr dup_handle
mov ah, F_CLOSE
int MS_DOS
mov bx, word ptr orig_handle
mov cx, STD_OUT
mov ah, F_CDUP
int MS_DOS
mov ah, F_WRITE
mov bx, STD_OUT
mov cx, msg4-msg3
mov dx, offset msg3
int MS_DOS
mov ah, P_TERM
int MS_DOS
test_redirect endp
code ends
end test_redirect