Renesas TM V.3.20A Spezifikationen Seite 99

  • Herunterladen
  • Zu meinen Handbüchern hinzufügen
  • Drucken
  • Seite
    / 762
  • Inhaltsverzeichnis
  • LESEZEICHEN
  • Bewertet. / 5. Basierend auf Kundenbewertungen
Seitenansicht 98
Renesas Technology, Tools FAQs
Last Updated: October 20, 2000
Document Number: 01051271_e
Q.
When I describe a multiply expression in C language, the expression does not result in the expected value. Why not?
For example, as shown below, the expected value for y0 is 0x0300, but the actual result is 0x055.
[Program example]
#define CONST_A 0x0400
#define CONST_B 0x0c0
void main( void )
{
unsigned char x0;
unsigned int y0;
x0 = 0x090;
y0 = ( x0 * CONST_A ) / CONST_B;
}
A.
The value of y0 is not 0x0300 because the multiplication result is stored as "int", and an overflow occurs during the multiplication
operation. You should cast x0 as "unsigned long" and make sure the multiplication results are stored as long. By using this
work-around, y0 should become 0x0300.
#define CONST_A 0x0400
#define CONST_B 0x0c0
void main( void )
{
unsigned char x0;
unsigned int y0;
x0 = 0x090;
y0 = ( (unsigned long)x0 * CONST_A ) / CONST_B;
}
[Reference notes]
The cause of y0 becoming 0x055 when x0 is "unsigned char", is as follows:
In the equation "y0 = ( x0 * CONST_A ) / CONST_B", x0 is converted to "signed int" when "x0 * CONST_A" is executed.
Therefore, the multiplication result is "int".
**immediate (CONST_A, CONST_B) is treated as "signed int".
1.
When dividing the multiplication result by CONST_B, the numerator (the multiplication result shown in item 1 above) is
expanded to signed long (32-bit) due to the M16C mnemonic limitations.
**The M16C MCUs have mnemonics for dividing long (32-bit) by short (16-bit).
2.
The division result of item 2 is stored in y0. In item 1 of this procedure, the result of "unsigned char * signed int" is treated as
"int". However, the multiplication result is 0x024000 and the upper first byte (0x020000) is cut off. Therefore, the division
equation becomes "0x4000/0x0c0" and the result becomes 0x055.
"y0 = ( x0 * CONST_A ) / CONST_B;" output code is as follows:
3.
Seitenansicht 98
1 2 ... 94 95 96 97 98 99 100 101 102 103 104 ... 761 762

Kommentare zu diesen Handbüchern

Keine Kommentare