0

I'm stumped by some of my variables getting clobbered on an ATTiny2313. Commenting out lines of my code one by one, the culprit seems to be the multiplication in this function:

int32_t bmp085_b5(int32_t ut) {
    int32_t x1, x2;
    x1 = (ut - (int32_t) ac6) * ((int32_t) ac5) >> 15;
    x2 = ((int32_t) mc << 11) / (x1 + (int32_t) md);
    return x1 + x2;
}

Either commenting out the x1 = ... line (and changing the reference to x1 to ut), or changing the multiply operation to an addition fixes the variable clobbering. To be specific, the upper byte of mc is being zeroed. What am I missing here? It is probably something very very obvious and/or stupid, but I'm not seeing it...

The variables ac5 and ac6 are of type uint16_t, mc and md are int16_t.

Michel
  • 603
  • 6
  • 15
  • 2
    Is this reproducible in a minimal test program with nothing but the above, or only as part of a larger program? I suspect you have a buffer overflow clobbering variables on the stack, and the multiplication is causing the compiler to have to discard values cached in registers (due to running out of registers) and having to reload them from the stack, thereby getting the clobbered values. – R.. GitHub STOP HELPING ICE Mar 14 '15 at 17:08
  • 1
    Ah, so it could be a lack of memory issue? I'll try to strip my code even more, to see if that helps--i.e., I'll remove the i2c stuff. But if that fixes it, the next problem is of course to get all of it to fit in memory! – Michel Mar 14 '15 at 17:12
  • 1
    The '2313 only has 128 bytes of RAM. Give a '4313 a try (256 bytes of RAM). That should tell you almost instantly. – Ignacio Vazquez-Abrams Mar 14 '15 at 17:28
  • Oh. With that little ram it might simply be a stack overflow/wraparound. – R.. GitHub STOP HELPING ICE Mar 14 '15 at 17:33
  • 1
    Ha! Fixed. You guys are lifesavers ;) I had "helpful" strings in my code to aid in debugging. I need to get used to the embedded mindset again. 128 bytes is really really tiny! – Michel Mar 14 '15 at 17:48

0 Answers0