Gripes and Moans of a High level programmer in a Low level world
Posted on 2009-04-29 21:22 in Blog • Tagged with programming
The majority of my schooling, I focused on high level software. I was more interested in data structures for sorting and storing information, abstracting ideas to create very robust classes, and implementing distributed applications (client/ server) than most of the low level programming details. That being said, I did take an assembly, C, and C++ class which have all proved very helpful in my day to day work as a programmer.
I currently work for a defense contractor writing software for embedded systems. Many of the components have a painfully small foot print. Getting a processor with 2K ROM which runs at 20kHz is not usual. This being said, all the software is written in either C (not C++) or assembly.
This has led to a major problem for me. I have found myself assuming too much while programming and inadvertently introducing logic problems, because a statement works differently in C that it would in Java or C#.
Type checking and precedence:
char a = 0xA0, b = 0x0A;
if(a | b != 0xAA)
As a java programmer I assumed the OR operation would be completed, and then the two sides would be compared. As it turns out, the comparison has a higher precedence level than the bit-wise OR. Still, in java, the strict type checking would have caught the problem since a Boolean value can’t be OR’d with a character value. In C, the result of the comparison is either a 1 or a 0, a number. It is a fairly straight forward fact that a number can be OR’d against another value. As such, this little logic error was propagated forward and was caught in testing a few days after getting committed. It has since been corrected:
if((a | b) != 0xAA)
Another problem I’ve been dealing with has to do with compiler bugs. Today, I ran into an annoying compiler optimization.
v1 = data[0]
v2 = data[1]
if( v1 != ~v2 ){
return...
}
continue function another 12 lines
The compiler “optimized” this to…
v1 = data[0]
v2 = data[1]
return
It threw out the ‘if’ statement and all of the code below the ‘if’ statement and replace it with a simple return. The unusual thing about this, is v1 and v2 are local variables and should have been optimized out also if they are going to be assigned and then immediately discarded. Hmm… The code was modified below and everything acted as it should have.
char a = v1 ^ v2
if( a != 0xFF ){
return
}
As I find more unusual traits of the compiler I’m using, I’ll be sure to document them.