;       SCCSID = @(#)d_sizseg.asm	6.1 91/02/15
;	SCCSID = @(#)d_sizseg.asm	1.4 90/01/22
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
; Purpose   DosSizeSeg returns the size of a segment.
;
; Format
;	    Calling sequence:
;
;	    EXTRN DosSizeSeg:FAR
;
;	    PUSH  WORD	Selector ; Selector/Segment
;	    PUSH@ DWORD Size	 ; Size of segment (returned)
;	    CALL  DosSizeSeg
;
;	    where     Selector is the selector/segment	of the
;		      segment	for  which  size  is   to   be
;		      determined.
;
;		      Size  is the  returned  size in bytes of
;		      the segment.  The  value	returned  will
;		      be  less	than  or  equal  to  65535.  A
;		      value of zero indicates 65536 bytes.
;
;	    Returns:   IF AX = 0
;
;			  NO error
;
;		      ELSE
;
;			  AX = error code
;
; Psuedocode:
;		Get segment
;		Subtract 1 to get to Arena
;		Validate signature is or 4D
;		if (VALID)
;			put size from Arena into users buffer
;		else
;			return error
;
;
; Remarks   This   function   is  provided  for  compatibility
;	    purposes for programs  which must run  in Real  or
;	    Protect  Mode.  This  function  is	part  of   the
;	    Family API.
;
; 03-24-88 (robertc) changed errorcode to Access Denied to agree with
;			protect mode.
;

INCLUDE basemaca.inc
INCLUDE dos2equ.inc
INCLUDE error.inc
INCLUDE arena.inc

	EXTRN	__api_begin:near
	EXTRN	__api_end:near


_TEXT	segment byte public 'CODE'

	assume	CS:_TEXT,DS:NOTHING

	public	DOSSIZESEG

Selector	equ	[BP+10]
ptr_Size	equ	[BP+6]

DOSSIZESEG	proc	far

	call	__api_begin	; init bp and save bp,ds,bx,cx,dx,si,di,es
				; required to save: bp,ds,bx
	mov	ax, Selector		; ax = requested segment
	sub	ax, 1			; ax = segments arena
	mov	es, ax			; es = segments arena
	xor	di, di			; clear d1
	xor	bh,bh
	mov	bl, byte ptr es:[di];	; get arena signature
	mov	ax, ERROR_ACCESS_DENIED      ;@@ prepare error
	cmp	bl, arena_signature_normal ; test for arena signature
	jne	ErrorExit

	mov	bx, word ptr es:[di+3];   ; get segment size in paragraphs

	xor	dx,dx
	mov	cx, 4			; convert to words
shift:
	sal	bx, 1
	rcl	dx, 1
	loop	shift

	les	di,[ptr_Size]

	mov	es:[di]  , bx	; pass back lo word
	mov	es:[di+2], dx	; and hi word

	xor	ax,ax			 ; no error
ErrorExit:

	call	__api_end	; check sigs & restore es,di,si,dx,cx,bx,ds,bp

	ret	6	; clear stack of entry parameters (bytes)
DOSSIZESEG	endp

_TEXT	ends

	end
