home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
database
/
trl14db.zip
/
TRLSRC.EXE
/
_TR_ALOC.ASM
next >
Wrap
Assembly Source File
|
1990-10-22
|
5KB
|
147 lines
; ALLOCMEM.ASM
;
; by Ralph Davis
; Modified by Rick Spence
;
; Placed in the public domain by Tom Rettig Associates, 10/22/1990.
;
PUBLIC _TR_ALLOCMEM
; TR-LIB low-level memory allocator for C functions
;
; Syntax: char *_tr_allocmem(lnbytes);
; long lnbytes;
;
; Return: pointer to allocated memory in AX:BX
;=============================================
; Structure for passed parameters
IN_PARMS STRUC
OLD_BP DW ? ; caller's BP
DD ? ; caller's return address
LOW_WORD DW ? ; low word of memory requested
HIGH_WRD DW ? ; high word
IN_PARMS ENDS
;============================================
;*********************************************
CODESEG SEGMENT BYTE PUBLIC
ASSUME CS:CODESEG
;---------------------------------------------
;
;===========================================
; DATA AREA
;
DUMMY_SEG DW ? ; Will save the segment of the dummy memory area
; we allocate
NUM_PARA DW ? ; Will save the number of paragraphs currently
; available
OLD_STRAT DB 0 ; Current allocation strategy
;
ERR_FLAG DB ? ; Has allocation error occurred?
;===========================================
_TR_ALLOCMEM PROC FAR
PUSH BP ; save caller's BP
MOV BP,SP ; set up stack addressability
PUSH DS
PUSH ES
AND CS:ERR_FLAG,0 ; set err_flag to false
MOV AH,58H ; get current allocation
MOV AL,0 ; strategy
INT 21H
MOV CS:OLD_STRAT,AL ; save current strategy
JNC AM2
MOV CS:OLD_STRAT,0 ; assume current strategy is
; first fit if error occurred
AM2: MOV AH,58H ; set strategy to high memory
MOV AL,1
MOV BX,2
INT 21H
JNC AM3 ; successful, go ahead and
; make allocation request
OR ERR_FLAG,0FFH ; set error flag
MOV BX,0FFFFH ; request impossible amount of memory
; to find out how much is available
MOV AH,48H ; try to allocate 1 megabyte
INT 21H ; BX will tell us how many paragraphs
; are available
MOV DX,BX ; save it
AM3:
MOV AX,[BP].HIGH_WRD ; pick up passed parameters
MOV BX,[BP].LOW_WORD
OR AX,AX
JNZ AM3A
OR BX,BX
JNZ AM3A
JMP SHORT ALLOC_ERR
AM3A: REPT 4 ; convert bytes requested
CLC
RCR AX,1 ; to number of paragraphs
RCR BX,1 ; in BX
ENDM
CMP ERR_FLAG,0FFH ; did error occur?
JNE AM4
CMP DX,BX ; find out right now if memory's there
JC ALLOC_ERR
PUSH BX
SUB DX,BX ; we're going to ask for that much
; minus what we want minus 2
MOV BX,DX
SUB BX,2
MOV AH,48H
INT 21H
POP BX
JC ALLOC_ERR
MOV CS:DUMMY_SEG,AX ; save segment so we can free it
AM4:
INC BX ; take care of any modulus-16
MOV AH,48H ; get the memory
INT 21H ; from DOS
PUSHF
CMP ERR_FLAG,0FFH ; was there an error setting strategy?
JNE AM5
MOV AX,CS:DUMMY_SEG ; free dummy block allocated
MOV ES,AX
MOV AH,49H
INT 21H
AM5:
POPF
JNC GOOD_EXIT ; tests carry from function 48H
; request
ALLOC_ERR:
XOR AX,AX ; return NULL pointer
GOOD_EXIT:
XOR BX,BX ; AX:BX now has pointer to
; memory block allocated
PUSH AX
PUSH BX
MOV BL,CS:OLD_STRAT ; reset original allocation strategy
XOR BH,BH
MOV AH,58H
MOV AL,1
INT 21H ; if error, too bad, not crucial
POP BX ; retrieve pointer
POP AX ; to return to calling routine
POP ES
POP DS
POP BP
RET
_TR_ALLOCMEM ENDP
;----------------------------------------
CODESEG ENDS
;****************************************
END