Is there a way to read the value of the Program Counter (PC)?
Submitted By: Craig Steiner FAQ Last Modified: 07/10/06
- The Program Counter is an internal value that the microcontroller uses to keep track of the address of the instruction that it is executing in memory. Another way to look at it is that the PC will always contain the address of the current instruction. In almost all cases, the address of any given instruction should be known since you have the source code.
Also, in most assemblers the $ symbol refers to the address of the current instruction. For example:
LJMP $
This instruction is the same as:
THISOP: LJMP THISOP
Both constructs will create a program that loops indefinitely. In this manner, the "$" symbol can be used to reference the current PC value. Again, this doesn't apply to all assemblers, but certainly most 8052 assemblers support the use of the $ symbol to reference the current instruction address.
If you truly must determine the PC and, for some reason, you truly don't know the address of the instruction and can't use the "$" symbol, Peter Dannegger offered the following solution: Call the GET_PC function (below). When it returns, DPTR (DPH and DPL) will hold the value of the address of the instruction following the GET_PC call.
GET_PC: POP DPH POP DPL CLR A JMP @A+DPTR
Add Information to this FAQ: If you have additional information or alternative solutions to this question, you may add to this FAQ by clicking here.