Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Dear sir;

I want to write a program in c++ or c# to calculate the contents of the working registers.
Can you help me in this issue? how can I write it?

Thanks in advance
Dalia
Posted
Comments
Sergey Alexandrovich Kryukov 4-Mar-12 2:14am    
What do you mean by registers? CPU registers? First, this is not easy (I did it), and C# won't help, and C++ -- only in its inline assembly.
--SA
Member 8697827 4-Mar-12 2:19am    
yes I mean the cpu registers ex DR AC PC AR IR TR
you say that you did it so can you show me how
Sergey Alexandrovich Kryukov 4-Mar-12 3:37am    
I must admit I'm not familiar with these registers. What's the CPU? I did it with 80x86 (yes, many years ago), could use x386 and compatible. I know RAX, RBX, RCX, RSI, RDI, RSP, RBP, RIP. I tell you, it would take considerable anount of time. The problem is that you need to use registers to store register data, and this is the problem, because you modify the registers while saving. This is possible but needs thinking...

--SA
Member 8697827 6-Mar-12 1:47am    
I mean mano computer architecture
Richard MacCutchan 6-Mar-12 3:10am    
If you had done this first, you might have an answer by now.

It sounds like you need to use assembler language to achieve this. You can do it by writing a stand-alone program using only assembler code (quite a challenge), or use the inline assembler in the C++ compiler. See here[^] for some useful guidelines.
 
Share this answer
 
Comments
Keith Barrow 4-Mar-12 6:22am    
5'd.
Richard MacCutchan 4-Mar-12 6:26am    
Cheers, or should I say ha way, pet :)
Member 8697827 6-Mar-12 1:47am    
thank you
As is often suggested you will need to do a little work using Google.
Registers are CPU specific and will you will need to employ CPU instructions directly to read their values. This will require assembly language functions or inlining ASM.

You have not stated CPU or OS so you seem not to have done too much homework yet.

From here I got this example to read Intel EDX:

http://sim0n.wordpress.com/2009/04/15/c-getting-register-values/[^]

C++
#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 ;
}

Work on understanding this example and you will be in business.

Repeat this process for all the CPU registers you wish to examine.

If it is not CPU registers you need but device registers (eg UART) then the same technique can be used.
 
Share this answer
 
v4
Comments
Richard MacCutchan 5-Mar-12 5:06am    
Added <pre> tags to format code properly.
Member 8697827 6-Mar-12 1:45am    
Thank you very much for your help but I have a question is itt suites mano computer architecture.
[no name] 6-Mar-12 1:55am    
I have answered your question. How about you do some work now and then perhaps come up with a more specific question related to a real world problem.
Member 8697827 7-Mar-12 8:37am    
please edx,_asm,std stands for what
[no name] 7-Mar-12 8:51am    
Aha - lets call that your assignment.
If you cannot work that out for yourself then you will not understand anything else I tell you.
The problem is not easy to solve and debug, but this is quite doable. The simplest basic idea is to push registers to stack. Then you can modify any registers used to moving data from stack to some memory, for example, representing the location of output parameters in the stack frame or some structure passed by its address. Then you restore the stack or just use it on return. Usually, you don't need to save instruction pointer, as you can use the address of some function of return address.

—SA
 
Share this answer
 
Comments
Member 8697827 4-Mar-12 5:16am    
of course thank you vey much for your help but how to write the code i want to load and store registers in memory but how to write the code in c++?
Nelek 4-Mar-12 6:22am    
Well, after getting the idea and the how-to, you should try a bit on your own. I don't think anyone here is going to give you the code of the final solution.
Sergey Alexandrovich Kryukov 4-Mar-12 13:30pm    
You see, the problem is not so simple and needs fair understanding of inline assembly and CPU. Any help could be useful. I have done tricky things with assembly (actually implemented early multithreading system implementation from scratch, directly manipulating the thread context with those registers, and more), even to me it would take considerable amount of time just to refresh my skills. Maybe some hours.

You are right though. OP should get to work.
--SA
Sergey Alexandrovich Kryukov 4-Mar-12 13:27pm    
You can use inline assembly, which is actually much more convenient compared with traditional assembly language, as you are allowed to use symbols like C++ functions and objects inside assembly code. Please find the inline assembly syntax for your compiler. If you never tried it, you will have to learn it.
You also need to understand at least basic CPU commands and addressing modes and principles.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900