

; -------------= Register Modify Subroutine =--------------
; this subroutine modifies the registers on the user stack. 
; for use after an SWI.
; ---------------------------------------------------------
***
;reg_edit_sub: "m_rm.asm"
;	Called from the command line: "> rm <reg> <val>"
;	Command line arguments:
;			<reg> is a register name <cbaxyp>
;			<val> is the value you want in the register
;	Reg Input:
;		None
;	Reg Output:
;		None
***


reg_edit_sub:
	jsr		skip_wspace		; skips over any space characters. see "util.asm"
	ldaa		0,y			; get the name of the register we want to edit
						; Y is the character buffer.
	
	iny					; increment Y to point at the next character
	pshy					; save it for later to get byte/word to put in reg	

	clrb					; B will be the offset counter into the regs on the stack
	ldx		#reglist		; X will be used as a pointer to the reglist (to compare to A)
						; reglist is in "m_rv.asm"
	ldy		#rl_size		; Y will be used to check the size of the regs to increment B
						; rl_size is in "m_rv.asm"

re_loop1:
	cmpa		0,x			; is the reg we're looking for in 0,x (reglist)?
	beq		re_gotit		; yes, we got it

	psha					; no, increment x, y, b (with correct # of bytes)
	ldaa		0,y
	cmpa		#$00			; is it a byte?
	beq		re_bs1			; if it is, just add one to b
	incb					; otherwise, add 2 to b
re_bs1:					
	incb					; yes, just add one
	pula					; restore A
	inx					; increments X
	iny					; increments Y
	cpx		#rl_size		; did we get to the end of the reglist and not find it?
	beq		re_error		; yes, then its an error
	bra		re_loop1		; no, lets continue

						; at this point B is the offset into the user's 
						; stack and [0,y] tells us how 
re_gotit:					; big it is. X is unnecessary.
	ldx		SP_save			; load X with the user's stack pointer
	abx					; add B to X to get a pointer to what we need
	ldaa		0,y			; load A with the size byte
	cmpa		#$00			; is it a byte?
	beq		re_bs2			; yes, then do something different



*						* another register switch with pushes and pulls

						; for 16-bit registers
	puly					; restore the loc in the cmd buffer
	pshx					; save the addr
	jsr		parse_address		; get the new value for this register (ret in X)
	cmpb		#$ff			; error?
	beq		re_error		; if it is an error, branch to the error section
	puly					; restore the addr in the user's stack
	stx		0,y			; save the new value to [0,y]
	bra		re_done			; end


re_bs2:						; 8-bit registers
	puly					; restore the loc in the cmd buffer
	jsr		parse_byte		; get the number to put in the reg
	cmpb		#$00			; error?
	beq		re_done			; yep -- just go.
	staa		0,x			; store one byte in the proper section
	bra		re_done			; end
re_error:
	puly					; pulls one off the stack so that a return can occur
re_done:
	rts					; returns
