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

How to fine tune a piece of C code into macro?

I have a piece of C code,


if(u3Varaible0 > u3Variable2)
{
#ifdef DEF_SOMETHING
.
.
#else
.
.
#endif
}
else
{
.
.
.
}


The variable length of u3Varaible0 is 3 bytes. I don't want to declare it as "unsigned long"(inorder to save onchip memory and reduce execution cycles), So I have to write this piece C code into asm code look like


CLR C
MOV Au3Varaible0_L
SUBB A,u3Variable2_L
MOV Au3Varaible0_M
SUBB A,u3Variable2_M
MOV Au3Varaible0_H
SUBB A,u3Variable2_H
JC C0049
.
.
.
.
C0049:
.
.
.


I want it be a macro(inorder to reduce execution cycles).
But now comes the problem:
1.
If I write this code as a asm macro in an asm source. I just can't use this macro from a C code.

2.
If I try to write is as C macro using #define, It is impossible to use inline assembly in the macro!!

3.
#ifdef DEF_SOMETHING
will also cause an error in #define macro


Does anyone know another good way to solve this fine tune problem?

Parents
  • It should be possible to expand a macro that includes in-line assembly, I have never done it myself.

    If you are really so very desparate for code and memory space, you will have to write your entire functions in assembler. This might be worth the effort if you are writing an ISR or perhaps you have to handle many 24-bit values in a very short time.

    But, before you do that, consider that there are many compiler optimisations that cannot be performed on code with in-line assembler. Consequently, you may loose more than you gain.

Reply
  • It should be possible to expand a macro that includes in-line assembly, I have never done it myself.

    If you are really so very desparate for code and memory space, you will have to write your entire functions in assembler. This might be worth the effort if you are writing an ISR or perhaps you have to handle many 24-bit values in a very short time.

    But, before you do that, consider that there are many compiler optimisations that cannot be performed on code with in-line assembler. Consequently, you may loose more than you gain.

Children
  • "But, before you do that, consider that there are many compiler optimisations that cannot be performed on code with in-line assembler. Consequently, you may loose more than you gain."

    [Ovid]
    That's why I prefer to write my project in C code with only some asm code(need to be fine tune).