// GMOVESTACK.SR

// outgoing procedures
external [
	movestack
	]

// incoming procedures
external [
	FrameSize; CallersFrame; Usc; errhlt
	]

manifest endCode=#335


let movestack(caller, newCFtop, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil ; numargs na)=valof [
	// extra args are optional pointers to the frame
	// they will be fixed up to point into the new frame

	let myArgs=lv caller
	let cf=CallersFrame()
	let cfs=FrameSize(caller)
	let newCF = newCFtop-cfs+1
	let dist = newCF-cf
	let i=1
	while i ls na-1 do [
		i=i+1
		let t=rv myArgs!i
		if Usc(t, cf) ge 0 & Usc(t, newCFtop) le 0 then
			rv myArgs!i=t+dist
		]

// BMOVE(cf, newCf, cfs-1)
// resultis newCf+cfs
	let DoMove=table [
	#35003	// lda 3 extraArgs,2
	#131400	// inc 1 2
	#173000	// add 3 2
	#61005	// blt
	#121400	// inc 1 0	return address of first new word
	#35001	// lda 3 savedPC,2
	#1401	// jmp 1,3
	]

movestack1: // breakpoint for debugging
	DoMove(cf-1, newCF+cfs-1, -cfs)
	]