Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Retrieving contents of an register

Posted on 2001-08-02
Last Modified: 2010-05-19
Question by:ICa
  • 4
  • 3
LVL 22

Expert Comment

ID: 6344375
Can you post your question as a comment?

Anyways, if you want to obtain the contents of a CPU register, you can do this usign inline assembly.  The exact details will depend on the compiler you are usign as well as the platform, but an example for VC wuld be like

int Somefunction()
    int RagEAX;
    __asm mov RegEAX,EAX
    return RegEAX;

Author Comment

ID: 6346006
Thank you nietod, I tried your idea, but the function:

long getEAX()
   long regEAX;
   __asm {mov regEAX,EAX}
   return regEAX;

still keeps returning -858993460 what is not the value of EAX (or any other register).
LVL 22

Expert Comment

ID: 6346053
How do you know the contents of the EAX register?

Not that the code there will probably change the contents of the EAX register, so if you have EAX set to something at one point, and then call this procedure, you will undoubtedly change EAX!

Did you look at the code in the debugger?  I bet that at teh time at which the _asm statement executes that EAX is that number.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 6351365
Well, since the >Debug Window / Registers< displays other values, and the returned value is the same for all registers plus it is the bottom limit of "long" I figure it is not what I am looking for. In fact I want to be able to query the contents exactly like the "Registers" Debug window for an visual Assembler-Simulator I am working on.
LVL 22

Expert Comment

ID: 6351556
You need to post your code and you need to explain how you are determining what you think the register should be.

Rember that the registers change constantly.  The code

  __asm mov RegEAX,EAX

returns the value of EAX at the time of that statement.  However its very likely that the immeidately previous statement changes the value of EAX.  (Almost everything changes the value of EAX!)

If you try

  int RagEAX;
__asm mov EAX,12345
__asm mov RegEAX,EAX

I bet the function returns 12345.   That does not indicate that the assembly I showed you before was wrong.  That indicates that your expectation of what should be in EAX is wrong.  

Note that the function itself, SomeFunction(), probalby changes EAX.  You can't expect some value to be in EAX and then call SomeFunction to get it.  it doesn't work.  SomeFunction will return the changed value of EAX.  You need to use the assembly code at the time at which EAX has the value you want.

>> Debug window for an visual
>> Assembler-Simulator I am working on.
This is the wrong approach for that anyways.  You can't use the CHIP to simulate itself.  Its like making two identical sized boxes.  One of which will be used to store the other.  It doesn't work they are the same size.  One of many problems will be how at any particualr time will you be able to get the value of all the registers?  your code to extract the values is going to end up changing the values (of at least some) registers.
Instead you need to write a program that simulates the behavior of the CPU.  If this is for a simulation, then it needs to be a simulation.  You need to have variables that represent the registers on the CPU.  You need to have a stack that represents th program's stack.  You need to write code that looks at instructions and figures out what actions they need to take etc etc etc.

Author Comment

ID: 6351888
:-) OK I got the clue, but lets reduce my question to: Why can the debug window get the values (registers, stack, memory)? ... if Microsoft can create a code that gets those values why cant I? I know that the contents of the registers change and it is not realy important that I get the exact values at a very particular time. All I want is a more or less realistic value and if it is possible it would be nice to have one that really is in that register at the time the function is called.
I found a command "flushreg" that might work.

And... yes my simulation has all those features and is actually a compiler, compiling assembler code to a virtual machine. That register thing is just a little extra I thought might be cool.

Thank u 4 your efforts.
LVL 22

Accepted Solution

nietod earned 50 total points
ID: 6352095
>> Why can the debug window get the
>> values (registers, stack, memory)?
There are a variety of ways this is done.

traditionally the way this was done, was then a debugger breakpoint (int 3) was reached, execution trapped immediately to an assembly interface procedure that is part of the debugger.  This procedure stores the registers current contents usually in global variables.  by using global variables it does not need to corrupt the registers in order to store their contents. In addition, since htis interface procedure is written in assembly, it does not need toset up a stack frame or perform other actions that would also corrupt the contents of the registers.  So in this way, allmost all the registers can safely be stored without corrupting them.  The exceptions to this are the stack pointer, the instruction pointer, and the flags registers, however, saved versions of these are placed on the stack as part of the interrupt processes, so the interface procedure obtians their values from the stack, not the current registers values.

If you know assembly well enough You could duplicate most of this yourself by using inline assembly and a "naked" function call.  In Vc a "naked" function call will not set up a stack frame, so at the start of the procedure's code the registers are exactly how they were when the caller made the call.  You still have to make sure the caller doesn't do anythign that corrupts the registers between the time they are set to some values of interest and the time the function is called.

However in more modern times with multi-threading  multi-processors, virtual memory, and protected mode this description really is not accurate as to what the debugger probalby really does.  These days debug interrupts can go through traps and call gates that may cause the entire CPU state to be saved to the stack (all the registers and flags etc).  Then the CPU might switch to a completely new stack and new thread run the debugger under this new thread with its new stack and new memory address space etc.  The debuger can eventually get hold of the stack where the debugged process's CPU state is stored and obtaint he information from there.
LVL 11

Expert Comment

ID: 6842568
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "nietod" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 114
Would like to move button in a function 3 79
How do i run a c++ file? 15 46
Outlook 13 41
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question