Storing the Program Counter

Showing results for 
Search instead for 
Did you mean: 

Storing the Program Counter

NXP Employee
NXP Employee

This message contains an entire topic ported from a separate forum. The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value to you as you search for answers to your questions.


Posted: Fri Jun 3, 2005  4:51 pm


I am using the MC9S12E128 board. How would I store the PC into a memory location? I've been trying to use MOVW or LDD and STD but I've been unsuccessful. It seems like a simple thing to do but I'm failing.



Posted: Fri Jun 3, 2005  6:08 pm


jsr storepc





ldd 0,sp

subd #3

std memory




Posted: Fri Jun 3, 2005  6:12 pm


Or more simply to save the PC after the call



movw 0,sp,memory




Posted: Fri Jun 3, 2005  6:15 pm


Perfect. Thanks.



Posted: Fri Jun 3, 2005  7:38 pm


Or to save the function call:


leax -2,pc


Puts the location of the leax into X. You can should be able to use an expression "Symbol-*-2" to get the address of a symbol in the program.



Posted: Fri Jun 3, 2005  8:01 pm


What about an inline solution:


jsr *



Then D has the value



Posted: Fri Jun 3, 2005  9:43 pm


> What about an inline solution:


> jsr *

> puld


There are two problems with that. First, jsrs are absolute. If your code is using absolute addresses, the assembler already knows the address of everything, so there is no need to determine PC at run time. Do "ldd #label" instead.


Second, "*" points to the start of the instruction, so "jsr *" goes into an infinite loop until it overflows the stack and dies.


You could solve both problems with bsr:


bsr label ; 4 cycles


puld ; 3 cycles


The leax/leay solution is quicker at only 2 cycles, and the built-in addition can point to the thing you actually need the address of rather than the address of the instruction obtaining the PC value.


leax label,pcr ; Note "pcr", not "pc" -- assembler then

; does the relative-address math for us!





Note that it's legal to use ",pc" and ",pcr" in all instructions that support indexed addressing (such as ",x"), so there may be no need to explicitly transfer PC to another register.



Posted: Fri Jun 3, 2005  11:27 pm


Thanks for fixing my idea since I hadn't thought much about it. I do

like the leax idea better, but hadn't seen it before.

Labels (1)
0 Kudos
0 Replies