Archive for April 15th, 2009


[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

	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.