Jump to content
IGNORED

questions about the stack command


jrok

Recommended Posts

This is sort of a two part question. I was curious whether or not it would be possible to create a variable stack pointer? In other words, something like:

 

 dim myVar = a

 x = 187

 stack 188 : push myVar  
 (...)
 stack x : pull myVar

 

I know that the above statement won't work (I mean, it will compile, but it won't retrieve the value at 187), but I was curious if there was a method that would do something similar?

 

The second part of my question is that, if a variable pointer can be used, can we then write a function that points to a location on the stack, and then pulls/pushes a block of contiguous stack data (perhaps with a for-next loop)?

 

Would it help to give a specific example of what I'm trying to do?

Edited by jrok
Link to comment
Share on other sites

You can already use the push and pull to manipulate a range of memory....

 

rem ** save a,b,c
push a-c

rem ** save a,b
push a b

 

No, I understand that. What I was trying to ask is whether the location in the stack at which that range begins to read/write can be assigned to RAM. For instance.

 

 dim stackpointer = d

rem ** save a,b,c
stack stackpointer push a-c

rem ** save a,b
stack stackpointer push a b

Edited by jrok
Link to comment
Share on other sites

I guess I'm hung up on the "why"... maybe to push or pop a bunch of values over a bunch of frames?

 

The "how" just takes a bit of custom assembly code. bB uses the value you provide the stack command with compile-time arithmetic, so we just need to do the same arithmetic at run-time.

 

Here's a subroutine that uses temp1 for setting the stack position...

 

setstackpointer
asm
lda #<(STACKbegin)
clc
adc temp1
STA DF7LOW
lda #(>(STACKbegin))
adc #0
and #$0F
STA DF7HI
end
return

 

And the example basic...

stack-variable.bas

Link to comment
Share on other sites

What I was trying to ask is whether the location in the stack at which that range begins to read/write can be assigned to RAM.

The stack pointer is part of the 6507 CPU, and the 6502 assembly language already has commands to save or set the stack pointer using the X register, so you could create your own in-line assembly code to define a function or macro to TSX and STX to whatever variable you choose, then later to LDX and TXS to restore the stack.

 

Michael

 

Edit: Or are you talking about something else?

Edited by SeaGtGruff
Link to comment
Share on other sites

I guess I'm hung up on the "why"... maybe to push or pop a bunch of values over a bunch of frames?

 

The "how" just takes a bit of custom assembly code. bB uses the value you provide the stack command with compile-time arithmetic, so we just need to do the same arithmetic at run-time.

 

Here's a subroutine that uses temp1 for setting the stack position...

 

setstackpointer
asm
lda #<(STACKbegin)
clc
adc temp1
STA DF7LOW
lda #(>(STACKbegin))
adc #0
and #$0F
STA DF7HI
end
return

 

And the example basic...

stack-variable.bas

 

Thanks! Let me try to see if I can incorporate it into a program and hopefully it will demonstrate the why. Basically, it is actually a bit like popping from an array. In the turn-based RPG game I've been trying to write, each player (or monster) needs to retrieve 8 bytes of RAM from the stack for use during it's "turn." Instead of a static round-robin, I wanted to try to create a sort function to determine the order of turns, so that characters and monsters are ranked in order of initiative at the beginning of a new "round", and then move in that order. The rankings would change based on conditionals (like, for instance, the relative speed properties of the monsters, whether Grog the Barbarian got bonked with an "Super Slow-Mo" Spell, etc).

Link to comment
Share on other sites

And the example basic...

stack-variable.bas

 

 

FYI, this doesn't seem to work quite as I expected (and maybe I'm just not thinking about it the right way). The stackpointer seems to work fine, but I thought "pull a-c" was intended to pull the range of a,b, and c starting at stack 197. If that's the case, shouldn't the following altered version display "040208"?

 

 set kernel DPC+
set kernel_options collision(playfield,player1)
set optimization inlinerand

dim sc0=score
dim sc1=score + 1
dim sc2=score + 2

temp1= temp1

goto Start bank2 

bank 2

temp1= temp1

Start

scorecolors:
12
12
12
12
12
12
12
12
end

a=4
b=2
c=8

rem ** save "abc" on the stack
temp1=100:gosub setstackpointer
push a-c

rem overwrite "a"
a=0
b=0
c=0

rem retrieve "abc" from the stack
temp1=97 : gosub setstackpointer
pull a-c

rem the score displayed should be 080000 
sc0=a
sc1=b
sc2=c

main
drawscreen
goto main

setstackpointer
asm 
lda #<(STACKbegin)
clc
adc temp1
STA DF7LOW
lda #(>(STACKbegin))
adc #0
and #$0F
STA DF7HI
end
return thisbank

 

stack-variable_multi.bas

 

EDIT: Actually, I'm a bit confused about how defining a range works with stack access. The only way I was able to get the stack to work in my RPG program was by reading the stack value directly after the location I wrote it to (for instance, I would push to stack 100, but pull from stack 99.)

Edited by jrok
Link to comment
Share on other sites

FYI, this doesn't seem to work quite as I expected (and maybe I'm just not thinking about it the right way). The stackpointer seems to work fine, but I thought "pull a-c" was intended to pull the range of a,b, and c starting at stack 197. If that's the case, shouldn't the following altered version display "040208"?

Yes, it should, but there appears to be a bug in the "pull #-#" code. I'll submit it to the bug thread shortly.

 

In the meantime, you can substitute "push a b c" and "pull c b a" instead, and it will work as expected.

 

 

EDIT: Actually, I'm a bit confused about how defining a range works with stack access. The only way I was able to get the stack to work in my RPG program was by reading the stack value directly after the location I wrote it to (for instance, I would push to stack 100, but pull from stack 99.)

You were doing it right for a single value. 2 things to keep in mind: (1) the stack grows toward 0. (2) the stack pointer always points to the topmost element in the stack.

 

So when you push a single value onto the stack at position 100, the new element goes on top of the element 100, at position 99. When you want to pull it, it needs to be from position 99. (after the pulling, the stack pointer will point to the next lower slot, at 100.)

 

If the stack pointer was at 100 and you push 3 values on the stack, when the operation is complete the stack pointer is 3 elements closer to 0, at 97. When you pull 3 elements from 97, they are "removed" and the stack pointer winds up pointing to the 100th element.

 

Clear, or no?

Link to comment
Share on other sites

FYI, this doesn't seem to work quite as I expected (and maybe I'm just not thinking about it the right way). The stackpointer seems to work fine, but I thought "pull a-c" was intended to pull the range of a,b, and c starting at stack 197. If that's the case, shouldn't the following altered version display "040208"?

Yes, it should, but there appears to be a bug in the "pull #-#" code. I'll submit it to the bug thread shortly.

 

In the meantime, you can substitute "push a b c" and "pull c b a" instead, and it will work as expected.

 

 

Yes, that's what I've been doing as a substitute. I guess it's really not a bug in that sense, more like an incomplete feature :)

 

EDIT: Actually, I'm a bit confused about how defining a range works with stack access. The only way I was able to get the stack to work in my RPG program was by reading the stack value directly after the location I wrote it to (for instance, I would push to stack 100, but pull from stack 99.)

You were doing it right for a single value. 2 things to keep in mind: (1) the stack grows toward 0. (2) the stack pointer always points to the topmost element in the stack.

 

So when you push a single value onto the stack at position 100, the new element goes on top of the element 100, at position 99. When you want to pull it, it needs to be from position 99. (after the pulling, the stack pointer will point to the next lower slot, at 100.)

 

If the stack pointer was at 100 and you push 3 values on the stack, when the operation is complete the stack pointer is 3 elements closer to 0, at 97. When you pull 3 elements from 97, they are "removed" and the stack pointer winds up pointing to the 100th element.

 

Clear, or no?

 

Yep, perfectly clear. Thanks! :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...