under/overflow descriptions reversed on stack page?

Discussion about the site's wiki, including bugs/issues encountered

Moderator: Moderators

Post Reply
FrankWDoom
Posts: 172
Joined: Mon Jan 23, 2012 11:27 pm

under/overflow descriptions reversed on stack page?

Post by FrankWDoom » Thu Mar 26, 2015 3:35 pm

Isn't the description backwards?

see here: http://wiki.nesdev.com/w/index.php/Stac ... _underflow
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?

User avatar
tokumaru
Posts: 11466
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: under/overflow descriptions reversed on stack page?

Post by tokumaru » Thu Mar 26, 2015 5:02 pm

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.

tepples
Posts: 21754
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: under/overflow descriptions reversed on stack page?

Post by tepples » Thu Mar 26, 2015 6:00 pm

If you have ideas for improvement, you could always go in and edit it.

User avatar
rainwarrior
Posts: 7677
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: under/overflow descriptions reversed on stack page?

Post by rainwarrior » Thu Mar 26, 2015 10:38 pm

I edited it.

User avatar
tokumaru
Posts: 11466
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: under/overflow descriptions reversed on stack page?

Post by tokumaru » Fri Mar 27, 2015 7:14 am

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.

tepples
Posts: 21754
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: under/overflow descriptions reversed on stack page?

Post by tepples » Fri Mar 27, 2015 7:58 am

I revised the section based on your suggestions. Any more?

User avatar
rainwarrior
Posts: 7677
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: under/overflow descriptions reversed on stack page?

Post by rainwarrior » Fri Mar 27, 2015 9:28 am

I had a few suggestions after reading it. They're added.

User avatar
tokumaru
Posts: 11466
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: under/overflow descriptions reversed on stack page?

Post by tokumaru » Fri Mar 27, 2015 12:52 pm

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.

Post Reply