//OS Utilities--simplified for Midas

get "AltoFileSys.d"

// outgoing procs
external [ Wss; Wns; Wos ]


// Incoming procedures
external [
// Streams
	Gets; Puts; Closes; Endofs; Resets
	Usc
	DefaultArgs
]


// Utility functions
let Wss(s, str) be
 for i=1 to str>>STRING.length do Puts(s, str>>STRING.char↑i)

and Wns(Stream, Num, Width, Radix; numargs NumArgs) be
// Do numeric output to stream.
// Suppresses leading spaces if you tell it to, even if the radix is 8.
[
DefaultArgs(lv NumArgs,-2,1,-10)  // Default Width=1, Radix=10, signed
let V=vec 16  // Max space needed for unpacked digits
let Negative=false
if Radix ls 0 then  // "Negative" only if decimal
   [
   Radix=-Radix
   if Num ls 0 then
	[
	Num=-Num
	Negative=true
	Width=Width-1  // Space for minus sign
	]
   ]
let I=0
   [
   V!I=(table	// Divide Num by Radix, put quotient back in Num,
      [		//  and return remainder.
       #55001	// sta 3 1 2
      #155000	// mov 2 3	// preserve frame pointer
       #31407	// lda 2 7 3	// get Radix from frame
       #25405	// lda 1 5 3	// get Num from frame
      #102460	// mkzero 0 0	// high dividend = 0
       #61021	// div
       #77400	// Swat		// if divide fails
       #45405	// sta 1 5 3	// store quotient back into Num
      #171000	// mov 3 2	// recover frame
       #35001	// lda 3 1 2
        #1401	// jmp 1 3
      ])()
   I=I+1
   ] repeatwhile Num ne 0
while Width gr I do [ Puts(Stream,$*s); Width=Width-1]
if Negative then Puts(Stream,$-)
while I ne 0 do [ I=I-1;  Puts(Stream,$0+V!I) ]
]

and Wos(s, n) be Wns(s, n, 6, 8)