Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm reading about the 6502 processor's instruction set from the many links at 6502.org, and one tutorial states:

The stack pointer (S) points to a byte on Page 1, that is, to a byte whose address is from 0100 to 01FF, where the last two digits are supplied by S. When a byte is pushed on the stack, it is written at the address in S, and then S is decremented.

The S register is 1 byte, so it obviously holds a value from 00 to FF, but since it decrements upon pushes, when nothing has yet been pushed on, it must start at FF. Does the physical hardware (transistors) in the chip set all the bits in that register to '1' when the chip gets its first breath of power?

I just like to know the low-level details.

share|improve this question
    
What's the over/under on somebody actually knowing this, I wonder...:) –  Shinrai Oct 14 '11 at 17:40
add comment

3 Answers

Every ROM based on 6502 and compatible code I've ever seen initializes the stack pointer (LDX #$FF, TXS) during the RESET routine. You should too.

It's possible later 6502 revisions (i.e. 65C02 in the Apple II) do explicitly initialize it, as well as the 65816 16-bit variants and later.

I would bet significantly that .S is a random value on power up on the original 6502 and the 6510 in the Commodore 64, and probably even the 2A07 in the NES.

Generally any 6502 reset routine also begins with the following, generally as the first two instructions:

SEI ;disable interrupts (set interrupt disable flag)
CLD ;turn decimal mode off
share|improve this answer
1  
The Apple I and II resets do not set the SP, actually. It's probably safer to say most ROMs set it, but it's not actually required in most cases. –  Dennis Munsie Jun 12 '13 at 15:53
    
Interesting ... I guess honestly if you are going to use it as a stack for JSR, PLA, PHA, etc. and never reference it directly as memory with LDA, etc. (and indeed have 256 bytes of RAM there - the Atari 2600 for example only has the 128 bytes from $80-$FF) - it doesn't really matter where it "starts." - unless you want to "clear" it sometime. Bet Woz just wanted to save 3 bytes in his ROM. –  ultrasawblade Jun 13 '13 at 0:30
add comment

If you watch Michael Steil's presentation on reverse engineering the 6502 (http://www.youtube.com/watch?v=fWqBmmPQP40), you'll see at around 42:15 that he explains what happens when RESET occurs. As part of the RESET handling, the 6502 actually does push the PC and P values. The stack pointer starts at 0x100 and after pushing those three bytes, ends up at 0x1fd because of that.

share|improve this answer
add comment

http://forum.6502.org/viewtopic.php?t=468&sid=ccdf15a560f1520a347ba896ae89767f claim that it's not specified.

http://whats.all.this.brouhaha.com/2011/07/07/stack-usage-in-the-apple-1-monitor/ also claims that it's not specified and further that it doesn't matter if you don't care about the exact position of the stack within that page. Wherever it starts it will wrap round, so as long as you don't use more than 256 bytes of stack you're fine.

http://visual6502.org/JSSim/ which is a transistor-level simulation of actual hardware appears to boot up with SP set to FD; you could probably trace the actual registers there and determine if that's deliberate or just a coincidence.

(In that context "monitor" means a low level control program rather than a display)

share|improve this answer
    
re "it doesn't matter if you don't care about the exact position of the stack": if you use the stack to store locals or function arguments, you will care about the exact position of the stack. E.g. reading the second local from the stack to the accumulator (TSX / LDA $00FF,X) will fail if the stack pointer is at 0 because you will read from $00FF instead of $01FF. This can easily happen if the stack starts at too low an address. This issue only applies if you access the stack with absolute indexed addressing, though. –  Snarfblam May 31 '12 at 1:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.