return - Address Offset

AX (CL in danger) CX (AX in danger) XX (CL, AX in danger)
parm

Chunk
Pointer

AL entry ;Preserve CL
PUSH CX

MOV CL,AL
;Preserve AH
MOV CX,AX
;Preserve CL,AH
XCHG CX,XX
MOV CX,AX
exit POP CX XCHG AX,XX XCHG AX,XX
XCHG AX,CX
AH entry ;Preserve CL
XCHG AL,AH

PUSH CX

MOV CL,AL
;Preserve AL
XCHG AL,AH

MOV CX,AX
;Preserve CL,AH
XCHG AL,AH

XCHG CX,XX
MOV CX,AX
exit POP CX XCHG AX,XX
XCHG AL,AH
XCHG AX,XX
XCHG AX,CX
XCHG AL,AH
CL entry ;Ideal call
MOV AL,CL
;Preserve AX
PUSH AX

MOV AL,CL
;Preserve AX
XCHG AX,XX

MOV AL,CL
exit XCHG AX,XX
POP AX
XCHG AX,XX
CH entry ;Preserve CL
XCHG CH,CL

MOV AL,CL
;Preserve AX
PUSH AX

XCHG CH,CL

MOV AL,CL
;Preserve CL,AX
XCHG AX,XX

XCHG CL,CH

MOV AL,CL
exit XCHG CH,CL XCHG AX,XX
POP AX
XCHG AX,XX
XCHG CL,CH
XX entry ;Preserve CL
XCHG XX,CL

MOV AL,CL
;Preserve AX
PUSH AX

XCHG XX,CL

MOV AL,CL
;Preserve CL,AX
XCHG AX,XX

XCHG CL,XX

MOV AL,CL
exit XCHG XX,CL XCHG AX,XX
POP AX
XCHG AX,XX
XCHG CL,XX

cp2oset	MACRO		;Preferably chunk pointer in cl and returns result in ax or any other 16 bit reg or var

	##IF '#1' EQ 'AH'
	XCHG AL,AH
	##ENDIF

	##IF '#1' EQ 'AH' OR '#1' EQ 'AL'
	 ##IF '#2' EQ 'AX' ;AH (now in AL) or AL to AX: Preserve all of CX and destination not available so must use stack
	  PUSH CX
	  MOV CL,AL
	 ##ELSE 
	  ##IF '#2' NE 'CX' ;AH (now in AL) or AL to anything but AX or CX: use eventual destination to save CX
	   XCHG CX,#2
	   ##ENDIF
	  ;AH (now in AL) or AL to anything but AX: Get #1 into CL and use CH to save AH or AL (now in AH)
	  MOV CX,AX 
	  ##ENDIF
	##ELSE ;not AH or AL
	 ##IF '#2' EQ 'CX'
	  PUSH AX ;not AH or AL to CX: Preserve all of AX and destination not available so must use stack
	 ##ELSE
	  ##IF '#2' NE 'AX';not AH or AL and not AX or CX
	   XCHG AX, #2
	   ##ENDIF
	  ##ENDIF
	 ##IF '#1' NE 'CL' ;not AH, AL or CL
	  XCHG CL,#1
	  ##ENDIF
	 MOV AL,CL
	 ##ENDIF

	XOR AH,AH
	AND CL,03
	SHL AX,CL
	SHL AX,CL
	ADD AL,CL
	ADC AX,0
	INC AX
	
	##IF '#2' EQ 'AX'
	 ##IF '#1' EQ 'AL' OR '#1' EQ 'AH'
	  POP CX
	 ##ELSE ; not AL or AH
	  ##IF '#1' NE 'CL'
	   XCHG #1,CL
	   ##ENDIF
	  ##ENDIF
	##ELSE ; not AX
	 XCHG AX,#2
	 ##IF '#2' EQ 'CX'
	  ##IF '#1' EQ 'AL' OR '#1' EQ 'AH'
	  ##ELSE
	   POP AX
	   ##ENDIF
	 ##ELSE ; not AX or CX
	  ##IF '#1' EQ 'AL' OR '#1' EQ 'AH'
	   XCHG AX,CX
	  ##ELSE ; not AX or CX and not AL or AH
	   ##IF '#1' NE 'CL'
	    XCHG #1,CL
	    ##ENDIF
	   ##ENDIF
	  ##ENDIF
	 ; not AX
	 ##IF '#1' EQ 'AH'
	  XCHG AL,AH
	  ##ENDIF
	 ##ENDIF