Multos Forums

   

Calling macros with runtime-updated parameters

Rank

Total Posts: 1

Joined 2014-02-05

PM

Hi,

I’m dealing with a similar problem described in this thread i.e. error E8000: arguments to __code can only be locals, arguments, globals, or integer constants. This time, I need to perform the right shift of a block of memory by a unknown number of bits (extracted during run time). I’m relaying on the multosBlockShiftRight macro, defined in MULTOS.h as

#define multosBlockShiftRight(blockLength, numShiftBits, blockSource, blockDest) \
do \
{ \
  __push 
(BLOCKCAST(blockLength)(__typechk(unsigned char *, blockSource))); \
  __code 
(PRIMPRIM_SHIFT_RIGHTblockLengthnumShiftBits); \
  __code 
(STORE__typechk(unsigned char *, blockDest), blockLength); \
while (0

There is a workaround for calling multosBlockShiftRight  as multosBlockShiftRight(4, offset, var_1, var_2); where offset is a local variable updated at run time?

Thanks in advance,
Antonio

 

     
RankRankRank

Total Posts: 74

Joined 2012-02-21

PM

Hi Antonio,

The best solution to your problem depends on how much shifting you are going to be doing in your application and how big the data is that you are shifting. Some options:-

1. Use the >> operator. However, the resulting byte code isn’t terribly efficient, so don’t do this if you do the shift operation many times. Also, you’re limited to shifting 4 byte values at most (longs).

2. Use the divide / operator. You have to work out the value to divide by. You can do this most efficiently by having a look-up table if you know the maximum number of bits you will shift by.

#pragma melstatic
static BYTE shiftTable[8] {1,2,4,8,16,32,64,128};

unsigned long shiftR_using_math(unsigned long data,BYTE places )
{
 BYTE denominator 
shiftTable[places];
 return (
data denominator);

 
3. Use the multosBlockDivide macro. Again you need to have a lookup table to get the denominator to use but this time the block length can be up to 255 bytes.

Hope this helps,
Chris.