15
Apr
09

[C++] Getting Register Values

I was asked today about how to get the values from the processor registers today, and here is the very simple answer.
It uses some inline asm to move the value of EDX into a variable.
There are 2 functions: setEDX and getEDX; which do exactly what they say.

#include <iostream>

void setEDX(int value) {
	//Moves 'value' into edx
	_asm mov edx, value;
}

int getEDX() {
	int value;
	//Moves edx into 'value'
	_asm mov value, edx;
	return value;
}

int main()
{
	int edxValue = getEDX() ; //Get Register
	std::cout << "EDX: " << edxValue << "\n"; // Display

	std::cout << "Set EDX Value: ";
	int value;
	std::cin >> value; //Get input
	setEDX(value); //Set register

	edxValue = getEDX() ; //Get Register
	std::cout << "EDX: " << edxValue << "\n"; //Display

	system("pause");
	return 1 ;
}

The programs output is essentially just this:
EDX: 1
Set EDX Value: 54321
EDX: 54321
Press any key to continue . . .

Showing the original value, setting the value, and displaying the set value.

Advertisements

2 Responses to “[C++] Getting Register Values”


  1. April 16, 2009 at 7:52 am

    I think it’s worth mentioning that this is compiler/platform specific. For example, the inline assembly syntax for GNU’s C/C++ compileron (GCC, g++) is completely different. And of course the assembly language you can use is different for different machines. I’m only guessing that this example of yours is supposed to compile with Microsoft’s C++ compiler on an x86 machine.

  2. 2 ih4x
    April 16, 2009 at 10:45 am

    Yeah, that is true, but it is clearly going to be platform specific due to the differences between the assembly. It also uses System(“pause”) which again, is going to be platform specific.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: