#2253 A bug in Division of STM8

closed-fixed
None
STM8
6
2014-02-13
2014-02-13
Ben Shi
No

With c code

extern char ad(char *p);

unsigned int ss(unsigned int c, unsigned int d)
{
char s1[255];
ad(s1);
return c / d;
}

the following assembly instructions are generated

_ss:
ldw y, sp
subw y, #6
sub sp, #255
; c.c: 6: ad(s1);
ldw x, sp
incw x
pushw y
pushw x
call _ad
addw sp, #2
popw y
; c.c: 7: return c / d;
pushw y
ld a, (0xc, y)
push a
ld a, (0xb, y)
push a
ldw x, y
ldw x, (0xb, x)
popw y
divw x, y
popw y
addw sp, #255
ret

However, parameter d is loaded into register x, rather than parameter c. So the result is always 1. :)

This is due to line 3569 of stm8/gen.c, the right->aop should be left->aop.

Discussion

    • assigned_to: Philipp Klaus Krause
    • Priority: 5 --> 6
     
  • Increasing priority, since bad code is generated silently.

    Philipp

     
    • status: open --> closed-fixed
     
  • Fixed in revision #8946.

    Philipp