Solved

Retrieving contents of an register

Posted on 2001-08-02
8
207 Views
Last Modified: 2010-05-19
0
Comment
Question by:ICa
  • 4
  • 3
8 Comments
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:ICa
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
:-) 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
Comment Utility
>> 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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now