ARM Technical Support Knowledge Articles

INTEGER PROMOTION

Applies to: C51 C Compiler

Answer


Information in this article applies to:


SYMPTOMS

I have written the following program that performs operations using unsigned chars.

void Test (void)
{
unsigned char Bob;
unsigned char Sally;
unsigned char Tom;
unsigned char Result;

Sally = Bob + 3;

if (( Tom + 2) == 5)
  {
  Result = 0 ;
  }
}

The code that's generated performs these operations in integers (16 bits).


             ; FUNCTION SpecialTest (BEGIN)
                                           ; SOURCE LINE # 7
                                           ; SOURCE LINE # 8
                                           ; SOURCE LINE # 14
0000 E500        R     MOV     A,Bob
0002 2403              ADD     A,#03H
0004 F500        R     MOV     Sally,A
                                           ; SOURCE LINE # 16
0006 E500        R     MOV     A,Tom
0008 2402              ADD     A,#02H
000A FF                MOV     R7,A
000B E4                CLR     A
000C 33                RLC     A
000D FE                MOV     R6,A
000E EF                MOV     A,R7
000F 6405              XRL     A,#05H
0011 4E                ORL     A,R6
0012 7002              JNZ     ?C0002
                                           ; SOURCE LINE # 17
                                           ; SOURCE LINE # 18
0014 F500        R     MOV     Result,A
                                           ; SOURCE LINE # 19
                                           ; SOURCE LINE # 20
0016         ?C0002:
0016 22                RET

What causes that?

CAUSE

This is caused by the ANSI integer promotion rules that the Keil C51 compiler observes. These rules are defined by the ANSI C Specification.

RESOLUTION

ANSI integer promotion rules are used by default. However, you may disable them by unselecting this option in the C51 compiler options (in uVision2) or from the command line with the NOINTPROMOTE directive.

The resulting output is what you will get when you do that.

                                           ; SOURCE LINE # 13
0000 E500        R     MOV     A,Bob
0002 2403              ADD     A,#03H
0004 F500        R     MOV     Sally,A
                                           ; SOURCE LINE # 15
0006 E500        R     MOV     A,Tom
0008 2402              ADD     A,#02H
000A FF                MOV     R7,A
000B BF0503            CJNE    R7,#05H,?C0003
                                           ; SOURCE LINE # 16
                                           ; SOURCE LINE # 17
000E E4                CLR     A
000F F500        R     MOV     Result,A
                                           ; SOURCE LINE # 18
                                           ; SOURCE LINE # 19
0011         ?C0003:
0011 22                RET

SEE ALSO

Article last edited on: 2001-04-16 00:00:00

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential