473,473 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Acessing registers with C

Hiya,

I would like to be able to read the values in the registers of the x86
type processor, using C.

I could do this in ASM but I really want to use C for my project, I
havent done anything like this in C before and would be interested to
learn how to.

Although the operating system doesnt really relate to pure C, and I
doubt there will be any difference between *nix and windows products,
I think i should make it clear that I would like to find out the space
that is being used by a certain program, in both *nix and windows. I
no that this is the CX register just not sure how to apply it to a
certain program.

Thanks

Greg
Nov 14 '05 #1
17 4982
gr**********@hotmail.com (jeff) wrote:
I would like to be able to read the values in the registers of the x86
type processor, using C.
Not possible in a language that was designed to be portable to other
processors than a single one which didn't even exist when the language
was born.
It may be possible to do this using an extension to the language
provided by your compiler, but this would be off-topic in comp.lang.c,
which deals with the language itself and not with the various mutually
incompatible add-ons. Then again, it may not be possible at all using
your compiler.
You should ask this question in a specialised newsgroup, one that deals
with your implementation.
Although the operating system doesnt really relate to pure C, and I
doubt there will be any difference between *nix and windows products,


I wouldn't be so sure.

Richard
Nov 14 '05 #2

"jeff" <gr**********@hotmail.com> wrote in message
news:cd**************************@posting.google.c om...
Hiya,

I would like to be able to read the values in the registers of the x86
type processor, using C.

I could do this in ASM but I really want to use C for my project, I
havent done anything like this in C before and would be interested to
learn how to.
A good optimizing compiler won't tell you meaningful things at the source
level about what's in the registers at all times. So you're best bet is to
actually code an assembler routine.
Although the operating system doesnt really relate to pure C, and I
How so? Most operating systems are written in C nowadays.
doubt there will be any difference between *nix and windows products,
I think i should make it clear that I would like to find out the space
that is being used by a certain program, in both *nix and windows. I
no that this is the CX register just not sure how to apply it to a
certain program.


How to apply the CX register to your program? That doesn't make any sense
whatsoever.

Tom
Nov 14 '05 #3
Tom St Denis wrote:
"jeff" <gr**********@hotmail.com> wrote in message

[...]
I doubt there will be any difference between *nix and windows products,
No difference between *nix and Windows???

Well, this is true as far as "pure ANSI C" is concerned, but I doubt
that "how do I access the CX register" counts as "pure ANSI C".
I think i should make it clear that I would like to find out the space
that is being used by a certain program, in both *nix and windows. I
no that this is the CX register just not sure how to apply it to a
certain program.


How to apply the CX register to your program? That doesn't make any sense
whatsoever.


I think he's referring to the fact that tiny-model MS-DOS programs
(ie: *.COM) start with the CX register containing the size of the
executable image. (At least, that's what I recall from years ago.)

Now, how this applies to *nix and Windows, let alone C in general,
I don't know. (Well, I do. The answer is "it doesn't".)

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+

Nov 14 '05 #4
Hiya
How so? Most operating systems are written in C nowadays.
I was trying to Pre-empt a reponse saying that i was offtopic.
How to apply the CX register to your program? That doesn't make any sense
whatsoever.


If you go to command and then run debug and debug something.com (or
exe), then you can use the -r command to get the register values, the
CX value here relates to the program size. Thats what I want to get
at.

Im not sure whether its just related to C in the sense that is
newgroup use it but theres the ASM command, I could use that get the
value of CX but I wouldnt know how to then use it in the rest of the
program,

cheers

greg
Nov 14 '05 #5
On 13 Feb 2004 02:51:32 -0800, gr**********@hotmail.com (jeff) wrote:
Although the operating system doesnt really relate to pure C, and I
doubt there will be any difference between *nix and windows products,
I think i should make it clear that I would like to find out the space
that is being used by a certain program, in both *nix and windows. I
no that this is the CX register just not sure how to apply it to a
certain program.


I'm curious as to how you will apply it to any program on a machine
which doesn't have a CX register.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #6
On 13 Feb 2004 10:09:18 -0800, gr**********@hotmail.com (jeff) wrote:
Hiya
How so? Most operating systems are written in C nowadays.


I was trying to Pre-empt a reponse saying that i was offtopic.

It didn't help. You're still off-topic.

Find a newsgroup which deals with the implementation you're interested
in, whether that be Windows or *nix. Don't expect the answer to be the
same for both. Don't even expect the answer to be the same for
different *nix implementations.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #7
Alan Balmer <al******@att.net> scribbled the following:
On 13 Feb 2004 10:09:18 -0800, gr**********@hotmail.com (jeff) wrote:
Hiya
How so? Most operating systems are written in C nowadays.
I was trying to Pre-empt a reponse saying that i was offtopic.

It didn't help. You're still off-topic.

Find a newsgroup which deals with the implementation you're interested
in, whether that be Windows or *nix. Don't expect the answer to be the
same for both. Don't even expect the answer to be the same for
different *nix implementations.


....some of which are for architectures that don't have "CX registers"
in the first place. Sun Microsystems, for instance, makes millions
selling those.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Hasta la Vista, Abie!"
- Bart Simpson
Nov 14 '05 #8
In article <news:cd**************************@posting.google. com>
jeff <gr**********@hotmail.com> writes:
I would like to be able to read the values in the [ordinary cpu]
registers of the x86 type processor, using C.


While many or even most "real world" C compilers provide a way to
do this, there is no Standard C method. In practice, each C compiler
uses a different syntax from the next. If I gave you a gcc-specific
answer but you are using a Microsoft compiler, it would do you no
good. Thus, to get a *useful* answer, you need some other newsgroup,
where people who use your particular compiler on your particular
system can answer your question, and correct those who give wrong
answers.

This same sort of reasoning -- i.e., that you will be much more
likely to get a correct and useful answer elsewhere -- applies to
questions about Unix-like systems (including Linux) and Windows
systems. In particular, I believe they have quite different
methods for determining load-image and runtime memory sizes.
(Consider, e.g., ELF-style shared libraries vs Windows-style DLLs.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #9
jeff wrote:
Hiya

How so? Most operating systems are written in C nowadays.

I was trying to Pre-empt a reponse saying that i was offtopic.

How to apply the CX register to your program? That doesn't make any sense
whatsoever.

If you go to command and then run debug and debug something.com (or
exe), then you can use the -r command to get the register values, the
CX value here relates to the program size. Thats what I want to get
at.

[snip]

1. I've searched and searched the ARM Reference Guide and the
ARM User Guide and I can't find the CX register. Can you help?

2. The embedded operating system I'm using doesn't have the "debug"
command. What can I use?

cheers

greg

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #10

"jeff" <gr**********@hotmail.com> wrote in message

I would like to be able to read the values in the registers of the x86
type processor, using C.

Why do you want to do this? If you are interfacing C and assembly the
convention is to write the assembly function so that it is C callable.

As you probably know, there is no portable way of accessing registers in C,
however many compilers provide extensions that allow you to do so.
Nov 14 '05 #11

On Fri, 13 Feb 2004, jeff wrote:
[I think TSD wrote:]
How to apply the CX register to your program? That doesn't make any sense
whatsoever.


If you go to command and then run debug and debug something.com (or
exe), then you can use the -r command to get the register values, the
CX value here relates to the program size. Thats what I want to get
at.


On an MS-DOS system, then, you may write the following program. But
it only works if you have 'debug' or a 'debug' clone, and it only
works for small programs (because CX can store only the low 16 bits of
the program size, as far as 'debug' is concerned).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char *ProgramToExamine = "myprog.exe";

int main(void)
{
char script[FILENAME_MAX];
char output[FILENAME_MAX];
char buffer[3*FILENAME_MAX+100];
unsigned int size = 0;
FILE *fp;
char *p;

/* Create a script file for 'debug' */
fp = fopen(tmpnam(script), "w");
fputs("r cx\n\nq\n\n", fp);
fclose(fp);

/* Call 'debug' */
sprintf(buffer, "debug %s < %s > %s",
ProgramToExamine, script, tmpnam(output));

system(buffer);
remove(script);

/* Retrieve second line of 'debug' output */
fp = fopen(output, "r");
fgets(buffer, sizeof buffer, fp);
p = fgets(buffer, sizeof buffer, fp);
fclose(fp);
remove(output);

/* Process it */
if (p != NULL) {
/* Match line of the form 'CX xxxx' returned under WinXP */
if (sscanf(buffer, "CX %x", &size) != 1) {
puts("Failure!");
exit(EXIT_FAILURE);
}
}
else {
puts("Couldn't open file!\n");
exit(EXIT_FAILURE);
}

printf("Size of program %s is %u (%x) bytes.\n",
ProgramToExamine, size, size);
return 0;
}

This program is standard C, but it uses the implementation-dependent
'system' call to try to execute 'debug'. How 'debug' works is not
topical in this newsgroup.
Im not sure whether its just related to C in the sense that is
newgroup use it but theres the ASM command,
That collection of words does not make sense. But you do seem
to be under the impression that C has an "ASM command," which is
wrong in at least two ways: C does not have "commands," and C does
not have any keyword "ASM" (or "asm", or "Asm", or any variation
thereon).
I could use that get the value of CX but I wouldnt know how to
then use it in the rest of the program,


I've run some tests, and AFAICT the value of CX upon program
entry has nothing to do with the program's size in bytes. And of
course the value of CX in the middle of a program has nothing to
do with anything -- the program is *using* CX to *compute stuff* at
that point!
Again, 8086 stuff is off-topic in comp.lang.c, but I just wanted
to point out that (1) C people can give you really bad x86 advice;
and (2) in this case, there's no good advice to be given except
"You can't do that that way; ask an MS-DOS group."

-Arthur
Nov 14 '05 #12
On Fri, 13 Feb 2004 18:01:10 -0500 (EST), "Arthur J. O'Dwyer"
<aj*@nospam.andrew.cmu.edu> wrote:
Again, 8086 stuff is off-topic in comp.lang.c, but I just wanted
to point out that (1) C people can give you really bad x86 advice;
The OP should realize that this post may be an example of that, since
there is unlikely to be any peer review here.
and (2) in this case, there's no good advice to be given except
"You can't do that that way; ask an MS-DOS group."


Which advice was already given, albeit more comprehensively (the OP
did mention Unix.)

What was your point in posting this off-topic reply?

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #13
On Fri, 13 Feb 2004 18:01:10 -0500 (EST), in comp.lang.c , "Arthur J.
O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
C does
not have any keyword "ASM" (or "asm", or "Asm", or any variation
thereon).


actaually, its not /entirely/ informative to sat that:
J.5 Common extensions
J.5.10 The asm keyword
1 The asm keyword may be used to insert assembly language directly into the
translator output (6.8). The most common implementation is via a statement
of the form:
asm ( character-string-literal );

Its worth bearing in mind though that J is informative.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #14
I figured out a way to do it now anyway
I'm curious as to how you will apply it to any program on a machine
which doesn't have a CX register.


I stated on my first post that this was for the x86 processor, sun
microsystems use the UltraSparc type processor, this is a complelty
different archietecture, so's PowerPC, and ARM. But i never said that
I wanted to do this on any of them styles archietecture. Just x86.

greg
Nov 14 '05 #15

On Fri, 13 Feb 2004, Alan Balmer wrote:

<aj*@nospam.andrew.cmu.edu> wrote:
Again, 8086 stuff is off-topic in comp.lang.c, but I just wanted
to point out that (1) C people can give you really bad x86 advice;
The OP should realize that this post may be an example of that, since
there is unlikely to be any peer review here.


Yes indeed.
and (2) in this case, there's no good advice to be given except
"You can't do that that way; ask an MS-DOS group."


Which advice was already given, albeit more comprehensively (the OP
did mention Unix.)


Yes indeed, and so was lots of other advice, some of it bad.
Thus I thought it would be good to point out that the *only* good
advice to be given was the above.
What was your point in posting this off-topic reply?


To inform the OP that (1) C people can give you really bad x86
advice; and (2) in this case, there's no good advice to be given
except "You can't do that that way; ask an MS-DOS group."

But also (3) to show a standard C way of accomplishing his
stated goal; and (4) to inform him that his stated goal didn't
make any sense, because CX doesn't hold any useful information.

Go ahead, ask me why I posted *this* reply...

-Arthur
Nov 14 '05 #16
>> I'm curious as to how you will apply it to any program on a machine
which doesn't have a CX register.


I stated on my first post that this was for the x86 processor, sun
microsystems use the UltraSparc type processor, this is a complelty
different archietecture, so's PowerPC, and ARM. But i never said that
I wanted to do this on any of them styles archietecture. Just x86.


It is extremely likely that by the time your program gets to execute,
whatever used to be in the CX register has been written over by something
the compiler put there. The x86 architecture is not known for having
a lot of registers. Expect that they will be used and re-used often.

Besides that, *ALL* "reasonable" compilers (that is, MINE) pre-load
all of the 32-bit registers (and uninitialized memory) with the
pattern 0xdeadbeef (the bit pattern for a NULL pointer that God
Intended). (CX overlaps the 32-bit register ECX.)

Gordon L. Burditt
Nov 14 '05 #17
On 13 Feb 2004 15:34:51 -0800, gr**********@hotmail.com (jeff) wrote:
I figured out a way to do it now anyway
I'm curious as to how you will apply it to any program on a machine
which doesn't have a CX register.
I stated on my first post that this was for the x86 processor, sun
microsystems use the UltraSparc type processor, this is a complelty
different archietecture, so's PowerPC, and ARM. But i never said that
I wanted to do this on any of them styles archietecture. Just x86.

greg


You also, said, in that same post,
I
doubt there will be any difference between *nix and windows products,
I think i should make it clear that I would like to find out the space
that is being used by a certain program, in both *nix and windows. I
no that this is the CX register


--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Philip | last post by:
Hi, first of all I'm new to this whole .net thing so please forgive my ignorance. Now I have a C++ object with me, and I want to be able to call its methods from VB.net. e.g. Foo::Foo { }...
3
by: Ruby Deepdelver | last post by:
Hello, I'm having trouble in find certain information, i've search over the web and through the documentation but i haven't had lucky. I need to know if there is some limit in the amount of...
5
by: volvox | last post by:
hi, is it possible to access protected mode registers (EAX , EBX,ECX...) via assembly in C .
7
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself...
0
by: ajitpsingh | last post by:
Hi, I recived Error message when acessing OLE Object. "Cannot start the source application for this object" in MS Excel.
2
by: neat2006 | last post by:
This is somewhat of a strange issue. Allow me to detail it as much as I can. We use the Blackboard Transaction System (BTS) for all students dinning services. The "ENVISION" server is Running...
0
by: neat2006 | last post by:
This is somewhat of a strange issue. Allow me to detail it as much as I can. We use the Blackboard Transaction System (BTS) for all students dinning services. The "ENVISION" server is Running...
0
by: neat2006 | last post by:
This is somewhat of a strange issue. Allow me to detail it as much as I can. We use the Blackboard Transaction System (BTS) for all students dinning services. The "ENVISION" server is Running...
3
by: swamimeenu | last post by:
hi, I am facing the problem in vb tht if more than one user accessing my program from various systems,if both of them acessing the progam at a same time (ie) for feeding data in it ,at tht time i...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.