	.include "os9.d"
	.global	__end

	.text

	.global	ibrk
	; char *ibrk(int)
ibrk:
	addd	heap,u			; d: new heap

	; new heap must be between __end and the stack
	tfr	d,x
	pshs	x
	cmpx	#__end
	blo	.1
	cmps	,s
	bls	.1

	ldd	heap,u			; old heap is base of free memory
	stx	heap,u
	stu	,s
	addd	,s++			; make absolute pointer
	rts

.1:	ldd	#-1
	puls	x,pc


	.data

heap:
	.2byte	__end			; start of heap = end of bss-section
