I have function for caseless compare:
int strnicmp (const char *s1, const char *s2, int maxlen) {
char c1, c2;
do {
c1=*s1++;
if (c1>='a' && c1<='z') c1&=0xDF;
c2=*s2++;
if (c2>='a' && c2<='z') c2&=0xDF;
// problematic code
if (c1==c2) maxlen--;
} while (maxlen && c1 && c2 && c1==c2);
// problematic code end
if ((!c1 && !c2) || !maxlen) return 0;
if ((!c1 && c2) || (c1<c2)) return -1;
return 1;
}
Problematic code is compiled into this assembly:
;Common.c:31: if (c1==c2) maxlen--;
; genCmpEq
; genCmpEq: left 1, right 1, result 1
;4
ld a,c
sub b ;A=c1-c2
jr NZ,00139$
ld a,#0x01 ;A=1
jr 00140$
00139$:
xor a,a ;A=0
00140$:
;6
; genIfx
or a,a ;using A
jr Z,00113$
; genMinus
dec de
; genLabel
00113$:
;Common.c:32: } while (maxlen && c1 && c2 && c1==c2);
; genIfx
ld a,e ;overwriting A!!!
or a,d
jr Z,00114$
; genIfx
xor a,a
or a,c
jr Z,00114$
; genIfx
xor a,a
or a,b
jr Z,00114$
; genIfx
or a,a ;seems wants to use A???
jp NZ,00112$
It seems that it wants to use previously computed result stored in A but
later this is overwritten. What to do to get correctly compiled program?
Thanks
Hynek Sladky
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Sdcc-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sdcc-user