I got this question and wanted to know if my answer was right.

Q.92: Assuming that initially the PIC is addressing Page 0 in program memory; Which of the following snippets of code will jump to program location “PPS2” in Page 1?

goto        PPS2
movlw       b’00010000’
movwf       PCLATH
goto        PPS2
goto        PPS2
None of the above

Doing some research online I understand that PCLatch is a register in bank 1 that works in conjuction with pcl to form a 13 bit register due to the high volume of addresses that need to be viewed.

As a result I concluded (not entirely sure on this) that to jump to PPS2 in page 1 from page 0 I would simply run the goto command therefore solution A.

Am I right on this?

  • \$\begingroup\$ Which PIC are you referring to? \$\endgroup\$ – tcrosley Mar 27 '14 at 0:23
  • \$\begingroup\$ the pic 16f877a. Sorry for not mentioning earlier \$\endgroup\$ – user1819517 Mar 27 '14 at 0:26
  • 1
    \$\begingroup\$ Edit your question with the part number - don't post it in comments. The question must contain all the pertinent details. \$\endgroup\$ – Adam Lawrence Mar 27 '14 at 12:14

Tcrosley's answer is mostly right and addresses the question as asked. The point of the problem is apparently for you to realize that PCLATH<4:3> must have the appopriately state before any GOTO, and that while options B and C did set these bits, they set them to the wrong value.

However, the right answer is not the methods of options B or C to set PCLATH<4:3> to the correct value, but to use PAGESEL. Specifically, the correct code is:

     pagesel    PPS2
     goto       PPS2
| improve this answer | |

The register you are referring to is called PCLATH (not PCLatch), and is accessible from any of the four register files at address 0AH, 8AH, 10AH, and 18AH to make it easier to write to.

The PIC16f877A has 8K of program Flash, but the CALL and GOTO instructions only provide an 11 bit address so branching is limited to a 2K memory page, and four pages (13 bits) are required to map the entire 8K memory.

So the lower 11 bits (10:0) of the 13 bit PC (program counter) come from the CALL or GOTO instruction itself, and the upper 2 bits (12:11) come from bits 4 and 3 of the PCLATH resister. Since the PCLATH bits are not transferred to the PC until the CALL or GOTO instruction is executed, it is safe to set them up before hand. This is usually done using the BSF and BCF instructions.

Why bits 4:3 of PCLATH and not just 1:0? Because bits 4 and 3 "line up" with bits 4 and 3 of the high byte of the PC. This probably makes the internal bus structure simpler, and would also allow for different page sizes, e.g. 1K pages instead of 2K by using bits 4:2.

enter image description here

If you execute a CALL instruction, the entire 13 bit address is saved on the stack, so when you execute a RETURN instruction at the end of the subrotuine, you don't have to worry about the PCLATH bits.

Since the CALL and GOTO instructions only supply 11 bits of address, you must explicitly set up the PCLATH bits if they are different from the page you are in, or you are not sure what they are currently set to, even if you will be executing a CALL or GOTO within the same page.

Your solution c), where you use the BSF and BCF instructions to access PCLATH is almost correct, except it will jump to page 2 since you are setting bit 4 and clearing bit 3. For page 1, you want to clear bit 4 and set bit 3:

goto        PPS2
| improve this answer | |
  • 1
    \$\begingroup\$ "if you are branching or calling a subroutine within the same 2K page you are in, you don't need to modify the PCLATH" this is correct only when you arrived in this page via a CALL, GOTO, or write to PCL. If you arrived in then page via a RETURN the PCLATH might not match the page you are in! IIRC the same holds for the case when you 'straddle' into a page after executing the last instruction of a previous page. \$\endgroup\$ – Wouter van Ooijen Mar 27 '14 at 11:54
  • \$\begingroup\$ Whether you need to set PCLATH before a GOTO has nothing to do with the page your are in, but rather whether you know PCLATH is set appropriately. You may have a convention that PCLATH<4:3> is set to the page execution is currently in, but this is not guaranteed otherwise. \$\endgroup\$ – Olin Lathrop Mar 27 '14 at 12:45
  • \$\begingroup\$ Wouter and Olin, thanks for your comments, I have edited the second to last paragraph of my answer. \$\endgroup\$ – tcrosley Mar 27 '14 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.