home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CASM.ARJ
/
IO.ASM
< prev
next >
Wrap
Assembly Source File
|
1988-05-17
|
7KB
|
381 lines
;_ io.asm Thu Apr 21 1988 Modified by: Walter Bright */
;Copyright (C) 1985-1988 by Northwest Software
;All Rights Reserved
;Written by Walter Bright
include MACROS.ASM
begdata
c_extrn errno,word
enddata
begcode io
c_public read,write,open,close,creat,unlink,lseek,getDS,filesize,rename
c_public dos_creat
c_public isatty,remove
if SPTR
c_public _readx,_writex
endif
;;;;;;;;;;;;;;;;;;;;;;;;;
; Read data from a file.
; Use:
; read(fd,buffer,length)
; Returns:
; -1 error
; 0 end of file
; n number of bytes actually read
;
func read
mov AH,3Fh ;read from file or device
F4: push BP
mov BP,SP
mov BX,P[BP] ;BX = fd (file handle)
if SPTR
mov DX,P+2[BP] ;DX = buffer address
mov CX,P+2+SIZEPTR[BP] ;CX = number of bytes to read/write
bdos ;read/write from/to file or device
else
push DS
lds DX,P+2[BP] ;DS:DX = buffer address
mov CX,P+2+SIZEPTR[BP] ;CX = number of bytes to read
bdos ;read from file or device
pop DS
endif
F2: jnc F1 ;no error
mov errno,AX ;save DOS error number
sbb AX,AX ;error
F1: pop BP
ret
c_endp read
;;;;;;;;;;;;;;;;;;;;;;;;;
; Read data from a file.
; Use:
; _readx(fd,buffer,length,segment)
; Returns:
; -1 error
; 0 end of file
; n number of bytes actually read
;
if SPTR
func _readx
mov AH,3Fh ;read from file or device
F3: push BP
mov BP,SP
mov BX,P[BP] ;BX = fd (file handle)
mov DX,P+2[BP] ;DX = buffer address
mov CX,P+4[BP] ;CX = number of bytes to read/write
push DS
mov DS,P+6[BP] ;DS = segment of buffer
bdos ;read/write from file or device
pop DS
jmps F2
c_endp _readx
endif
;;;;;;;;;;;;;;;;;;;;;;;;;
; Write data to a file.
; Use:
; int write(fd,buffer,length)
; Returns:
; -1 error
; n number of bytes actually written
;
func write
mov AH,40h ;write to file or device
jmps F4
c_endp write
;;;;;;;;;;;;;;;;;;;;;;;;;
; Write data to a file.
; Use:
; int _writex(fd,buffer,length,segment)
; Returns:
; -1 error
; n number of bytes actually written
;
if SPTR
func _writex
mov AH,40h ;write to file or device
jmps F3
c_endp _writex
endif
;;;;;;;;;;;;;;;;;;;;;;;;;
; Return data segment.
func getDS
mov AX,DS
ret
c_endp getDS
;;;;;;;;;;;;;;;;;;;;;;;;;
; Open a file
; Use:
; int open(name,rwmode)
; Returns:
; -1 error
; fd file handle
;
func open
push BP
mov BP,SP
mov AH,3Dh ;open file
mov AL,P+SIZEPTR[BP] ;AL = rwmode (0,1,2)
OPEN1:
if SPTR
mov DX,P[BP] ;DX -> name
bdos
else
push DS
lds DX,P[BP] ;DS:DX -> name
bdos
pop DS
endif
jmp F2
c_endp open
;;;;;;;;;;;;;;;;;;;;;;;;;
; Create a file
; Use:
; int creat(name,pmode)
; Returns:
; -1 error
; fd file handle
;
func creat
push BP
mov BP,SP
clr CX ;always ignore file attribute
jmps creat1
c_endp creat
func dos_creat
push BP
mov BP,SP
mov CX,P+SIZEPTR[BP] ;CX = file attribute
creat1:
mov AH,3Ch ;create file
jmp OPEN1
c_endp dos_creat
;;;;;;;;;;;;;;;;;;;;;;;;;
; Close a file
; Use:
; int close(fd)
; Returns:
; -1 error
; 0 successful
;
func close
push BP
mov BP,SP
mov BX,P[BP] ;file handle
bdos 3Eh ;write to file or device
jmp U2
c_endp close
;;;;;;;;;;;;;;;;;;;;;;;;
; Rename a file. J.K.H. 2/15/86
; Use:
; int rename (from, to)
; Returns:
; -1 error
; 0 successful
;
func rename
push BP
mov BP,SP
.save DI
mov AH,56h ;DOS rename function
if SPTR
ife ESeqDS
mov BX,DS
mov ES,BX
endif
mov DI,P+SIZEPTR[bp] ;The new name.
else ;LPTR
les DI,P+SIZEPTR[bp] ;The new name.
endif
jmps U1
c_endp rename
;;;;;;;;;;;;;;;;;;;;;;;;;
; Delete a file
; Use:
; int unlink(name)
; Returns:
; -1 error
; 0 successful
;
func remove
c_endp remove
func unlink
push BP
mov BP,SP
.save DI
mov AH,41h ;delete file function
U1:
if SPTR
mov DX,P[BP] ;DX -> name
bdos
else
push DS
lds DX,P[BP] ;DS:DX -> name
bdos
pop DS
endif
.restore DI
U2: jnc L7 ;no error
mov errno,AX
L7: sbb AX,AX ;-1 if error, 0 if not
pop BP
ret
c_endp unlink
;;;;;;;;;;;;;;;;;;;;;;;;;
; Seek to specified file position.
; Use:
; long lseek(int fd,long offset,int mode)
; Input:
; mode = SEEK_SET or SEEK_CUR or SEEK_END
; Returns:
; -1L error
; n new file position
;
func lseek
push BP
mov BP,SP
mov BX,P[BP] ;file handle
mov DX,P+2[BP] ;lsw of offset
mov CX,P+4[BP] ;msw of offset
mov AL,P+6[BP] ;mode (0,1,2)
bdos 42h ;write to file or device
jnc L9 ;no error
mov errno,AX ;save DOS error number
sbb AX,AX ;AX = -1
if MSC
cwd ;DX = -1
L9:
else
mov BX,AX ;error
pop BP
ret
L9: mov BX,AX ;lsw
mov AX,DX ;msw
endif
pop BP
ret
c_endp lseek
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Get and return the size of a file.
; Use:
; long filesize(filename)
; char *filename;
; Returns:
; -1L error
func filesize
push BP
push DS
sub SP,44 ;44 bytes for DTA
mov BP,SP
;Set DTA to the 44 bytes on the stack (SS:BP)
if LPTR
push SS
pop DS
endif
mov DX,BP ;DS:DX is DTA
bdos 1Ah ;set DTA
;Find first matching file
if SPTR
mov DX,44+2+P[BP]
else
lds DX,44+2+P[BP]
endif
mov CX,6 ;find all normal files, plus system and hidden
bdos 4Eh ;findfirst (DS:DX -> filename)
jc L11 ;no error
;Load file size from DTA
if MSC
mov DX,28[BP]
mov AX,26[BP]
else
mov AX,28[BP]
mov BX,26[BP]
endif
L12: add SP,44
pop DS
pop BP
ret
L11:
if LPTR
mov DS,44[BP] ;restore DS
endif
mov errno,AX ;remember error code
sbb AX,AX
if MSC
cwd
else
mov BX,AX ;return -1L on error
endif
jmp L12
c_endp filesize
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Determine if handle is a tty.
; Use:
; int isatty(fd)
; int fd;
; Returns:
; !=0 character device
; 0 not a character device or error
func isatty
mov BX,SP
if SPTR
mov BX,P-2[BX] ;get fd (file handle)
else
mov BX,SS:P-2[BX] ;get fd (file handle)
endif
mov AX,04400h ;get device information
bdos ;IOCTL
jc I2
;If sign bit is set in DL, it is a character device.
mov AL,DL
cbw ;AH = 0FFh if char dev, else 0
mov AL,AH
ret
I2: mov errno,AX
clr AX
ret
c_endp isatty
endcode io
end