Snippet:KStack

From veswiki
Revision as of 20:25, 5 August 2026 by E5frog (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
;===========================================================================
; KStack Functions
;===========================================================================
; the K stack implements a stack using the registers. it uses r63 as a stack
; pointer that holds the register number of the top of the stack.
; When called, K is pushed to the first two registers on the stack, and the 
; pointer is increased.
;
; at startup, the stack pointer (r63) should be set to 62 ($3E)

;---------------------------------------------------------------------------
; KStack Push
;---------------------------------------------------------------------------
; pushes register K onto the stack using r63 as the stack pointer
; destroys ISAR data

pushk:
		lisu	7				; get the top of the stack
		lisl	7				; r63, stack pointer
		lr	A,S
		lr	IS, A				; load the referenced register	
		lr	A,KU
		lr	I,A				; push high byte of K
		lr	A,KL
		lr	I,A				; push low byte of K
		lr	A,IS
		lisu	7
		lisl	3
		lr	S,A				; save the register number to the stack pointer
		pop

;---------------------------------------------------------------------------
; KStack Pop
;---------------------------------------------------------------------------
; pops register K from the stack using r63 as the stack pointer
; destroys ISAR
                
popk:
		lisu	7				; r63, stack pointer
		lisl	7
		lr	A,S				; A = stack pointer, points to next free
		ai	$ff				; A = last pushed byte, KL
		lr	IS,A				; ISAR -> KL
		lr	A,D				; read KL, then ISAR -> KU
		lr	KL,A
		lr	A,S				; read KU
		lr	KU,A
		lr	A,IS				; ISAR now points to new stack pointer
		lisu	7
		lisl	3
		lr	S,A				; store new stack pointer in r59
		pop