#1479 Z80 Port unsigned int < operator generates incorrect ASM

closed-fixed
z80 port (188)
5
2013-05-25
2008-09-05
No

BUG
---
1. Z80 Port. It looks like using the less then operator on unsigned int in the condition of a while loop may cause incorrect assembler code to be generated when large stack variables are present.

SAMPLE CODE (save to a file called bug.c)
-----------------------------------------
#include <stdio.h>

void Bug() {
char varA[256];
unsigned int varB;
unsigned short varC;

while (varB < VarC) {}
}

void main() {}

INVOCATION
----------
sdcc -mz80 bug.c

VERSION
-------
SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08
2.8.0 #5117 (Mar 23 2008) (MINGW32)

GENERATED ASM
-------------
;--------------------------------------------------------
; File Created by SDCC : free open source ANSI-C Compiler
; Version 2.8.0 #5117 (Mar 23 2008) (MINGW32)
; This file was generated Thu Sep 04 23:19:15 2008
;--------------------------------------------------------
.module bug
.optsdcc -mz80

;--------------------------------------------------------
; Public variables in this module
;--------------------------------------------------------
.globl _main
.globl _Bug
;--------------------------------------------------------
; special function registers
;--------------------------------------------------------
;--------------------------------------------------------
; ram data
;--------------------------------------------------------
.area _DATA
;--------------------------------------------------------
; overlayable items in ram
;--------------------------------------------------------
.area _OVERLAY
;--------------------------------------------------------
; external initialized ram data
;--------------------------------------------------------
;--------------------------------------------------------
; global & static initialisations
;--------------------------------------------------------
.area _HOME
.area _GSINIT
.area _GSFINAL
.area _GSINIT
;--------------------------------------------------------
; Home
;--------------------------------------------------------
.area _HOME
.area _HOME
;--------------------------------------------------------
; code
;--------------------------------------------------------
.area _CODE
;bug.c:3: void Bug() {
; genLabel
; genFunction
; ---------------------------------
; Function Bug
; ---------------------------------
_Bug_start::
_Bug:
push ix
ld ix,#0
add ix,sp
ld hl,#-260
add hl,sp
ld sp,hl
;bug.c:10: while (rxSize < packetSize) {
; genCmpLt
; AOP_EXSTK for _Bug_rxSize_1_1
; AOP_EXSTK for _Bug_packetSize_1_1
ld iy,#0
add iy,sp
ld a,0(iy)
ld iy,#2
add iy,sp
sub a,0(iy)
ld iy,#0
add iy,sp <== CORRUPTS C FLAG SET BY PREVIOUS SUB INSTRUCTION
ld a,1(iy)
ld iy,#2
add iy,sp <== CORRUPTS C FLAG NEEDED BY FOLLOWING INSTRUCTION
sbc a,1(iy) <== NOW USES CORRUPTED C FLAG
ld a,#0x00
rla
ld c,a
; genLabel
00101$:
; genIfx
xor a,a
or a,c
jr NZ,00101$
; genLabel
; genEndFunction
ld sp,ix
pop ix
ret
_Bug_end::
;bug.c:15: void main() {}
; genLabel
; genFunction
; ---------------------------------
; Function main
; ---------------------------------
_main_start::
_main:
; genLabel
; genEndFunction
ret
_main_end::
.area _CODE
.area _CABS

PROBLEM ENCOUNTERED
-------------------
The code generated to do the less then comparison on the two unsigned 16 bit numbers is using a SUB followed by a SUBC to do the subtraction of the 16 bit numbers. But between the SUB and SUBC instructions, there are ADD instructions which are corrupting the C flag set by the SUB instruction and needed by the SUBC instruction.

See line 67 of the generated asm output for the first SUB instruction
See line 69 of the generated asm output for the first ADD instruction which corrupts the C flag
See line 72 of the generated asm output for the second ADD instruction which corrupts the C flag
See line 73 of the generated asm output for the SUBC instruction which relies on the C flag from the line 67 SUB but uses instead the C flag value set by the line 72 ADD instruction

Discussion

  • FlyingElectron

    FlyingElectron - 2008-09-05

    Source code to reproduce bug

     
  • FlyingElectron

    FlyingElectron - 2008-09-05

    Generated ASM code for bug.c

     
  • FlyingElectron

    FlyingElectron - 2008-09-05

    Logged In: YES
    user_id=2194979
    Originator: YES

    File Added: bug.asm

     
  • Maarten Brock

    Maarten Brock - 2008-09-07
    • milestone: --> fixed
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-fixed
     
  • Maarten Brock

    Maarten Brock - 2008-09-07

    Logged In: YES
    user_id=888171
    Originator: NO

    Fixed in SDCC 2.8.3 #5233.

     

Log in to post a comment.

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

Sign up for the SourceForge newsletter:





No, thanks