Acessing registers with C

J

jeff

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
 
R

Richard Bos

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
 
T

Tom St Denis

jeff said:
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
 
K

Kenneth Brody

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".
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." |
+---------+----------------------------------+-----------------------------+
 
J

jeff

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
 
A

Alan Balmer

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.
 
A

Alan Balmer

Hiya


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.
 
C

Chris Torek

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.)
 
J

Joona I Palaste

Alan Balmer said:
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.
 
T

Thomas Matthews

jeff said:
Hiya




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




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
 
M

Malcolm

jeff said:
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.
 
A

Arthur J. O'Dwyer

[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
 
A

Alan Balmer

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?
 
M

Mark McIntyre

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.
 
J

jeff

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
 
A

Arthur J. O'Dwyer

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.
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
 
G

Gordon Burditt

I'm curious as to how you will apply it to any program on a machine
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
 
A

Alan Balmer

I figured out a way to do it now anyway


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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top