Solved

Reading a register

Posted on 2007-11-16
11
451 Views
Last Modified: 2010-04-15
Is there a way in C/C++ to read a register such as the stack pointer(SP) ?
0
Comment
Question by:kipp8r
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 100 total points
ID: 20302328
Not with standard C, no. You'll need some inline assembler for that.
0
 
LVL 7

Assisted Solution

by:UrosVidojevic
UrosVidojevic earned 100 total points
ID: 20302360
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
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20302382
0
 

Author Comment

by:kipp8r
ID: 20302636
I understand know.  Can this be used to read another processes registers?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20302653
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
 

Author Comment

by:kipp8r
ID: 20303244
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
 
LVL 24

Expert Comment

by:fridom
ID: 20303851
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20303881
>> 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
 

Author Comment

by:kipp8r
ID: 20310874
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 191
Problem to save 10 148
Computer slow / BSOD 10 42
Memory going from 12gb to 64gb or 96gb. worth it? 15 135
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 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

23 Experts available now in Live!

Get 1:1 Help Now