nesdev.com
http://forums.nesdev.com/

under/overflow descriptions reversed on stack page?
http://forums.nesdev.com/viewtopic.php?f=16&t=12558
Page 1 of 1

Author:  FrankWDoom [ Thu Mar 26, 2015 3:35 pm ]
Post subject:  under/overflow descriptions reversed on stack page?

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?

Author:  tokumaru [ Thu Mar 26, 2015 5:02 pm ]
Post subject:  Re: under/overflow descriptions reversed on stack page?

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.

Author:  tepples [ Thu Mar 26, 2015 6:00 pm ]
Post subject:  Re: under/overflow descriptions reversed on stack page?

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

Author:  rainwarrior [ Thu Mar 26, 2015 10:38 pm ]
Post subject:  Re: under/overflow descriptions reversed on stack page?

I edited it.

Author:  tokumaru [ Fri Mar 27, 2015 7:14 am ]
Post subject:  Re: under/overflow descriptions reversed on stack page?

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.

Author:  tepples [ Fri Mar 27, 2015 7:58 am ]
Post subject:  Re: under/overflow descriptions reversed on stack page?

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

Author:  rainwarrior [ Fri Mar 27, 2015 9:28 am ]
Post subject:  Re: under/overflow descriptions reversed on stack page?

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

Author:  tokumaru [ Fri Mar 27, 2015 12:52 pm ]
Post subject:  Re: under/overflow descriptions reversed on stack page?

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.

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/