Acessing registers with C

Discussion in 'C Programming' started by jeff, Feb 13, 2004.

  1. jeff

    jeff Guest

    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
     
    jeff, Feb 13, 2004
    #1
    1. Advertising

  2. jeff

    Richard Bos Guest

    (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
     
    Richard Bos, Feb 13, 2004
    #2
    1. Advertising

  3. jeff

    Tom St Denis Guest

    "jeff" <> wrote in message
    news:...
    > 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
     
    Tom St Denis, Feb 13, 2004
    #3
  4. Tom St Denis wrote:
    > "jeff" <> 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." |
    +---------+----------------------------------+-----------------------------+
     
    Kenneth Brody, Feb 13, 2004
    #4
  5. jeff

    jeff Guest

    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
     
    jeff, Feb 13, 2004
    #5
  6. jeff

    Alan Balmer Guest

    On 13 Feb 2004 02:51:32 -0800, (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
     
    Alan Balmer, Feb 13, 2004
    #6
  7. jeff

    Alan Balmer Guest

    On 13 Feb 2004 10:09:18 -0800, (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
     
    Alan Balmer, Feb 13, 2004
    #7
  8. jeff

    Chris Torek Guest

    In article <news:>
    jeff <> 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.
     
    Chris Torek, Feb 13, 2004
    #8
  9. Alan Balmer <> scribbled the following:
    > On 13 Feb 2004 10:09:18 -0800, (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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Hasta la Vista, Abie!"
    - Bart Simpson
     
    Joona I Palaste, Feb 13, 2004
    #9
  10. 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
     
    Thomas Matthews, Feb 13, 2004
    #10
  11. jeff

    Malcolm Guest

    "jeff" <> 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.
     
    Malcolm, Feb 13, 2004
    #11
  12. 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
     
    Arthur J. O'Dwyer, Feb 13, 2004
    #12
  13. jeff

    Alan Balmer Guest

    On Fri, 13 Feb 2004 18:01:10 -0500 (EST), "Arthur J. O'Dwyer"
    <> 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
     
    Alan Balmer, Feb 13, 2004
    #13
  14. On Fri, 13 Feb 2004 18:01:10 -0500 (EST), in comp.lang.c , "Arthur J.
    O'Dwyer" <> 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 =---
     
    Mark McIntyre, Feb 13, 2004
    #14
  15. jeff

    jeff Guest

    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
     
    jeff, Feb 13, 2004
    #15
  16. On Fri, 13 Feb 2004, Alan Balmer wrote:
    >
    > <> 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
     
    Arthur J. O'Dwyer, Feb 13, 2004
    #16
  17. >> 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
     
    Gordon Burditt, Feb 13, 2004
    #17
  18. jeff

    Alan Balmer Guest

    On 13 Feb 2004 15:34:51 -0800, (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
     
    Alan Balmer, Feb 13, 2004
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay
    Replies:
    2
    Views:
    928
    Peter O'Reilly
    Nov 21, 2003
  2. sandy
    Replies:
    2
    Views:
    553
    sandy
    Apr 26, 2004
  3. Fabiano

    Acessing codebehind data from ASPX

    Fabiano, May 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    Jeffrey Palermo [MCP]
    May 26, 2004
  4. Steve Peterson

    Acessing property

    Steve Peterson, Jun 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    397
    Kevin Spencer
    Jun 17, 2004
  5. Fernando Monteiro

    Acessing gridview data programmatially

    Fernando Monteiro, Nov 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,679
    Fernando Monteiro
    Nov 10, 2005
Loading...

Share This Page