cancel
Showing results for 
Search instead for 
Did you mean: 

Cosmic compiler, not completely right behaviour?

luter
Associate II
Posted on February 09, 2006 at 10:23

Cosmic compiler, not completely right behaviour?

3 REPLIES 3
luter
Associate II
Posted on February 09, 2006 at 07:06

Hello, All.

I discovered a little bit wrong behaviour of Cosmic compiler V4.5c.1 - 20 Jun 2005.

Let's examine following functions. They differ no so much.

1)

void my_func(unsigned char i)

{

unsigned char j;

j = i;

...

}

2)

void my_func(unsigned char i)

{

unsigned char j;

_asm(''ld a,0'');

j = i;

...

}

Value of j should be in both cases the same. But in practice it is not true. Why?

Let's take a look in asm code.

1)

;void my_func(unsigned char i)

;{

switch .text

_my_func:

push a

OFST: set 257

;j = i;

ld x,s

ld (OFST+0,x),a

...

;}

2)

;void my_func(unsigned char i)

;{

switch .text

_my_func:

push a

OFST: set 257

;_asm(''ld a,0'');

ld a,0

;j = i;

ld x,s

ld (OFST+0,x),a

...

;}

The function parameter i is stored in register a and on stack.

In the 1st case as far as register a keeps value of i compiler uses it to fill variable j.

But in the 2nd case register a are „corrupted“ by _asm function. Compiler doesn't analyse this code and skips it. That's why variable j gets a wrong value, in this case allways „0“.

Ok, we can prevent such sutiation by loading register a with value of parameter i ( _asm(''pop a\n push a'') or something else).

I would like to have more nice solution.

Is it a bug or a feature?

Do any compiler keys or parameters exist to make it analyse inlined asm code?

Tnx, Luter

luca239955_st
Associate III
Posted on February 09, 2006 at 09:09

Luter,

the compiler always assumes that any inline asm does not break any resource; if you do (in your case the A register), it's up to you to save and restore what you break.

Regards,

Luca

Viktor POHORELY
ST Employee
Posted on February 09, 2006 at 10:23

There is no direct link between C and Assembly in this case. Inline assembly is handle ''like'' function, so there is no expectation that you will destroy registers and/or variables.

For more details, you can contact Cosmic support on their web page.