 |
|
 |
MAEC TOOL NEWS:
MAECT-M3T-NC77WA-020516D
A Note on C Compiler M3T-NC77WA
|
Please take note of the following problem in using C compiler (with an assembler and integrated development environment) M3T-NC77WA for the 77xx series MCUs:
- On testing equality between the value that a function returns and a constant of 0
- Versions Concerned
M3T-NC77WA V.4.00 Release 1 -- V.5.20 Release 4
- Description
When a function returns a value of type signed or unsigned char, testing equality between this returned value and a constant of 0 will be performed in an incorrect way.
The reason is that the instruction for changing the status of the M or X flag is not correctly generated at compilation, so the test is conducted in 16 bits wide, not 8 bits.
- 2.1 Conditions
- This problem occurs if the following three conditions are satisfied:
- (1) An equality test is performed between the value a function returns and a constant of 0.
- Note that (a) conditional expressions in if, do, while, and for statements are involved, and
(b) the type of description "func( )" can also be tested with a constant of 0.
- (2) The function returns a value of type signed or unsigned char.
- (3) When calling the function, its arguments are saved on the stack.
- Note that if all the arguments are passed to the function via registers, Condition (3) is not met since the stack is not involved.
- 2.2 Example
- When the following C-source program is compiled, the instruction for changing the status of the M flag (the sem instruction that would be generated immediately before the test instruction) is not generated, resulting in an incorrect test being performed.
---------------------------------------------------------------------
unsigned char func(unsigned char, unsigned char); /* Condition (2) */
volatile int i;
void xxx( unsigned char c )
{
if( func( c, 1 ) == 0 ) /* Condition (1) */
i = 0;
else
i = 1;
}
---------------------------------------------------------------------
- Workaround
This problem will be circumvented by modifying the description of the C-source program in either of the following ways:
- (1) Make a function return a value of type other than signed or unsigned char
[Example]
---------------------------------------------------------------------
unsigned int func(unsigned char, unsigned char); /* Other than char */
volatile int i;
void xxx( unsigned char c )
{
if( func( c, 1 ) == 0 )
i = 0;
else
i = 1;
}
---------------------------------------------------------------------
- (2) When a function returns a value, assign it to a temporary variable of type other than signed or unsigned char; then perform an equality test between the temporary variable and a constant of 0.
---------------------------------------------------------------------
unsigned char func(unsigned char, unsigned char);
volatile int i;
void xxx( unsigned char c )
{
int tmp; /* Temporary variable tmp of type
other than char */
tmp = func( c, 1 ); /* Returned value is assigned to tmp */
if( tmp == 0 )
i = 0;
else
i = 1;
}
---------------------------------------------------------------------
- Schedule of Fixing the Problem
We plan to fix this problem in our next release.
|
 |