how can i obtain the current values of registers like accumulator,B,stack pointer,PSW,DPTR,etc in C?? i am currently working on a trainer/development kit for 8051..One of the commands to be executed is R<CR> Once this command is executed,the values of all the registers like A,B,SP,PSW,DPH,DPL,R0,R1,R7 should be displayed on screen...how do i retrieve these register values in C?
The right sentence is:
"the 'C' certainly can impact the register values"
And I think it's highly likely (to the point of being almost certain) that it will imact at least the accumulator, SP, and PC.
So it is still well worth considering doing this in Assembler - where you can be absolutely certain about what registers are affected - and how.
"I think it's highly likely"
true. and that means that it may not impact some registers for some operations. so your original sentence of "...will impact" is simply wrong.
I think we will find that in the end, something like what the OP wanted to do may not be doable for all cases / registers.
and this isn't unique to 8051.
"your original sentence of "...will impact" is simply wrong"
That's why I actually said,
"... (almost certainly) will impact..."
"(almost certainly) will impact..."
ok. take a typically statement of "x=REGISTER_Y;", for the 40+ registers on a typical 8051, tell us how many of them will CERTAINLY be impacted by that read statement and how many of them will NOT CERTAINLY be impacted by that read statement.
since these registers are in the memory,cant i just keep a pointer to these locations and read the values from those locations??
The specific registers you mentioned were:
"A,B,SP,PSW,DPH,DPL,R0,R1,R7"
They are not all together in one single, contiguous block of memory; They aren'y even in the same 8051 address spaces.
- so you can't have just 1 pointer.
It sounds like what you're actually trying to do is to make a Debug Monitor?
Rather than re-invent the wheel, why not use one of the existing open-source solutions?
eg, www.pjrc.com/.../paulmon2.html
Or, at least, study it to see how this stuff is done...
well i meant a separate pointer for each register,but i guess since these SFRs are directly accessible, something like x=ACC should work..but this doesn't work for PC..how can i retrieve values of PCH and PCL?
But, again, why re-invent the wheel?
Why not, at least, look at existing solution to see how they do this?
But accessing PC - what PC? Your program will get the PC value of the code that retrieves the PC, since that is where the processor will always be when you try to read out PC.
That was one reason why I said you should consider handling all this in an ISR. Then the ISR code can check what return address the ISR will use - that is the PC of the program that was running when the ISR picked up the serial character.
but this doesn't work for PC..how can i retrieve values of PCH and PCL?
because PC is one of those registers that ARE impacted by the current code being executed.
the likely solution is to push the PC, execute your code to pop the PC, and make adjustments to it there.
something like x=ACC should work.
Simply by expressing that belief, you've just demonstrated how haven't got one bit closer to understanding what the real problem is than when you started this thread.
No, that will not "work", for any meaningful interpretation of that word. Sure, you'll get what was in the accumulator as you executed that instruction --- but that will rather certainly not be what you actually wanted to get.
You're on the wrong path, and people have been telling you that --- yet instead of turning back or at least slowing down, you're accelerating. Do you really think that's a sensible use of anyone's time?
cant i just keep a pointer to these locations and read the values from those locations
No, you can't. And if you had anywhere near the skills it takes to have a remote chance at doing what you're trying to do, you wouldn't need to be told this very basic fact about the '51 architecture.
"Do you really think that's a sensible use of anyone's time?"
hi, smartie, rather than wasting everyone's time on how this will not work, why don't you put down on a piece of paper an approach that will work?
be constructive and show us how smart you really are.
It's already done and known as a Debug Monitor.
Most commercial paid-for and open-source are available.
hi, smartie, rather than commenting on peoples replies be constructive and show us how smart you really are.