Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

strange problem with k*e*i*l.

Status
Not open for further replies.

ITP

Advanced Member level 4
Joined
Jul 11, 2001
Messages
116
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
1,134
Hi All,
I am facing a strange problem with k*e*i*l. For an "if" statement in C it
generates an unconditional jump (LJMP)instruction. But when I put a dummy
statement [ while(!STOP_KEY_PORT)] it works.

Below I have shown a segment of my C*51 program and its corresponding k*e*i*l
output.

The declaration for two bit variable used are as follows


sbit STOP_KEY_PORT = P1^5;
sbit velocity_in = P2^3;


------------------------START-----------------------
// original C code
{
P1=0x0f0;
velocity_in = 1;

while(!velocity_in)
{
if(!STOP_KEY_PORT) goto abort;
}
while(velocity_in)
{
if(!STOP_KEY_PORT) goto abort;
}
}


K*e*i*l output:


758: P1=0x0f0;
C:0x0CA8 7590F0 MOV P1(0x90),#adc_data(0xF0)
759: velocity_in = 1;
760:
C:0x0CAB D2B4 SETB velocity_in(0xB0.4)
761: while(!velocity_in)
C:0x0CAD 20B403 JB velocity_in(0xB0.4),C:0CB3
762: {
763: if(!STOP_KEY_PORT) goto abort;
C:0x0CB0 020D80 LJMP C:0D80
764: }
765: while(velocity_in)
C:0x0CB3 30B403 JNB velocity_in(0xB0.4),C:0CB9
766: {
767: if(!STOP_KEY_PORT) goto abort;
C:0x0CB6 020D80 LJMP C:0D80
768: }
769: }
----------------------------END-------------------------------




---------------------------START------------------------------
//C Code after the introduction of statement [ while(!STOP_KEY_PORT)]


{
P1=0x0f0;
velocity_in = 1;
while(!STOP_KEY_PORT); // <-- newly introduced line
while(!velocity_in)
{
if(!STOP_KEY_PORT) goto abort;
}
while(velocity_in)
{
if(!STOP_KEY_PORT) goto abort;
}
}


K*e*i*l output:

758: P1=0x0f0;
C:0x0B0B 7590F0 MOV P1(0x90),#adc_data(0xF0)
759: velocity_in = 1;
C:0x0B0E D2B4 SETB velocity_in(0xB0.4)
760: while(!STOP_KEY_PORT);
C:0x0B10 3095FD JNB STOP_KEY_PORT(0x90.5),C:0B10
761: while(!velocity_in)
C:0x0B13 20B406 JB velocity_in(0xB0.4),C:0B1C
762: {
763: if(!STOP_KEY_PORT) goto abort;
C:0x0B16 2095FA JB STOP_KEY_PORT(0x90.5),C:0B13
C:0x0B19 020BF2 LJMP C:0BF2
764: }
765: while(velocity_in)
C:0x0B1C 30B406 JNB velocity_in(0xB0.4),C:0B25
766: {
767: if(!STOP_KEY_PORT) goto abort;
C:0x0B1F 2095FA JB STOP_KEY_PORT(0x90.5),C:0B1C
C:0x0B22 020BF2 LJMP C:0BF2
768: }
769: }
---------------------------END----------------------------



Kindly help me to understand this problem.
Thanks for your time.
Itp.
 

tom324

Full Member level 1
Joined
Jun 26, 2001
Messages
98
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Croatia
Activity points
672
Put optimization level to zero (turn optimization off) and try again. It could be one of Keil's strange bugs.

Tom
 

crono

Member level 3
Joined
May 1, 2001
Messages
59
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
767
HI,

May be a cut and paste error but in the example you declare

sbit velocity_port = P2^3;

and use

velocity_in

if it is't an error and if velocity_in is declared into the function without type it may be a int type (see your .lst and .M51 file).

If you use a high level of optimization and because the test is repeted two times the compiler optimize with a subroutine for the test of "int" == 0.
 

Hero

Full Member level 2
Joined
Mar 6, 2002
Messages
145
Helped
4
Reputation
8
Reaction score
2
Trophy points
1,298
Activity points
1,608
Hi,

ke*il compiler is very good in some cases but in some other is not.

For example if you want to make optimal iterative loop use do while loop instead for loop and declare loop varable as local register varable. In the first case compiler use DJNZ instruction and register variable as loop counter in the second case use CJNE and temporary varable in data memory.

It is not easy to write well optimized compiler for CISC CPU with limited resources.

With well C and assembly programming experience you can always force ke*il compiler to produce good asssembly code.

Where C compiler is not enough efficient use pure assembly routines. When you write your C programs it is always good to think about problem from compiler point of view.

I found that ke*il compiler is excellent for coding do while loops and switch case structures. Be ware with some library functions and always check results when you use them.

With new RISC CPUs situation is better because you can easy get efficient assembly code from all C constructions because you have appropriated instructions for coding.
 

ITP

Advanced Member level 4
Joined
Jul 11, 2001
Messages
116
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
1,134
crono

Hi crono,

Yes, it was a cut and paste error. I am sorry. I have corrected it.

sbit velocity_port = P2^3; // Wrong !!!
sbit velocity_in = P2^3; // Corrected One.




When I changed the optimization to 2 [Data overlaying] It generated the correct code.
Earlier the optimization was on 8 [Reuse common entry code], I think this option is default.

If it was an optimization problem in k*e*i*l, is there any way to tell the
compiler to don't do optimization for this particular bit variable?.

Thanks to all your replies.
Regards
Itp
 

M!k

Advanced Member level 2
Joined
Apr 22, 2002
Messages
682
Helped
109
Reputation
218
Reaction score
35
Trophy points
1,308
Activity points
4,818
Re: crono

ITP said:
... is there any way to tell thecompiler to don't do optimization for this particular bit variable?

by using the #pragma ot(level) you can define the optimization level in the source. I never tried it out but it should be possible to write this:
#pragma ot(0) // disable optimization
... // code that shouldn't be optimized
#pragma ot(8,SPEED) // reestablish default optimization level
... // optimized code

Consider the C51 compiler user guide...


Mik
 

chcliang

Newbie level 6
Joined
Oct 30, 2001
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
8
which version did you tested ?
It's seem ok under version 6.23.
(all optimization level)




Chcliang.
 

crono

Member level 3
Joined
May 1, 2001
Messages
59
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
767
Hi,

on version 7.02 an optimize 9 is OK

----------------------------------------------
*51 COMPILER V7.02b

Test1.c ROM(SMALL) OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTE
-XTEND CODE LISTINCLUDE



3 sbit STOP_KEY_PORT = P1^5;
4 sbit velocity_in = P2^3;

7 // original C code
8 void main(void)
9 {
10 1 P1=0x0f0;
11 1 velocity_in = 1;
12 1
13 1 while(!velocity_in)
14 1 {
15 2 if(!STOP_KEY_PORT)
16 2 goto abort;
17 2 }
18 1
19 1 while(velocity_in)
20 1 {
21 2 if(!STOP_KEY_PORT)
22 2 goto abort;
23 2 }
24 1
25 1 abort:
26 1 while (1);
27 1
28 1 }

; FUNCTION main (BEGIN)
; SOURCE LINE # 8
; SOURCE LINE # 9
; SOURCE LINE # 10
0000 7590F0 MOV P1,#0F0H
; SOURCE LINE # 11
0003 D2A3 SETB velocity_in
0005 ?C0001:
; SOURCE LINE # 13
0005 20A305 JB velocity_in,?C0005
; SOURCE LINE # 14
; SOURCE LINE # 15
0008 2095FA JB STOP_KEY_PORT,?C0001
; SOURCE LINE # 16
000B 8006 SJMP ?C0008
; SOURCE LINE # 17
000D ?C0005:
; SOURCE LINE # 19
000D 30A303 JNB velocity_in,?C0008
; SOURCE LINE # 20
; SOURCE LINE # 21
0010 2095FA JB STOP_KEY_PORT,?C0005
; SOURCE LINE # 22
; SOURCE LINE # 23
; SOURCE LINE # 25
0013 abort:
0013 ?C0008:
; SOURCE LINE # 26
0013 80FE SJMP ?C0008
; FUNCTION main (END)


*51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
-------------------------------------------
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top