This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C51 Compiler, 8051 Utilities manuals' examples syntax for 'code' conflict

User's guide, 01.97, C compiler C51 on page 63 'explicitly defined memory types' recommends a syntax.

The Utilities guide, 04.95, page 119, has a different syntax in files c_mess0.c, c_mess1.c.

the utilities guide contradicts the recommendation in the user's guide.

Yet the 'preferred' method in the C51 compiler will NOT work. You must use the 'obsolete' method of the Utilities guide.

(1) Please explain.

(2) What is correct syntax for:
const unsigned char code *MyArray[NumberFruits] = {"apple,"banana","cherry"};

  • (2) What is correct syntax for:
    const unsigned char code *MyArray[NumberFruits] = {"apple,"banana","cherry"};


    Maybe I'm missing something, but the following code compiles just fine here.

    const unsigned char code *MyArray[] = { "apple", "banana", "cherry" };
    

    Note that I did have to add a quote after apple and I removed NumberFruits from the array index since I didn't know what it was (variable, #define) and what its value was.

    Jon

  • You can obtain a free CD from Keil, which includes all the manuals in PDF.

    My CD is dated 09.2000 and has C51 User's Guide 03.2000 and Assembler/Utilities Guide 07.2000 - so your manauals would seem to be way out of date!


    With the mspace qualifiers (code, xdata, etc), you can specify both where the pointer is located, and the mspace to which it points; eg:

    A definition of the form

    int code *var
    is a pointer to an int located in code memory;

    A definition of the form
    code int code *var
    is a pointer located in code memort which points to an int located in code memory.

  • I agree: it compiles fine. The problem is that the linker behaves differently depending on the position of the keyword 'code'. If you place 'code' at the beginning of the line (against the recommendation of the C51 manual and according to the example in the Utilities manual) you can properly locate this in the banked code space.

    However, if you place 'code' in the location that C51 manual recommends, the linker does not behave properly. Specifically, the strings go into the banked, code space (as desired), but the pointers to the strings go into the common area.

    (Thanks for your reply.)

  • (1) I have the 09.2000.9 CD also. (I didn't realize that the Utilities guide is now included with the assembler PDF. I was looking for a special utilities PDF.)

    Unfortunately, the example in the utilities section refers to c_mess[01].c but does not list the the source code (as the outdated 04.95 did).

    (2) Your explanation and example of using the keyword 'code' answers my question.

    I didn't realize that there could be more than one instance of the keyword 'code'. I concluded (wrongly) from the 'Explicitly Declared Memory Types' (page 71) that the latter form was preferred over the first and that there could be only one instance of the memory location keyword 'code'.

    Thanks a lot.

  • Yes you can have two mem. space qualifiers. However I believe for future compatibility we are supposed to write the definitions thusly:

    // Pointer is in data space but points back to code space.
    char code * data pToCodeChar; 
    
    // Pointer is in code but points back to idata space.
    char idata * code pToIdataChar; 
    
    // Pointer in xdata but points back to default memory space (typically data, small model).
    char * xdata pToDefaultChar;
    
    // Pointer in default memory model but points to pdata.
    char pdata *pToPdataChar;
    I think you get the idea.

    - Mark

  • (I didn't realize that the Utilities guide is now included with the assembler PDF. I was looking for a special utilities PDF.)

    Yes: there are 2 separate printed manuals, but only one PDF!

  • You're right; that's the format stated on p84 of the C51 User's Guide, 03.2000.

  • >Andrew Neil wrote:

    >A definition of the form

    >code int code *var
    
    >is a pointer located in code memort
    >which points to an int located in code
    >memory.



    This work, but this is obsolete syntax,
    supplied just for compatibility with old versions of C51.

    I think, good style is:

    Exapmle_1:
    int code * code ptr;
    

    In common:
    VAR_TYPE MEMORY_TYPE VAR_NAME;
    
    In Example_1:
    // VAR_NAME     - "ptr"
    // MEMORY_TYPE - "code"
    // VAR_TYPE    - "int code * "
    //                 (pointer to int, located in 'code' memory)
    


    Example_2:

    int xdata * code ptr;
    
    // VAR_NAME     - "ptr"
    // MEMORY_TYPE - "code"
    // VAR_TYPE    - "int xdata * "
    //                 (pointer to int, located in 'xdata' memory)
    




    In other words, MEMORY_TYPE keyword (code,data,bdata,xdata,pdata) should be placed just before variable name.


    Last example:
    char code * xdata * data ptr;
    
    // Read from right to left:
    //
    // 'ptr' (located in 'data' memory) is a pointer to
    // (located in 'xdata' memory) pointer to
    // (located in 'code'  memory) 'char'