; ; readfile.asm ; ; Demo program to read file contents into memory. Get file length, ; allocate space, and read the data ; ; David Lindauer, feb 15,1996 gclind01@starbase.spd.louisville.edu ; ; .model small .stack .data .386 fname db "temp.dat",0 handle dw 0 fsize dw 0 datapos dd 0 enofile db "No such file",13,10,'$' enomem db "Not enough memory",13,10,'$' endseg SEGMENT public "XXX" endseg ends .code start: mov ax,DGROUP ; DS = data segment mov ds,ax ; mov ax,es ; mov bx,endseg ; BX = end of prog - psp sub bx,ax ; mov ah,4ah ; Refsize PSP down int 21h ; mov ah,3dh ; open fle mov al,0 ; For read mov dx,offset fname ; use fname int 21h jc nofile ; Get out if not existant mov bx,ax ; save handle mov [handle],bx mov ah,42h ; Reposition to end of file mov al,2 ; sub cx,cx ; Offset of 0 sub dx,dx ; int 21h ; mov [fsize],ax ; save fsize (assume < 64K-16 bytes) add ax,15 ; Round up to next paragraph shr ax,4 ; Need a paragraph for the allocate mov bx,ax ; mov ah,48h ; Allocate mem for the image int 21h ; jc nomem ; mov word ptr [datapos+2],ax ; Save position mov ah,42h ; Reposition to start of file mov al,0 ; mov bx,[handle] ; get handle sub cx,cx ; OFFSET = 0 sub dx,dx ; int 21h ; mov bx,[handle] ; Prepare to do the read mov cx,[fsize] ; mov ah,3fh ; push ds ; lds dx,[datapos] ; Load up position to read to int 21h ; Read data pop ds ; Restore DS mov bx,[handle] ; Close the file mov ah,3eh ; int 21h ; exit: mov ah,4ch ; Get out, DOS will deallocate memory int 21h ; automatically nofile: mov dx,offset enofile ; Get no file error perror: mov ah,9 ; Print it int 21h ; jmp exit ; Exit nomem: mov dx,offset enomem ; Get no memory error jmp perror ; print it & exit end start