On Tue, 1 May 2007 20:05:31 +0200, "Jens M. Guessregen / Mailinglists" wrote: >> > I did some sim-debugging and had to find out, that RETLW >> is not restoring PCLATH correctly after returning from the >> computed goto. >> > A normal RETURN does :-( >> > >> > >> You are wrong. The datalist is not so clear but the PCLATH is >> NEVER restored by a return. >> If you want to call a subroutine in another page: >> PAGESEL MySub ;PAGESEL is an internal MPASM >> macro that load the correct value >> call MySub >> PAGESEL CurrentPage ;the choice of CurrentPage is >> important! > >I was not sure, if a RETURN restores the PCLATH, but all the doc's I >found in the web said, it does. >I made a small sample code for a 16F877A, jumping between the pages all >over the 8Kbyte ROM. >I used the ICD2 on real hardware with MPLAB7.50 and also the Software >Simulator in 7.50. > >And ... after the RETURN, the PCLATH was restored to the value, the CALL >was made from ... >But only with a RETURN. RETLW was not restoring, but RETLW is the one to >use for tables ... > Don't confuse the upper bits of PC (which are retored on a retrun) with the PCLATH (PC Latch High) register (which isn't). These are not the same. All of the return instructions pop the return address from the stack in the same way and none of them change PCLATH. >And, the datasheet is absolute clear about the PC stack: > >This is from the 16F877A datasheet: > >2.3.2 STACK >The PIC16F87XA family has an 8-level deep x 13-bit >wide hardware stack. The stack space is not part of >either program or data space and the stack pointer is not >readable or writable. The PC is PUSHed onto the stack >when a CALL instruction is executed, or an interrupt >causes a branch. The stack is POP'ed in the event of a >RETURN, RETLW or a RETFIE instruction execution. >PCLATH is not affected by a PUSH or POP operation. > >That means, that a RETURN, RETLW and RETFIE will cause a POP of PC (= >PCLATH and PCL) back to where the CALL was made from, or? > Sorry, no, it doesn't. Notice the last sentence - "PCLATH is not affected by a PUSH or POP operation." The PCLATH register will retain the same value that it contained when the CALL was executed. Any CALL or GOTO executed following the subroutine return will be made to the code page where the subroutine is located, not to the current code page. That's why you need the 'pagesel $' to set PCLATH back to the current code page. Regards, Bob -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist