Is this broken or wot?

Discussion in 'C Programming' started by Robert Samelson, Feb 13, 2012.

  1. unsigned x = 0;
    if(x-- < 4)
    printf("foo\n");
    else
    printf("bar\n");

    GCC for Data General Aviion 5000 (an 88000 machine) will convert this
    into:

    unsigned x = 0;
    if(--x < 3) ....

    in the assembly.

    This seems broken if x == ~0.
     
    Robert Samelson, Feb 13, 2012
    #1
    1. Advertising

  2. Robert Samelson

    Willem Guest

    Robert Samelson wrote:
    ) unsigned x = 0;
    ) if(x-- < 4)
    ) printf("foo\n");
    ) else
    ) printf("bar\n");
    )
    ) GCC for Data General Aviion 5000 (an 88000 machine) will convert this
    ) into:
    )
    ) unsigned x = 0;
    ) if(--x < 3) ....

    That doesn't look like assembly to me.
    I assume this is your 'decompiling' of the actual assembly instructions.
    It means that there either is a fault in the compiler's compilation, or
    in your decompilation. Without information on what the actual assembly
    instructions are, it's hard to say which it is.

    ) in the assembly.
    )
    ) This seems broken if x == ~0.

    You should test what the actual output from the code is, to see if it
    does what it's supposed to.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Feb 13, 2012
    #2
    1. Advertising

  3. On Mon, 13 Feb 2012 18:53:49 +0000 (UTC), Robert Samelson
    <> wrote:

    >unsigned x = 0;
    >if(x-- < 4)
    > printf("foo\n");
    >else
    > printf("bar\n");
    >
    >GCC for Data General Aviion 5000 (an 88000 machine) will convert this
    >into:
    >
    > unsigned x = 0;
    > if(--x < 3) ....
    >
    >in the assembly.
    >
    >This seems broken if x == ~0.


    As long as the code produces the correct response for any initial
    value of x, why would you think it broken? For x initialized to 3, 2,
    or 1, it should print foo. For all other others it should print bar.
    Do you have results that show otherwise?

    --
    Remove del for email
     
    Barry Schwarz, Feb 13, 2012
    #3
  4. Robert Samelson

    James Kuyper Guest

    On 02/13/2012 03:14 PM, Barry Schwarz wrote:
    > On Mon, 13 Feb 2012 18:53:49 +0000 (UTC), Robert Samelson
    > <> wrote:
    >
    >> unsigned x = 0;
    >> if(x-- < 4)
    >> printf("foo\n");
    >> else
    >> printf("bar\n");
    >>
    >> GCC for Data General Aviion 5000 (an 88000 machine) will convert this
    >> into:
    >>
    >> unsigned x = 0;
    >> if(--x < 3) ....
    >>
    >> in the assembly.
    >>
    >> This seems broken if x == ~0.

    >
    > As long as the code produces the correct response for any initial
    > value of x, why would you think it broken? For x initialized to 3, 2,
    > or 1, it should print foo. For all other others it should print bar.


    No, for x initialized with 0 (as shown), the original C code should
    print "foo\n", not "bar\n". What you've described corresponds to the
    second sample of C code. That difference is why the rewrite would be
    incorrect.
     
    James Kuyper, Feb 13, 2012
    #4
  5. Robert Samelson <> writes:
    > unsigned x = 0;
    > if(x-- < 4)
    > printf("foo\n");
    > else
    > printf("bar\n");
    >
    > GCC for Data General Aviion 5000 (an 88000 machine) will convert this
    > into:
    >
    > unsigned x = 0;
    > if(--x < 3) ....
    >
    > in the assembly.
    >
    > This seems broken if x == ~0.


    It's broken if and only if the program produces incorrect output. C
    code specifies behavior, not machine code.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 13, 2012
    #5
  6. On Feb 13, 6:53 pm, Robert Samelson <> wrote:
    > unsigned x = 0;
    > if(x-- < 4)
    >                 printf("foo\n");
    > else
    >                 printf("bar\n");
    >
    > GCC for Data General Aviion 5000 (an 88000 machine) will convert this
    > into:
    >
    >         unsigned x = 0;
    >         if(--x < 3) ....
    >
    > in the assembly.
    >
    > This seems broken if x == ~0.
    >

    Yes, it will fail, because of wrap round. If it prints "bar" than this
    is a compiler bug.
     
    Malcolm McLean, Feb 13, 2012
    #6
  7. I was convinced once that gcc generated wrong code
    because my program ran differently on two machines.

    But further inspection revealed that I was using the %
    operator on a zero operand.
     
    Bl0ckeduser ., Feb 14, 2012
    #7
    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. Cindy M  -WordMVP-
    Replies:
    0
    Views:
    568
    Cindy M -WordMVP-
    Dec 2, 2003
  2. Kev

    wot no postback?

    Kev, Mar 20, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    587
    jasonkester
    Mar 24, 2005
  3. Steven D'Aprano

    Why are "broken iterators" broken?

    Steven D'Aprano, Sep 21, 2008, in forum: Python
    Replies:
    8
    Views:
    693
  4. Cameron Simpson

    Re: Why are "broken iterators" broken?

    Cameron Simpson, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    613
    Cameron Simpson
    Sep 22, 2008
  5. Fredrik Lundh

    Re: Why are "broken iterators" broken?

    Fredrik Lundh, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    627
    Fredrik Lundh
    Sep 22, 2008
Loading...

Share This Page