Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this broken or wot?

Reply
Thread Tools

Is this broken or wot?

 
 
Robert Samelson
Guest
Posts: n/a
 
      02-13-2012
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.
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      02-13-2012
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
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      02-13-2012
On Mon, 13 Feb 2012 18:53:49 +0000 (UTC), Robert Samelson
<(E-Mail Removed)> 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-13-2012
On 02/13/2012 03:14 PM, Barry Schwarz wrote:
> On Mon, 13 Feb 2012 18:53:49 +0000 (UTC), Robert Samelson
> <(E-Mail Removed)> 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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-13-2012
Robert Samelson <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      02-13-2012
On Feb 13, 6:53*pm, Robert Samelson <(E-Mail Removed)> 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.
 
Reply With Quote
 
Bl0ckeduser .
Guest
Posts: n/a
 
      02-14-2012
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why are "broken iterators" broken? Steven D'Aprano Python 8 09-28-2008 09:19 PM
Re: Why are "broken iterators" broken? Fredrik Lundh Python 0 09-22-2008 04:32 PM
Re: Why are "broken iterators" broken? Cameron Simpson Python 0 09-22-2008 04:32 AM
Latest WIndows Update for Wireless-G PCI Adapter Broken? Paul Moloney Wireless Networking 0 06-13-2005 06:40 PM
wireless broken Nasir Sadi Wireless Networking 0 03-16-2005 04:13 PM



Advertisments