ARM Technical Support Knowledge Articles

RET AND RETS GENERATED IN THE SAME FUNCTION

Applies to: C166 C Compiler

Answer


Information in this article applies to:


SYMPTOMS

I have written the following code:

extern int test1;

int (WriteState) (void)
{
  if (test1 == 0)
     return 1;
  return 2;
}

The assembly code generated by the compiler is incorrect. The first return statement uses a RET instruction. The second return statement uses a RETS instruction.

 stmt lvl     source

    1         extern int test1;
    2
    3         int (WriteState) (void)
    4         {
    5  1
    6  1        if (test1 == 0)
0000 F2F40000 E    MOV       R4,test1
0004 3D02          JMPR      cc_NZ,?C0001
    7  1           return 1;
0006 E014          MOV       R4,#01H
0008 CB00          RET
000A         ?C0001:
    8  1        return 2;
000A E024          MOV       R4,#02H
    9  1      }
000C         ?C0002:
000C DB00          RETS

Obviously this causes problems as the RET instruction only pops the return address off the stack. However, the RETS instruction pops the return address and the Code Segment Pointer off the stack.

CAUSE

This problem is caused by the parentheses around the function name:

int (WriteState) (void)

This confuses the compiler as to the function's memory model.

RESOLUTION

This problem is solved in C166 Version 4.05 or higher. You may download the latest updates from the Keil Website.

If you must use an older version of the tools, there are two workarounds for this problem.

MORE INFORMATION

ATTACHED FILES

Request the files attached to this knowledgebase article.

Article last edited on: 2008-04-06 19:38:05

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