Tell me more ×
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

2 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

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

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.