Jump to content
IGNORED

EricBall's Tech Projects - Z3 compiler frustration


RSS Bot

Recommended Posts

What do you think the following C should do?

 

unsigned byte zmem[128*1024];

int read_word( int a, x )
{
 a = zmem[a++]<<8 + zmem[a];
...
}

There are three (!) errors in the above code:

  1. precedence error : addition is done before shift
  2. order of operations : is a incremented before the second array reference?
  3. compiler bug : a contains a++ after the operation

All three can be worked around by using a temporary variable. Here's another gotcha I ran into:

int PC;

void run( void )
{
...
 op2code( op & 31, zmem[PC++], zmem[PC++] );
...
}

Looks reasonable, provide the next two bytes in the instruction stream as operands. Again, there are several potential bugs lurking:

  1. is PC incremented before or after the function call (tested - it's before, at least with my compiler)
  2. order of operations : which operand gets which byte?
  3. is actually PC incremented before the second array reference?

#3 turned out to be not true - the same value was passed in for both operands. Again, recoding with temporary variables worked around the problem.

 

In both of these cases, the code looks sane. The compiler doesn't flag them as errors. But the results aren't what you'd expect.

 

 

http://www.atariage.com/forums/index.php?a...;showentry=6130

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...