|
Post by vectrexrc on Jun 17, 2015 19:23:10 GMT -5
Am I correct in thinking that you need to use the hardware stack to create a temporary "variable" that can be used for the duration of a sub-routines execution?
Can anyone give a code example of doing this?
Thanks
|
|
|
Post by christophertumber on Jun 18, 2015 12:00:23 GMT -5
Am I correct in thinking that you need to use the hardware stack to create a temporary "variable" that can be used for the duration of a sub-routines execution? No tmp EQU $c880 .... SubroutineA: lda #Value1 sta tmp ... rts .... SubroutineB: lda #Value2 sta tmp ... rts .... SubroutineC: lda #Value3 sta tmp ... rts
|
|
|
Post by vectrexrc on Jun 19, 2015 11:13:34 GMT -5
yeah that's using a "global" variable. i guess that's ok and different routines could share it.
I was thinking more like declaring and using a "local" variable.
think i figured it out - use the stack a memory location to save the "local" variables...
eg. a simple subroutine:
// start // initialize temp location on the stack with default value 0
LDA #0 PSHS A
//do something here eg. increment the temp variable
LDA 1,S ADDA #1 STA 1,S
// do something else that will trash the value of A .......
// now get the temp variable value again LDA 1,S
// do something
// release the temp variable from the stack PULS A
// end subroutine RTS
|
|
|
Post by vectrexrc on Jun 19, 2015 11:16:29 GMT -5
probably inefficient when compared to using global variables
|
|
|
Post by vectrexrc on Jun 19, 2015 11:18:22 GMT -5
it is LDA 1,S and STA 1,S as the stack point decrements as values are put onto the stack (so a positive offset is required to index the value)
|
|
|
Post by garryg on Jun 19, 2015 13:53:47 GMT -5
What are you trying to do? This initially seems like an unnecessary use of the stack. Why are you not just referencing memory locations instead?
|
|
|
Post by vectrexrc on Jun 19, 2015 19:19:51 GMT -5
never mind think i figure what i need to do
its for my custom text drawing routine
eg.
LDA #$10 ; draw text at 0,10 LDB #$00 LDU #string1 ; the string JSR drawtext
in my subroutine to keep a copy of the x,y location without needing to dedicate global memory location i use the stack like this
drawtext: PSHS A,B ; save start location on stack
JSR Reset0Ref ; reset vector LDA ,S ; reload start location coords LDB 1,S JSR Moveto_d ; move to start location
at the moment i have dedicated memory location to keep count of the chars being drawn because I need special handling when the string is long to prevent vector wobble. this is why i need to keep the original start coords to do this and using stack mean i don't need to dedicated mem location.
LDB #$00 ; clear char counter STB charcount
I will just use a stack location instead like the example in previous posting
thought its better to keep the subroutine self contained
|
|
|
Post by christophertumber on Jun 19, 2015 19:35:14 GMT -5
This will be the first thing you rewrite when you need to optimize for speed.
You're going to eventually need to stop thinking like you're developing in C, C++, C#, Java, Python or whatever.
You're much better off dedicating a bit of RAM to scratch/local variables. You can use as many labels as you need when defining RAM to preserve readability.
|
|
|
Post by garryg on Jun 19, 2015 19:46:56 GMT -5
I agree with christophertumber. I usually have some locations set aside for loop counters etc, instead of overly relying on the stack. It may look messy but you are better off in the long run.
But good luck with your procedure...
|
|
|
Post by kokovec on Jun 21, 2015 16:27:37 GMT -5
Plus you can get in trouble with the stack if you're not careful.
|
|
|
Post by binarystar on Jun 22, 2015 9:39:31 GMT -5
Yeah, as a rule I'd avoid stack manipulation unless totally necassary. I normally reserve a few temp variables (typically 9 bytes - defined as 3 8bit and 3 16bit numbers) for temp stuff.
|
|
|
Post by mountaingoat on Jun 28, 2015 10:38:59 GMT -5
There is a great example for local stack allocation / deallocation for this purpose in Ville Krumlinde's Thrust source code. I recommend you check out his page at www.emix8.org/static.php?page=VectrexThrustSourceHe has a section on local variables. The actual macros for them are in DEF.ASM.
|
|
|
Post by gauze on Feb 10, 2020 1:15:59 GMT -5
The second post shows you that you can reuse global variables and treat them as "local" within subroutines but one thing that I have learned to do is then set up a "local" assign label so the code is a bit easier to read in subroutines with these local variables is assign another name for it, and alias basically (for use in macros it's very different btw, this is only for subroutines)
; in your RAM block somewhere temp0 ds 2 ; 3 2 byte generic "variables" temp1 ds 2 temp2 ds 2 ...
some_subroutine: duckcount = temp0 duckmove = temp1 lda #0 sta count ;etc ;etc rts another_routine: goosecount = temp0 goosemove = temp1 lda #1 sta goosecount ;etc rts
you do not want to reuse these local names anywhere else btw! ps this is identical to doing it all in one place like this:
duckcount ; label, doesn't take up any ram space goosecount ; label, doesn't take up ram space temp0 ds 2 ; 2 bytes, above 2 are aliases to same RAM location
|
|