# Translate from AVR pgm_read_byte to normal C array manipulation in STM8

I am trying to translate a couple of lines of code from AVR space data to STM8 normal C data manipulation.

As you should know, AVR has a program space which can storage data for read only operation, hence a special function should be used to read the data (pgm_read_byte()). In the case of STM8, it has a unique flash for program space and ram space, and storage data in program space is simple as using static constant keyword before the variable definition. The main problem for me is translate the AVR code to normal c code in STM8.

This is the line that I want to translate:

AMP[divider] = pgm_read_byte(envs[divider] + (((unsigned char*)&(EPCW[divider]+=EFTW[divider]))[1]))


And this is my translation:

EPCW[divider] += EFTW[divider];
AMP[divider] = EPCW[1+envs[divider]];


I am not sure if "EPCW[divider] += EFTW[divider];" is executed after or before of data reading in "pgm_read_byte()" function. And also I am not sure if the translated index are correct.

Since pgm_read_byte() as a macro resolves finally in __LPM() is looks like being a function call. Then all its arguments are calculated before the call.
The argument to pgm_read_byte() is a address, so I would try this:
AMP[divider] = *(envs[divider] + (((unsigned char*)&(EPCW[divider]+=EFTW[divider]))[1]));

#define pgm_read_byte(address) ((uint8_t)(*(address)))