## #190 use djnz for integer variables in loop reversal

open
nobody
5
2010-05-21
2006-12-16
No

/* it would be nice if djnz instructions
were used for the loop.

Currently is:

mov r2,#0x00
mov r3,#0x02
00103\$:
cpl _mybit
dec r2
cjne r2,#0xff,00109\$
dec r3
00109\$:
mov a,r2
orl a,r3
jnz 00103\$

And could be:
mov r2,#((0x200+0xff)>>8) ; see crtxclear.asm
mov r3,#0x00 ;
00103\$:
cpl _mybit
djnz r3,00103\$
djnz r2,00103\$
*/

bit mybit;

#if 1
# define BODY mybit = !mybit
#else
# define BODY *ptr++ = 0x00
#endif

void foo()
{
char xdata *ptr = (unsigned char xdata *) 0xf000;
int i;

for(i=0;i<0x200;i++)
BODY;
}

## Discussion

• Frieder Ferlemann - 2007-04-16

sample source with formatting preserved

• Frieder Ferlemann - 2007-04-16

Logged In: YES
user_id=589052
Originator: YES

SDCC is already able to generate the above requested
code for a loop with a known 16-bit loop count:)

_bar:
mov r2,#0x00
mov r3,#0x02
00101\$:
cpl _mybit
djnz r2,00101\$
djnz r3,00101\$
ret

if it is presented with the following source:

#define COUNT_H(x) (((x)+0xff)>>8)
#define COUNT_L(x) ((x)&0xff)
#define ITERATIONS 0x200

void bar ()
{
unsigned char i, k;

k = COUNT_H (ITERATIONS);
i = COUNT_L (ITERATIONS);

do
do
BODY;
while (--i);
while (--k);
}

it would be nice(TM) if SDCC would ...

• wek - 2007-04-16

Logged In: YES
user_id=1201677
Originator: NO

Good joke, Frieder... :-)

I'll add an another one tip for optimisation of loops: see http://www.8052.com/forum/read.phtml?id=133111 (and also the explanation in http://www.8052.com/forum/read.phtml?id=133114 , which clearly shows that it is not the best idea to put most of the optimisations into the peepholes).

Shouldn't we open a separate tracker thread for "optimisation requests"?

Jan Waclawek

• labels: --> mcs51(8051) target