Menu ▾ ▴

#2719 Z80: compilation bug when comparing integers

closed-invalid
None
Z80
5
2018-02-15
2018-02-14
No

Hello,

I've joined two source files that highlight a compilation bug.

I put here the first one:

bug1.c :

#include <stdio.h>

int i=0;
int j=1;

main()
{
    printf("i=%d j=%d\n", i, j);
    if (i < j) puts("i<j"); else puts("i>=j");
}

The output is:

i=0 j=1
i>=j

The assembly generated for the test:

;bug1.c:9: if (i < j) puts("i<j"); else puts("i>=j");
        ld      hl, #_j
        ld      iy, #_i
        ld      a, 0 (iy)
        sub     a, (hl)
        ld      a, 1 (iy)
        inc     hl
        sbc     a, (hl)
        jp      PO, 00110$
        xor     a, #0x80
00110$:
        jp      P, 00102$

(00102$ is the else clause)
If I'm not mistaken, a parity test is made on the high order byte of the result and as it is set, the sign bit is inverted, which distorts the result.

1 Attachments

Discussion

  • Christophe Staïesse

    Here the second case

     
  • Christophe Staïesse

    Well, I was completly mistaken, the code is correct. it was of am emulator bug. Sorry for the disturbance.

     
  • Philipp Klaus Krause

    • status: open --> closed-invalid
    • assigned_to: Philipp Klaus Krause
    • Category: other --> Z80
     

Log in to post a comment.