It is currently Sun Sep 23, 2018 4:03 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Peeking into the stack?
PostPosted: Fri Dec 29, 2017 3:16 pm 
Offline

Joined: Fri Dec 29, 2017 3:08 pm
Posts: 1
I would like to peek into the stack to get a value N bytes from the top of the stack.
But I cannot find an example for this.

Is there some syntax like in NESASM similar to NASM's where you can get a value at an address that is the sum of a register plus a certain value?
Similar to this:

Code:
lda [S+10]


Top
 Profile  
 
PostPosted: Fri Dec 29, 2017 3:20 pm 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 7541
Location: Seattle
The plain 6502 has no stack-indexed instructions, unfortunately.

You basically have to save or discard the contexts of X, use the TSX instruction, and use instructions that use absolute-indexed addressing like LDA $0100,X

This is one of the many reasons that cc65 uses a separate software stack for function parameters, instead of using the same stack as the one that holds return addresses.


Top
 Profile  
 
PostPosted: Fri Dec 29, 2017 5:29 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20565
Location: NE Indiana, USA (NTSC)
On 6502 (NES), you would indeed use TSX and index into the stack that way. Just watch for wraparound if there aren't already $10 bytes pushed to the stack.

On 65816 (Super NES), the stack behaves more like an ordinary index register in that you can use LDA $10,S, or even LDA ($10,S),Y.


Top
 Profile  
 
PostPosted: Fri Dec 29, 2017 6:33 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10814
Location: Rio de Janeiro - Brazil
The most common way to read the Nth element from the top of the stack is:
Code:
  tsx
  lda $100+N, x


Top
 Profile  
 
PostPosted: Fri Dec 29, 2017 11:34 pm 
Offline

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 112
Location: Southern California
See my 6502 stacks treatise. (Notice it's "stacks," plural, as it includes virtual stacks, and is not limited to just the page-1 hardware stack.) There's all you'd ever want to know about 6502 stacks there, plus more, in 19 chapters plus appendices. Indexing into the page-1 hardware stack is introduced in chapter 5, at http://wilsonminesco.com/stacks/stackaddressing.html .

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group