It is currently Tue Oct 17, 2017 1:14 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Mar 26, 2015 3:35 pm 
Offline

Joined: Mon Jan 23, 2012 11:27 pm
Posts: 141
Isn't the description backwards?

see here: http://wiki.nesdev.com/w/index.php/Stac ... _underflow

Quote:
the program is either attempting to push more data on to the stack when S is already at $FF, or attempting to pull data off of the stack when S is already at $00.


based on reading the page to that point, pushing a value onto the stack at $ff is basically the first thing you do isn't it? And you'd have to add values until S gets to $00 before you run out of room to add?


Top
 Profile  
 
PostPosted: Thu Mar 26, 2015 5:02 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10046
Location: Rio de Janeiro - Brazil
Looks reversed to me. Either way, I find that explanation about overflows/underflows very poor. It assumes you're going to use all 256 bytes for the stack, which isn't always the case. Most programs need way less than that, and could benefit from stealing a few bytes from the stack, meaning it won't always have $00 and $ff as its boundaries.

Also, the Stack Pointer wraping around doesn't necessarily mean an overflow or underflow. If I initialize S with a value other than $FF, I can still use all 256 bytes of it just fine, with S crossing the $FF-$00 boundary back and forth.


Top
 Profile  
 
PostPosted: Thu Mar 26, 2015 6:00 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19087
Location: NE Indiana, USA (NTSC)
If you have ideas for improvement, you could always go in and edit it.


Top
 Profile  
 
PostPosted: Thu Mar 26, 2015 10:38 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5710
Location: Canada
I edited it.


Top
 Profile  
 
PostPosted: Fri Mar 27, 2015 7:14 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10046
Location: Rio de Janeiro - Brazil
rainwarrior wrote:
I edited it.

It's much better. But I think it sounds like the 2nd and 3rd paragraphs are contradicting the first a bit. Personally, I'd give the strict definition first, and then provide an example of the common case that is initializing S to $FF and using all 256 bytes at $0100-$01FF for the stack, and finally mention the other possible cases: stack smaller than 256 bytes and/or S initialized to values other than $FF (or left uninitialized). Something like this:

tokumaru wrote:
Strictly speaking, a stack overflow is when the program attempts to push more data than the stack can hold, and an underflow is when it attempts to pull data off the stack when it's empty. Under normal circumstances, that would mean pushing when S is already at $00 (overflow) or pulling when S is already $FF (underflow). Usually this implies a PHA vs. PLA mismatch of some sort.

However, since programmers can choose to use less than 256 bytes for the stack in order to free some of the RAM at $0100-$01FF for other uses, the boundaries of the stack won't necessarily always be at $FF and $00.

There's also the fact that S can be initialized to values other than $FF, or even be left uninitialized (since the stack pointer simply wraps around at the end of the range), and that that could also cause the boundaries to be at positions other than $FF and $00.

I prefer not to edit the wiki directly, since I'm not a native English speaker and that makes me a little insecure about the correctness and clarity of what I write.


Top
 Profile  
 
PostPosted: Fri Mar 27, 2015 7:58 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19087
Location: NE Indiana, USA (NTSC)
I revised the section based on your suggestions. Any more?


Top
 Profile  
 
PostPosted: Fri Mar 27, 2015 9:28 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5710
Location: Canada
I had a few suggestions after reading it. They're added.


Top
 Profile  
 
PostPosted: Fri Mar 27, 2015 12:52 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10046
Location: Rio de Janeiro - Brazil
I hadn't even realized that pushing a byte when S is at $00 doesn't cause an overflow, only the next push does. Nice catch.


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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