Is this broken or wot?

R

Robert Samelson

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

Willem

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
 
B

Barry Schwarz

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

James Kuyper

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

Keith Thompson

Robert Samelson said:
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.
 
M

Malcolm McLean

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

Bl0ckeduser .

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.
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top