puts() code - just for fun - Programmer

This is a discussion on puts() code - just for fun - Programmer ; I was wondering whether or not it's possible to output the (hex)code of a stanard C function like puts() using someting like this: #include int main(void) { unsigned long int n; unsigned char c; n = (unsigned long int)puts; printf("puts() ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: puts() code - just for fun

  1. puts() code - just for fun

    I was wondering whether or not it's possible to output the (hex)code of
    a stanard C function like puts() using someting like this:

    #include

    int main(void)
    {
    unsigned long int n;
    unsigned char c;

    n = (unsigned long int)puts;

    printf("puts() starts at address - 0x%lX\n\nDUMP:\n\n", n);

    // While we haven't found RET.
    //
    while((c = *(unsigned char *)n) != 0x4D)
    {
    printf("\t%lX : 0x%.2X\n", n++, c);
    }

    printf("puts() ends [ret] at address - 0x%lX\n\n", n);

    return 0;
    }

    Asides from the fact that this code contains lots of undefined
    behaviour - does it look like it would do the job?

    As I say, this is just for fun really.

    x

    Jo


  2. Re: puts() code - just for fun

    mechanicfem@googlemail.com wrote:
    > I was wondering whether or not it's possible to output the (hex)code of
    > a stanard C function like puts() using someting like this:
    >
    > #include
    >
    > int main(void)
    > {
    > unsigned long int n;
    > unsigned char c;
    >
    > n = (unsigned long int)puts;
    >
    > printf("puts() starts at address - 0x%lX\n\nDUMP:\n\n", n);
    >
    > // While we haven't found RET.
    > //
    > while((c = *(unsigned char *)n) != 0x4D)
    > {
    > printf("\t%lX : 0x%.2X\n", n++, c);
    > }
    >
    > printf("puts() ends [ret] at address - 0x%lX\n\n", n);
    >
    > return 0;
    > }
    >
    > Asides from the fact that this code contains lots of undefined
    > behaviour - does it look like it would do the job?
    >
    > As I say, this is just for fun really.
    >
    > x
    >
    > Jo
    >

    It will certainly print quite a few hex numbers but probably not what
    you want.

    The biggest problem that I see is that you've made your pointer n a
    pointer to a long int. That means it points to a 32 bit value which is
    four bytes. When it is incremented, its value will increase by four.
    Thus you'll only see every fourth byte printed. (Note also that on some
    platforms int or long int pointers are required to be aligned; that is
    they must contain an address that is a multiple of their size. You don't
    know whether that's true of the address of puts().)

    If you fix this problem by making n a pointer to an unsigned char,
    you'll get every byte printed (one per line) but your loop may not stop
    until some sort of exception, or it may stop too soon. There are several
    forms of the "RET" instruction and puts() may end with something other
    than 0x4D. It's also possible that puts() contains more than one return
    statement and thus more than one "RET" instruction and you may never get
    past the first one.

    What you're trying to do here is called a "disassembler". If you really
    need to do this, you should buy a good one.

    Norm

    --
    --
    To reply, change domain to an adult feline.


  3. Re: puts() code - just for fun


    Norman Bullen wrote:
    > mechanicfem@googlemail.com wrote:
    > > I was wondering whether or not it's possible to output the (hex)code of
    > > a stanard C function like puts() using someting like this:
    > >
    > > #include
    > >
    > > int main(void)
    > > {
    > > unsigned long int n;
    > > unsigned char c;
    > >
    > > n = (unsigned long int)puts;
    > >
    > > printf("puts() starts at address - 0x%lX\n\nDUMP:\n\n", n);
    > >
    > > // While we haven't found RET.
    > > //
    > > while((c = *(unsigned char *)n) != 0x4D)
    > > {
    > > printf("\t%lX : 0x%.2X\n", n++, c);
    > > }
    > >
    > > printf("puts() ends [ret] at address - 0x%lX\n\n", n);
    > >
    > > return 0;
    > > }
    > >
    > > Asides from the fact that this code contains lots of undefined
    > > behaviour - does it look like it would do the job?
    > >
    > > As I say, this is just for fun really.
    > >
    > > x
    > >
    > > Jo
    > >

    > It will certainly print quite a few hex numbers but probably not what
    > you want.
    >
    > The biggest problem that I see is that you've made your pointer n a
    > pointer to a long int. That means it points to a 32 bit value which is
    > four bytes. When it is incremented, its value will increase by four.
    > Thus you'll only see every fourth byte printed. (Note also that on some
    > platforms int or long int pointers are required to be aligned; that is
    > they must contain an address that is a multiple of their size. You don't
    > know whether that's true of the address of puts().)
    >
    > If you fix this problem by making n a pointer to an unsigned char,
    > you'll get every byte printed (one per line) but your loop may not stop
    > until some sort of exception, or it may stop too soon. There are several
    > forms of the "RET" instruction and puts() may end with something other
    > than 0x4D. It's also possible that puts() contains more than one return
    > statement and thus more than one "RET" instruction and you may never get
    > past the first one.
    >
    > What you're trying to do here is called a "disassembler". If you really
    > need to do this, you should buy a good one.


    Thanks for the reply.

    > The biggest problem that I see is that you've made your pointer n a
    > pointer to a long int. That means it points to a 32 bit value which is
    > four bytes.


    Is this really true - not that a long int *, when incremented, will not
    move to point to the next long, but that I've done this?

    n is just a long ...

    unsigned long int n;

    so, in the while loop, ++n will just add one to it, i.e., n is declared
    as a long, and not a long *?

    x

    Jo


  4. Re: puts() code - just for fun

    mechanicfem@googlemail.com wrote:
    > Norman Bullen wrote:
    >
    >>mechanicfem@googlemail.com wrote:
    >>
    >>>I was wondering whether or not it's possible to output the (hex)code of
    >>>a stanard C function like puts() using someting like this:
    >>>
    >>>#include
    >>>
    >>>int main(void)
    >>>{
    >>> unsigned long int n;
    >>> unsigned char c;
    >>>
    >>> n = (unsigned long int)puts;
    >>>
    >>> printf("puts() starts at address - 0x%lX\n\nDUMP:\n\n", n);
    >>>
    >>> // While we haven't found RET.
    >>> //
    >>> while((c = *(unsigned char *)n) != 0x4D)
    >>> {
    >>> printf("\t%lX : 0x%.2X\n", n++, c);
    >>> }
    >>>
    >>> printf("puts() ends [ret] at address - 0x%lX\n\n", n);
    >>>
    >>> return 0;
    >>>}
    >>>
    >>>Asides from the fact that this code contains lots of undefined
    >>>behaviour - does it look like it would do the job?
    >>>
    >>>As I say, this is just for fun really.
    >>>
    >>>x
    >>>
    >>>Jo
    >>>

    >>
    >>It will certainly print quite a few hex numbers but probably not what
    >>you want.
    >>
    >>The biggest problem that I see is that you've made your pointer n a
    >>pointer to a long int. That means it points to a 32 bit value which is
    >>four bytes. When it is incremented, its value will increase by four.
    >>Thus you'll only see every fourth byte printed. (Note also that on some
    >>platforms int or long int pointers are required to be aligned; that is
    >>they must contain an address that is a multiple of their size. You don't
    >>know whether that's true of the address of puts().)
    >>
    >>If you fix this problem by making n a pointer to an unsigned char,
    >>you'll get every byte printed (one per line) but your loop may not stop
    >>until some sort of exception, or it may stop too soon. There are several
    >>forms of the "RET" instruction and puts() may end with something other
    >>than 0x4D. It's also possible that puts() contains more than one return
    >>statement and thus more than one "RET" instruction and you may never get
    >>past the first one.
    >>
    >>What you're trying to do here is called a "disassembler". If you really
    >>need to do this, you should buy a good one.

    >
    >
    > Thanks for the reply.
    >
    >
    >>The biggest problem that I see is that you've made your pointer n a
    >>pointer to a long int. That means it points to a 32 bit value which is
    >>four bytes.

    >
    >
    > Is this really true - not that a long int *, when incremented, will not
    > move to point to the next long, but that I've done this?
    >
    > n is just a long ...
    >
    > unsigned long int n;
    >
    > so, in the while loop, ++n will just add one to it, i.e., n is declared
    > as a long, and not a long *?
    >
    > x
    >
    > Jo
    >

    You're correct. I didn't read carefully enough.

    Norm

    --
    --
    To reply, change domain to an adult feline.


+ Reply to Thread