cseg	segment para public 'code'
; Copyright 1983 Data Base Decisions

; This program is used to load an EXE file faster than BASIC.
; To use it, enter 'BRUN', after having poked the program name
; into the DOS communications area 0:4f0 - 0:4ff.
; This program is similar to, but different from RUN.COM. First,
; it is an EXE file, and can be run directly from a compiled BASIC
; program. Since compiled basic does not support the use of command
; line parameters, the name of the program to be run is put in the
; dos communications area. Be sure to follow the program name with
; at least one space. This program runs EXE programs only.

recct	equ 504 		; max recs per read

sseg	segment para stack 'stack'
	db 16 dup('stack   ')
sseg	ends

brun	proc far
	assume cs:cseg,ds:cseg,ss:sseg,es:nothing

	cli			; switch to internal stack
	push cs
	pop ss
	mov sp,offset estak
	sti
	call p100		; initialize
	call p200		; open file
	jmp p300		; copy this prog to hi memory
p010:	call p400		; read exe file to lo memory
	jmp p600		; relocate and execute prog

p050:	mov ah,9		; print error message
	int 21h
	mov ax,oseg
	push ax
	xor ax,ax
	push ax
	ret 0			; terminate

p100	proc near		; initialize
	mov ax,ds
	mov si,2
	mov bx,[si]		; get top of memory from psp
	push cs
	pop ds			; make ds=cs
	push cs
	pop es			; make es=cs
	mov oseg,ax		; save original seg
	add ax,10h
	mov nseg,ax		; save new prog seg
	mov topmem,bx		; save top of memory

	mov ax,offset endprog
	add ax,15
	and ax,0fff0h
	mov eprog,ax		; point to end of prog
	mov cl,4
	shr ax,cl
	mov bx,ds
	add ax,bx
	mov dtads,ax		; point to start dta

	push ds
	mov ax,0
	mov ds,ax
	mov si,4f0h		; point to dos comm area
	mov di,offset fcb	; work fcb
	mov ax,2901h		; parse file name
	int 21h
	pop ds

	mov di,offset fcb+9	; make sure extension is 'EXE'
	mov si,offset exe
	mov cx,3
	repnz movsb		; copy 'EXE' to extension
	ret
p100	endp

p200	proc near		; open file
	mov dx,offset fcb
	mov ah,0fh
	int 21h
	and al,0ffh		; error?
	jz p210 		; no
	mov dx,offset errmsg1
	jmp p050

p210:	mov si,offset fcb+16	; get file size
	mov ax,[si]		; lower part
	mov cl,7
	shr ax,cl		; divide by 128
	mov bx,ax		; hold in bx
	mov ax,[si+2]		; upper part of file size
	mov cl,9
	sal ax,cl		; divide by 128
	add ax,bx		; total file size divided by 128
	mov norecs,ax		; save as norecs

	mov ax,128
	mov si,offset fcb+14	; set recsize = 128
	mov [si],ax
	mov al,0		; zero 5 bytes in fcb
	mov di,offset fcb+32
	mov cx,5
	repnz stosb		; clear fcb bytes 32 - 36

	mov ax,norecs
	mov cl,3
	sal ax,cl		; # of pghs for prog
	add ax,dtads		; top of prog (pghs)
	mov bx,topmem		; top of memory
	sub bx,ax		; is there room?
	jae p220		; yes
	mov dx,offset errmsg4
	jmp p050

p220:	ret
p200	endp

p300:				; read exe header
	mov cx,4
	call p500		; read 4 recs
	mov si,eprog
	mov ax,[si]		; get EXE signature
	cmp ax,5a4dh		; valid exe file?
	jz p310 		; yes
	mov dx,offset errmsg2
	jmp p050

p310:				; check stack placement
	mov ax,[si+10h] 	; get value for sp
	mov cl,4
	shr ax,cl		; convert to pghs
	add ax,nseg		; add base seg
	add ax,[si+0eh] 	; and sp offset
	mov bx,topmem		; get top of memory
	cmp bx,ax		; is there room for the stack?
	jae p315		; yes
	mov dx,offset errmsg4
	jmp p050

p315:	mov ax,[si+8]		; get header size (pghs)
	mov cl,3
	shr ax,cl		; divide by 8 to get 128-byte records
	sub ax,4		; already read
	mov cx,ax
	jcxz p320		; no more header
	call p500		; read rest of header

p320:	mov ax,norecs		; recs left
	mov cl,3
	sal ax,cl		; convert to pghs
	add ax,nseg		; plus start point gives copy destination
	mov bx,dtads		; end of header
	cmp ax,bx		; is start point above header?
	jae p325		; yes
	mov ax,bx		; no - change copy destination

p325:	mov es,ax		; copy prog here
	sub bx,oseg		; minus psp gives pghs to copy
	mov cl,3
	sal bx,cl		; words to copy
	mov cx,bx
	xor di,di
	xor si,si
	rep movsw		; copy it

	mov di,offset p330
	push es
	push di
	ret 0			; jump to new copy

p330:	push es
	pop ds			; set ds=es=new cs
	mov ax,es
	mov ss,ax		; set ss=es
	jmp p010

p400	proc near		; read file
	mov ax,nseg
	mov dtads,ax
p410:	mov cx,norecs		; # of records left
	sub cx,recct		; 63K worth?
	ja p420 		; yes
	add cx,recct		; no - read remainder
	jmp p430

p420:	mov cx,recct		; read 63K
p430:	call p500		; read file

p440:	mov cx,norecs		; any more to read?
	jcxz p450		; no
	jmp p410		; read next block

p450:	ret
p400	endp

p500	proc near		; read file - cx contains recs to read
	push ds
	mov bx,dtads
	mov ds,bx		; new ds
	mov dx,0
	mov ah,1ah
	int 21h 		; set dta
	pop ds
	mov dx,offset fcb
	mov ah,27h
	int 21h 		; random block read
	and al,0ffh		; error?
	jz p510 		; no
	mov dx,offset errmsg3
	jmp p050

p510:	mov ax,cx		; adjust dta for next read
	sub norecs,ax
	mov cl,3
	sal ax,cl		; 8 pghs/record
	add dtads,ax
	ret
p500 endp

p600:	mov ax,nseg		; relocate and execute it
	mov si,eprog
	mov bx,si
	add bx,[si+18h] 	; first relocation item
	mov cx,[si+6]		; # of items
	jcxz p620		; none
p610:	mov dx,[bx+2]		; get code seg
	add dx,ax		; add start seg
	mov es,dx		; in es
	mov di,[bx]		; set instruction in es
	add es:[di],ax		; add start seg
	add bx,4		; do another
	loop p610		; done?

p620:				; pass control to exe prog
	mov ax,nseg		; new prog seg
	add ax,[si+0eh] 	; plus ss offset gives new ss
	cli
	mov ss,ax
	mov sp,[si+10h]
	sti

	mov ax,nseg		; new prog seg
	add ax,[si+16h] 	; plus cs offset gives new cs
	mov bx,[si+14h] 	; new ip
	push ax
	push bx

	mov ax,oseg		; set up ds & es
	mov ds,ax
	mov es,ax

	mov ah,1ah		; reset dta
	mov dx,80h
	int 21h
	xor ax,ax		; clear drive status

	ret 0			; far return to exe prog

errmsg1 db 'Program not found',7,10,13,'$'
errmsg2 db 'Header not found ',7,10,13,'$'
errmsg3 db 'Unable to load program',7,10,13,'$'
errmsg4 db 'Not enough memory',7,10,13,'$'

oseg	dw 0			; original data seg
nseg	dw 0			; start of new prog
topmem	dw 0			; top of memory
norecs	dw 0			; # of 128 byte recs in file
dtads	dw 0			; dta data seg
exe	db 'EXE'
fcb	db 37 dup(0)		; work fcb
eprog	dw 0			; end of this prog
stak	db 16 dup('stack   ')   ; temp stack
estak	db 0

endprog equ this word

brun	endp
cseg	ends
end	brun
                                                                                                                          