• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

Reading a register

Is there a way in C/C++ to read a register such as the stack pointer(SP) ?
0
kipp8r
Asked:
kipp8r
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
Infinity08Commented:
Not with standard C, no. You'll need some inline assembler for that.
0
 
UrosVidojevicCommented:
Example for VS C++:


#include <iostream>
using namespace std;
 
typedef unsigned short REG_VAL;
 
int main() {
	 REG_VAL reg;
 
	_asm {
		mov reg, sp
	};
 
	cout << "SP register value: " << reg << endl;
 
	__asm mov reg, ax;
	cout << "AX register value: " << reg << endl;
	
	__asm mov reg, bx;
	cout << "BX register value: " << reg << endl;
 
	__asm mov reg, cx;
	cout << "CX register value: " << reg << endl;
	
	__asm mov reg, dx;
	cout << "DX register value: " << reg << endl;
 
	return 0;
}

Open in new window

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!

 
kipp8rAuthor Commented:
I understand know.  Can this be used to read another processes registers?
0
 
UrosVidojevicCommented:
No. These values are valid only for current ("RUNNING") process.

Information (register values) for other processes are stored in their PCB structures and are written in the registers when they get processor.
0
 
kipp8rAuthor Commented:
Is there anyway to read the registers of another process.  I've noticed windows has a GetWindowThreadProcessId() function.  Basically I'm trying to output the values of the registers when the SP gets to a specific address.  So I can get some debug information on my software.
0
 
fridomCommented:
Of course there is all debugger do make that. So just search for debugger development and see what you can get from it.

Regards
Friedrich
0
 
Infinity08Commented:
>> So I can get some debug information on my software.

Maybe it's better to just run your program in a debugger, and get the information that way ?
0
 
kipp8rAuthor Commented:
Debugging with another program would be too easy...lol...besides I want to log the values.  I found this though.  The win32 debug API.


            if (WaitForDebugEvent( &devent , 0))
                  {
                  switch(devent.dwDebugEventCode)
                        {
                         case CREATE_PROCESS_DEBUG_EVENT:
                                    h = ::OpenThread(THREAD_ALL_ACCESS, false, devent.dwThreadId);
                        // your handler here
                         break;            
                         case EXIT_PROCESS_DEBUG_EVENT:
                        // your handler here
                        break;
                         case EXCEPTION_DEBUG_EVENT:                   
                        // your handler here
                        break;

                        }      
                  
                  ContinueDebugEvent(devent.dwProcessId , devent.dwThreadId , DBG_CONTINUE);
      
                  }

            else
                  {
                     // other operations
                        
                  }

            }
            ::GetThreadContext(h,&rx);
0

Featured Post

Industry Leaders: 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!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now