home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CASM.ARJ
/
SBRK.ASM
< prev
next >
Wrap
Assembly Source File
|
1988-05-08
|
2KB
|
113 lines
;_ sbrk.asm Sun May 8 1988 Modified by: Walter Bright */
; Copyright (C) 1985-1988 by Northwest Software
; All rights reserved
; Written by Walter Bright
include macros.asm
begdata
c_extrn errno,word
enddata
begcode sbrk
c_public sbrk
; Storage allocator
begdata
c_extrn _psp,word
if SPTR
c_extrn _datapar,word
c_extrn _pastdata,word, _progpar,word
else
c_extrn _totalpar,word
endif
enddata
;;;;;;;;;;;;;;;;;;;;;;;;;;
; Request memory from operating system.
; Attempt to grow the data segment.
; Use:
; p = sbrk(nbytes);
; Returns:
; pointer to memory allocated
; (first word of allocated memory contains # of bytes allocated)
; -1 if error
func sbrk
push BP
mov BP,SP
mov BX,P[BP] ;get nbytes
add BX,15 ;round
and BX,0FFF0h ;BX = # of bytes to allocate
jz sbrk3 ;error if sbrk(0)
mov DX,BX ;save
mov CL,4
shr BX,CL ;# of paragraphs to allocate
mov CX,BX ;save
if SPTR
add BX,_datapar ;add in # already in data segment
jc sbrk3 ;too much
.if BX a 0FFFh, sbrk3 ;if > 64k
add BX,_progpar ;BX = total new size of program
else
add BX,_totalpar ;BX = total new size of program
endif
push ES
mov ES,_psp ;segment of start of program
bdos 4Ah ;set new program size
pop ES
if SPTR
jc sbrk2 ;failed
mov AX,_pastdata ;pointer to allocated memory
mov BX,AX
mov [BX],DX ;store # of bytes allocated
add _pastdata,DX ;and remember for posterity
add _datapar,CX ;new size of data segment
else
jnc sbrkok ;succeeded
.if AX ne 8, sbrk2 ;if something is very wrong
;Can't grow data segment. Try to allocate an independent block.
; sub BX,_totalpar ;BX = # of paragraphs req'd
mov BX,CX ;BX = # of paragraphs req'd
bdos 48h ;allocate memory
jc sbrk2 ;failed
jmps sbrk1 ;success. AX = segment of new block
sbrkok:
mov AX,_psp
add AX,_totalpar ;AX = segment of new block
add _totalpar,CX ;new total size of program
sbrk1:
clr BX ;BX = offset of new block
mov ES,AX
mov ES:[BX],DX ;store size of new block
if MSC
mov DX,AX
mov AX,BX
endif
endif
pop BP
ret
sbrk3: mov AX,8 ;fake an ENOMEM
sbrk2: mov errno,AX
mov AX,-1
if LPTR
ifdef MSC
cwd
else
mov BX,AX
endif
endif
pop BP
ret
c_endp sbrk
endcode sbrk
end