BitTest and While or IF statement

General discussion on mikroC.
Post Reply
Author
Message
bignand
Posts: 1
Joined: 27 Jul 2005 08:24

BitTest and While or IF statement

#1 Post by bignand » 27 Jul 2005 08:37

Dear Friends,

I'm try to develop my first C application on PIC 16F676. Normally I use MPASM from microchip.
I'm using the Uregistred version of the compiler.

I'm usually check the assembler generated by the compiler to check the quality of code due the uC used have so little RAM/FLASH.

I have found two strange behavoiur:

FIRST:

check this piece of code

Code: Select all

do {
  // Make SomeThing
} while (1);
The compiler generate all the code to check if 1 is 1 !!!!
Normally all the compiler substitute the while with a unconditioned jump.

SECOND:

Code: Select all

while (!INTCON.T0IF);
The compiler make this code:

Cast bit to short integer
(Make an bittest and load STACK1 with the value 1 if bit asserted)
Load STACK2 with the value 0
Make a test beetwen short integer.

It's better make a bit-test like

Code: Select all

REP          BTFSC  INTCON,T0IF
               GOTO   REP
Can Anyone observed this?
There's any work-around to do the compiler uses bit-tests?

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: BitTest and While or IF statement

#2 Post by pizon » 27 Jul 2005 09:30

bignand wrote:

Code: Select all

do {
  // Make SomeThing
} while (1);
The compiler generate all the code to check if 1 is 1 !!!!
Normally all the compiler substitute the while with a unconditioned jump.
I checked this on mikroC v2.1.0.0 and it generates optimal code (unconditional jump). If you have an older version, please upgrade to this one.

Code: Select all

while (!INTCON.T0IF);
...There's any work-around to do the compiler uses bit-tests?
At the present moment,

Code: Select all

while (INTCON.T0IF == 0) 
will produce somewhat less code for bit testing. You can do it like this as well:

Code: Select all


void main() {
...
  asm  {
    BTFSC     INTCON,T0IF
    GOTO      ___main_Usr_Lbl_01
  }
    //do something
    goto Usr_Lbl_02;
  Usr_Lbl_01:
    //do something else
  Usr_Lbl_02:
    //continue program
}
The program memory page settings are then up to you.

MikroC is a work in progress; it is being constantly improved and will continue to do so. Unfortunately, the advanced code optimization issues must come last, due to the nature of work.
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#3 Post by gambrose » 28 Jul 2005 17:29

I tend to write something like this if i am looking for performance. good idea get compiler to generate the labels.

Code: Select all

//do something

do
{

    //do something else

asm BTFSC     INTCON,T0IF
}while(1);

//continue program
Graham Ambrose

gblair
Posts: 2
Joined: 28 Jul 2005 20:09

#4 Post by gblair » 28 Jul 2005 20:48

You need a simple goto:

Code: Select all

Loop:
  ...
  goto Loop;

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#5 Post by gambrose » 28 Jul 2005 21:09

the

Code: Select all

do{
}while(1) 
provide the simple goto loop but the compiler provides the loop label names which is a much better situation else you find your code jumping to a point in another function because you forgot you used that name already.
Graham Ambrose

Post Reply

Return to “mikroC General”