#1787 Z80 multiplication code bug

z80 port (188)


// This SDCC test program demonstrates a serious Z80 multiplication bug
// This bug present with internal loop if called a procedure with statement

// if we define THE_BUG, the bug is present. DoProc() not returns.
#define THE_BUG
// if we define CORRECT, the bug is hidden (masked). DoProc() returns.
//#define CORRECT

void TheBug (void);
void DoProc (signed char col, signed char row, unsigned char spr);

unsigned char y;

int main (void) {
y=1; TheBug();
return 0;

void TheBug (void)
unsigned char i,j,n,spr;
for (j=0; j<=1; j++) {
for (n=1; n<=7; n++) {
for (i=2; i<=7; i++) {
#ifdef THE_BUG
DoProc(30, i*y, spr); // Here i*y == 1 every time
#ifdef CORRECT
DoProc(30, y*i, spr); // Here is correct

void DoProc (signed char col, signed char row, unsigned char spr) {
if(row!=1) return;

sdcc\sdcc -mz80 --code-loc 26000 --data-loc 0xF000 --no-std-crt0 --opt-code-size --funsigned-char --disable-warning 59 -L z80 testbug.c

SDCC : mcs51/gbz80/z80/ds390/pic16/pic14/TININative/ds400/hc08 3.0.2 #6489 (May 9 2011) (MINGW32)

4. if change 'y*i' to 'i*y', we will have the serious problem. Possible reasons are deep internal loop + statement calculated in calling a fn + optimizations in SDCC code generation. I've no ideas, sorry. Thanks

5. My e-mail is allot )at( ukr.net


  • Oleg N. Cher

    Oleg N. Cher - 2011-05-09

    Source and batch files for replaying a bug

  • Philipp Klaus Krause

    Fixed in revision #6555.


  • Philipp Klaus Krause

    • assigned_to: nobody --> spth
    • status: open --> closed-fixed
  • Maarten Brock

    Maarten Brock - 2011-06-10

    Is the fact that spr is uninitialized part of this bug or can we initialize it in the regression test to remove the warning?

  • Philipp Klaus Krause

    I don't remember, but suppose that initializing it e.g. to 0 wouldn't change the behaviour, since the initialization would be outside all control structures.


  • Maarten Brock

    Maarten Brock - 2011-06-11

    When it is initialized the constant propagation might remove spr all together which changes register allocation and register access around the call. On the other hand it might have no effect on the bug.

  • Philipp Klaus Krause

    'Forgot about constant propagation. This bug is sensitive to the register assignment, thus initializing spr would probably change the behaviour. On the other hand, the world today is not what it was yesterday, with the new register allocator changing everything anyway.


  • Maarten Brock

    Maarten Brock - 2011-06-11

    I tried with initializer and SDCC 3.0.0 and it fails as expected. I have updated the regression test to remove the warning in #6589.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks