0

I'm using SDCC to compile for a STM8 microcontroller. Compiling the following file results in a seemingly unnecessary div instruction - which is very slow on the STM8.

char a[1];
char b;

void foo() __interrupt(1) {
    char c = a[0];
    b = c >> 0;
}

void main() {
}

Compiling with sdcc -mstm8 file.c results in the following assembly:

_foo:
    clr a
    div x, a
    ld  a, _a+0
    ld  _b+0, a
    iret

The function seems to work as expected, however I can't see why the first two instructions are required.

phuclv
  • 37,963
  • 15
  • 156
  • 475
user2248702
  • 2,741
  • 7
  • 41
  • 69
  • 1
    did you compile with optimization on? And please show a [mcve]. A broken if block isn't meaningful – phuclv Apr 30 '21 at 05:29
  • The [manual](https://www.st.com/resource/en/programming_manual/cd00161709-stm8-cpu-programming-manual-stmicroelectronics.pdf) says that if the denominator is zero, the operand registers are unaffected. And the `div` is interruptible. So it's a zero being pushed, not a remainder. In all this looks like an intentional delay. I wonder if it's something to do with interrupt protocol. Maybe it's time for a data register to latch or something. – Gene Apr 30 '21 at 05:41
  • And what is your question that **we** can answer? -- BTW, the SDCC project has a friendly user mailing list. You might like to ask there for the reasons. – the busybee Apr 30 '21 at 08:30
  • 1
    Where is the `iret` instruction? This doesn't look like correct/complete disassembly of an ISR. Does it make any difference if you drop obsolete C style and write the function correctly as `void updateDisplay(void) __interrupt(23)`? – Lundin Apr 30 '21 at 08:40
  • @phuclv I've rewritten the question with the minimal amount of code possible to see the div instruction. My SDCC version is 4.1.0. – user2248702 Apr 30 '21 at 08:54

1 Answers1

2

Looks like that's a bug somewhere in the compiler because if b = c >> 0 is changed to b = c << 0, b = c + 0, b = a[0]... then no such thing happens. The behavior is observed on both optimized and unoptimized code. But if you optimize for size (--opt-code-size) then only the div is there, the clr instruction isn't emitted. You might want to report that to the developers

Demo on Compiler Explorer

phuclv
  • 37,963
  • 15
  • 156
  • 475