Microchip C18 - Weird code behavior (maybe extended-mode / non-extended-mode related)

1.3k views Asked by At

I have this weird problem with the Microchip C18 compiler for PIC18F67J60.

I have created a very simple function that should return the index of a Sub-String in a larger String.

I don't know whats wrong, but the behavior seems to be related to wether extended mode is enabled or not.

With Extended-Mode enabled in MPLAB.X I get:

  • The memcmppgm2ram function returns zero all the time.

With Extended-Mode disabled in MPLAB.X I get:

  • The value of iterator variable i counts as: 0, 1, 3, 7, 15, 21

I'm thinking some stack issue or something, because this is really weird. The complete code is shown below.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char bigString[] = "this is a big string";

unsigned char findSubStr(char *str, const rom char *subStr, unsigned char n, unsigned char m)
{
    unsigned char i;

    for (i=0; i < n-m; i++)
    {
        if(0 == memcmppgm2ram(&str[i], (const far rom void*)subStr, m))
            return i;
    }
    return n; // not found
}

void main(void)
{
    char n;

    n = findSubStr(bigString, (const rom void*)"big", sizeof(bigString), 3); 
}
1

There are 1 answers

0
makes On

memcmppgm2ram() expects a pointer to data memory (ram) as its first argument. You are passing a pointer to a string literal, which is located in program memory (rom).

You can use memcmppgm() instead, or copy the other string to ram using memcpypgm2ram() or strcpypgm2ram().

Unfortunately I can't test this, as I don't have access to this compiler at the moment.