<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Tue, 02 Aug 1994 03:08:02 PDT
From   : Stephen_Youell.wgc-e@...
Subject: Re: Stack Behaviour

Received: by samwise.wgc.rx.xerox.com (4.1/SMI-4.0) id AA03268; Tue, 2 Aug
94 11:07:56 BST

>> The odd thing to me is for JSR I have to -1 before I throw it on the
>> stack and for a RTS I have to +1 after I pull it of the stack.  Can
>> anyone comment on this behaviour????  It is really bugging me!
 
> Again, from memory, the address pushed onto the stack is PC+2, where PC is
> the address of the JSR instruction itself. RTS adds one and then continue.
> I would guess there is a very good reason for this. Maybe the
> implementation of the 6502 was easier this way, because an instruction like
> BRK could use the same logic as JSR. I don't know though. The 6502 isn't
> microcoded, right? Does that mean that there is a completely separate piece
> of logic for each instruction? I think it does. In which case I can't
> really see the point. Someone has just suggested to me that the pipeline
> might explain this.

The reason is that RTS is a single byte instruction. The PC is incremented by
the number of bytes of the previous instruction, in this case 1. Since JSR is
a three byte instruction PC+2 is pushed, in theory PC+3-1 is pushed. This way
the RTS pulls PC+2 back from the stack, the 6502 increments PC by one (the
length of the instruction just executed ie 1) and gets PC+3, cunningly enough
this is the first byte of the instruction after the JSR.


       Steve Youell

 
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>