// Micro binary output
// last edited June 23, 1978  10:22 AM
// Copyright Xerox Corporation 1979

get "micdecl.d"

external	// O.S.
[	Puts
	MoveBlock
]


// static storage
static [
@oldmem = 0
@oldloc = 0
   ]


let initout(femb) be
// Initialize binary output
  [	if mbext eq 0 then
	  [	let nw = femb>>BS.length/2+1
		let extp = alloc(nw)
		MoveBlock(extp, femb, nw)
		mbext = extp-fstop
	   ]
   ]

and wword(addr,mem,loc) be
// write data word
  [	if (mem ne oldmem) % (loc ne oldloc+1) then
	  [	Puts(outchan,MBseta)
		Puts(outchan,mem!msno)
		Puts(outchan,loc)
		oldmem = mem
	   ]
	oldloc = loc
	let nw = (mem!mswidth + 15) rshift 4
	Puts(outchan,MBdata)
	Puts(outchan,stlct)
	for i = 0 to nw-1 do Puts(outchan,addr!i)
   ]

and wfixup(mem,loc,fep,ep) be
// Write a fixup
  [	Puts(fixchan, mem)
	Puts(fixchan, loc)
	Puts(fixchan, fep)
	Puts(fixchan, ep)
	Puts(fixchan, lbsym)
	Puts(fixchan, stlct-lblct)
   ]

and wsym(ep) be
// write symbol name
  [	ep = ep+1
	Puts(outchan,@ep)
   ] repeatuntil (@ep&#377) eq 0

and wmemdef(mem) be
// write memory definition
  [	Puts(outchan,MBmemdef)
	Puts(outchan,mem!msno)
	Puts(outchan,mem!mswidth)
	wsym(mem)
   ]