;       SCCSID = @(#)apilmr.asm	6.1 91/02/15
;	SCCSID = @(#)apilmr.asm	1.4 90/01/22
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
;***  apilmr.asm - FAPI loader memory report
;
;	Member of API library
;	Bill Mershon
;	Copyright MicroSoft Corporation, 1986
;	11-Sep-1986
;
;	Reports minimum of 64K and total memory available,
;	converted to paragraphs.
;
;
;	Special Information
;		This routine, when included in the bind step, replaces
;		the api library routine of the same name (located
;		in ...\api\i_apilmr.asm).  The associated api library
;		routine always returns zero.  The stubloader calls
;		this routine and then attempts to allocate the maximum
;		(using int 21h fcn 48h) of the value returned and
;		the memory required for the program.  Function 48h may
;		return an error, in which case the stubloader will fail.
;
;	Modification History
;
;	07-Oct-86 (billme) change api_memrep to return value in
;	bytes rather than paragraphs.
;
;	12-Jan-88 (robertc) allow true 64k auto ds via lobyte flag.
;			stubload.asm will interpret 0ffffh as 64k


INCLUDE basemaca.inc
INCLUDE error.inc


;***	api_memrep - report memory available to loader
;
;	errorcode = unsigned far pascal
;	API_MemRep()
;
;	ENTRY:
;		nothing
;
;	EXIT:
;		if (AX == 0)
;			BX == minimum (in paragraphs) of 64K and
;			total memory available
;		else
;			AX == error code
;
;	USES:
;		AX, BX
;
;	Pseudo Code:
;		BX <- total memory available (using int 21h fcn 48h)
;		if (BX > 64K (in paragraphs))
;			BX <- 64K (in paragraphs)
;		return
;
;	WARNING:
;		may return error code from Dos 2.x, 3.x (see dos manuals)
;
;	EFFECTS:
;		none
;

	title	API_MEMREP - report memory available to loader
;
;	EXTRN	API_MEMREP:FAR
;
;	CALL	API_MEMREP
;

; note: we use 0FFFh for 64K paragraphs (when really 01000h would be more
; accurate) since we need to convert to bytes and 0 will not do.

ALLOCATE_BLOCK	EQU	48h		; int 21h memory alloc function
K64INPARA	EQU	0FFFh		; 64K bytes in paragraphs (less one)

_TEXT	segment byte public 'CODE'

	assume	CS:_TEXT,DS:NOTHING

	public	API_MEMREP

API_MEMREP	proc	far
	mov	bx,0ffffh		; try to allocate more than available
	mov	ah,ALLOCATE_BLOCK
	int	21h			; NOTE: we assume THIS MUST FAIL
	cmp	ax,ERROR_NOT_ENOUGH_MEMORY ; if unexpected error
	jne	mr100			;@@ then return that error
					; else BX contains paragraphs available
	xor	ax,ax			; clear ax
	cmp	bx,K64INPARA		; if less than 64K paragraphs available
	jb	mr99			; then return BX as is
	mov	bx,K64INPARA		; else BX <- 64K in paragraphs
	mov	ax,000fh		;@@ use lobyte as flag
mr99:
	shl	bx,1			; convert BX to byte count
	shl	bx,1
	shl	bx,1
	shl	bx,1
	add	bx,ax			;@@ if 64k then fff0+f (add in flag)
	xor	ax,ax			; clear error
mr100:
	ret

API_MEMREP	endp

_TEXT	ends

	end

