	.title	acsv$	C register save and restore
	.ident	"V02.00"
;+
; Index		C register save and restore with RSX AST's
;
; Usage
;
;	jsr	r5,csv$
;	...
;	jmp	cret$
;
; Description
;
;	Each C subroutine starts with a call to CSV$ and exits by
;	jumping to CRET$.  Upon exit, the stack need not be equal
;	to its value on entrance.  This version preserves R0, so
;	that AST's will work.
;
;	During the execution of all C subroutines,  register R5
;	points to the current "environment."  Within a subroutine,
;	it appears as follows:
;
;		 _______________
;		|		|
;	  SP ->	| 1st loc. var. | -10(R5)	C$AUTO-2(R5)
;		|_______________|
;		|		|
;		| Saved R2	|  -6(R5)
;		|_______________|
;		|		|
;		| Saved R3	|  -4(R5)
;		|_______________|
;		|		|
;		| Saved R4	|  -2(R5)
;		|_______________|
;		|		|
;	  R5 ->	| Saved R5	|
;		|_______________|
;		|		|
;		| Return add.	|  +2(R5)
;		|_______________|
;		|		|
;		| First arg.	|  +4(R5)	C$PMTR+0(R5)
;		|_______________|
;		|		|
;		| Second arg.	|  +6(R5)	C$PMTR+2(R5)
;		|_______________|
;
;	Within a subroutine, Registers R0-R4 and the top of the
;	stack, (sp), are available for use.  Registers R0 and R1
;	are not preserved by subroutines and may be used to pass
;	a return value.
;
;	R5 must not be modified by a subroutine. All variable
;	addressing must be done by offsets to R5.  Subroutine
;	arguments must be accessed by reference to C$PMTR.
;	Subroutine local variables must be accessed by reference to
;	C$AUTO.  This permits modification of calling sequences
;	without rewriting all subroutines.
;
;	CSV$ refers to global symbol $$main to call the run-time
;	startup program from the (RSX) library.
;
;	An RSX AST routine must call astset() immediately upon
;	entry to save R0 and R1.  These registers are restored by
;	astx(), which must be used to exit from an AST service
;	routine.
;
;	See the documentation of the CX library, CX.DOC.
;
;-
; Edit history
; V02.00 19-Oct-82 TTC		Rework of old CS library
;

	.globl	$$main		; Call start from the library

	.psect	c$code
;
; save R4-R2 and make a one-word temp slot on the stack.
;
csv$::
	sub	#10,sp		; make room for stack frame
	mov	r5,(sp)		; put entry point in temp slot
	mov	sp,r5		; r5 --> temp slot
	tst	(r5)+		; r5 --> r2 save slot
	mov	r2,(r5)+	; save r2; r5 --> r3 save slot
	mov	r3,(r5)+	; save r3; r5 --> r4 save slot
	mov	r4,(r5)+	; save r4; r5 --> r5 save slot
	jmp	@(sp)		; enter routine
;
; pop r2-r4 and restore stack linkage registers.
;
cret$::
	mov	r5,r2
	mov	-(r2),r4
	mov	-(r2),r3
	mov	-(r2),r2
	mov	r5,sp
	mov	(sp)+,r5
	rts	pc
	.end
