Find limit of recursion: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) No edit summary |
Puppydrum64 (talk | contribs) |
||
Line 10: | Line 10: | ||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
The 6502's hardware stack isn't like other processors. First, it is fixed at the $0100-$01FF address range. The stack pointer is an 8-bit value, and assumes that the stack is always between $0100-$01FF. If the stack were to reach $01, and another <code>JSR</code> is executed, the stack would underflow to $FE, and the bottom of the stack would get clobbered. Since each JSR pushes a 2-byte return address onto the stack, the hardware limit of recursion is 128 calls. |
The 6502's hardware stack isn't like other processors. First, it is fixed at the $0100-$01FF address range. The stack pointer is an 8-bit value, and assumes that the stack is always between $0100-$01FF. If the stack were to reach $01, and another <code>JSR</code> is executed, the stack would underflow to $FE, and the bottom of the stack would get clobbered. Since each JSR pushes a 2-byte return address onto the stack, the hardware limit of recursion is 128 calls. |
||
Reading the current stack pointer is unreliable, as there is no requirement that the stack be "aligned" in any way. Unlike the 8086 and Z80, which require all pushes/pops to be exactly two bytes, the 6502's stack will likely contain both 1 byte registers and 2 byte return addresses. It's much easier to use a stack canary. Pick a value that is unlikely to be used in your program. |
|||
<lang 6502asm> |
|||
;beginning of your program |
|||
lda #$BE |
|||
sta $0100 |
|||
lda #$EF |
|||
sta $0101 |
|||
ldx #$ff |
|||
txs ;stack pointer is set to $FF |
|||
lda $0100 ;if this no longer equals $BE the stack has overflowed |
|||
cmp #$BE |
|||
bne StackHasOverflowed</lang> |
|||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |