0 Introduction
Recently, I encountered a situation where the ROM size was quite tight, and the code size was too large, resulting in the inability to fit all the code into the ROM. I had to optimize the C code line by line. During the optimization process, I noticed that in previous cases, some frequently called functions were declared using define. I researched the benefits of defining functions with define and recorded my findings.
The following examples are only meant to illustrate the differences, so the functions used are relatively simple.
1 Conventional Subfunction Calls
Assuming there is the following subfunction:
int add (int a, int b){
return (a+b);
}
The main function is as follows:
int main (void){
int num1;
int num2;
int sum;
num1 = 1;
num2 = 2;
sum = add(num1, num2);
return 0;
}
The disassembled content after compilation is as follows (the disassembly instruction function is quite numerous, so only a portion is captured):

This compilation tool can match the disassembly with each line of C code.
Jumping from the <span>main</span> function to the corresponding disassembly of the subfunction takes these few lines:

The declaration of the subfunction <span>add</span> corresponds to the following disassembly lines:

Jumping out of the subfunction <span>add</span> corresponds to these disassembly lines:

The actual code required for the subfunction <span>add</span> is these few lines:

The entire calling process can be summarized as follows:
Jump from the main function to the subfunction -> Declare the subfunction -> Execute the subfunction body -> Jump back from the subfunction to the main function
2 Calling Functions Declared with #define
Change the subfunction <span>add</span> to be declared using <span>define</span>.
// Define subfunction using define
#define add(a,b) ((a)+(b))
<span>main</span> function corresponds to these few lines when calling <span>add</span>, requiring only 4 assembly instructions in total. Since the content of <span>define</span> is expanded during the pre-compilation stage, no additional jump instructions are needed.

From this example, comparing the two methods, the conventional subfunction definition requires about 18 assembly instructions to call the subfunction, while the subfunction defined using define only requires 4 assembly instructions during the call, resulting in a difference of about 14 instructions.
If this subfunction is called 100 times throughout the entire project, the difference amounts to approximately 1400 instructions.
Therefore, when a function is called frequently and the function body itself is not complex, using <span>define</span> to define it can significantly reduce the code size.
Previous Content
SystemVerilog Pitfalls – Logic Initialization Issues
A Brief Introduction to C Language Structure Bit Fields
Methods to Obtain Signal Hierarchy from the Waveform Interface in Verdi
Advanced Operations for Removing Duplicate Lines, Jumping to Specific Columns, and Line Break Settings in Gvim
Advanced Batch Replacement Operations in Gvim
Column Replacement and Vertical Splitting in Gvim