Solved

Retrieving contents of an register

Posted on 2001-08-02
8
243 Views
Last Modified: 2010-05-19
0
Comment
Question by:ICa
[X]
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
  • 4
  • 3
8 Comments
 
LVL 22

Expert Comment

by:nietod
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;
}
0
 
LVL 1

Author Comment

by:ICa
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).
0
 
LVL 22

Expert Comment

by:nietod
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ICa
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.
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 1

Author Comment

by:ICa
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.
0
 
LVL 22

Accepted Solution

by:
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.
0
 
LVL 11

Expert Comment

by:griessh
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

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 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