A refefernce to 6502 by “Remember that in a stack trace, the addresses are return addresses, not call addresses – The Old New Thing”
Posted by jpluimers on 2018/09/11
On x86/x64/ARM/…:
It’s where the function is going to return to, not where it came from.
And:
Bonus chatter: This reminds me of a quirk of the 6502 processor: When it pushed the return address onto the stack, it actually pushed the return address minus one. This is an artifact of the way the 6502 is implemented, but it results in the nice feature that the stack trace gives you the line number of the call instruction.
Of course, this is all hypothetical, because 6502 debuggers didn’t have fancy features like stack traces or line numbers.
Source: [WayBack] Remember that in a stack trace, the addresses are return addresses, not call addresses – The Old New Thing
Which resulted in these comments at [WayBack] CC +mos6502 – Jeroen Wiert Pluimers – Google+:
- mos6502: And don’t forget the crucial difference in PC on 6502 between RTS and RTI!
- Jeroen Wiert Pluimers: +mos6502 I totally forgot about that one. Thanks for reminding me
<<Note that unlike RTS, the return address on the stack is the actual address rather than the address-1.>>
References:
[WayBack] 6502.org: Tutorials and Aids – RTI
RTI retrieves the Processor Status Word (flags) and the Program Counter from the stack in that order (interrupts push the PC first and then the PSW).
Note that unlike RTS, the return address on the stack is the actual address rather than the address-1.
[WayBack] 6502.org: Tutorials and Aids – RTS
RTS pulls the top two bytes off the stack (low byte first) and transfers program control to that address+1. It is used, as expected, to exit a subroutine invoked via JSR which pushed the address-1.
RTS is frequently used to implement a jump table where addresses-1 are pushed onto the stack and accessed via RTS eg. to access the second of four routines.
–jeroen
Leave a Reply