I’m working on manually constructing an IDT table in x86 assembly. I have the following macros defined using the C preprocessor in my .S file:
// sets up an entry in the idt for a trap type
#define SETUP_IDT_ENTRY( name, num, istrap, segment, dpl ) \
lea name, %edx; \
movl $(KCODE << 16), %eax; \
movw $0x8e00, %dx; \
lea (idt + (8 * num)), %edi; \
movl %eax, (%edi); \
movl %edx, 4(%edi);
// sample set of args to a call to setup_dt_entry
#define M_DIVIDE _t_divide_ep, T_DIVIDE, 0, KCODE, 0
// the call
SETUP_IDT_ENTRY( M_DIVIDE )
However, gcc complains: error: macro "SETUP_IDT_ENTRY" requires 5 arguments, but only 1 given
I thought that #define’d arguments to #define’d functions were expanded before the function call was evaluated, in which case M_DIVIDE would expand to the five arguments required and SETUP_IDT_ENTRY would be happy. I’ve tried various combinations of parentheses and nothing seems to be working; is there a way to make this work?
Note: I know there are alternate approaches for building IDT’s in x86 assembly, but that’s not the question I’m trying to answer here; I’m just trying to figure out if macros can be expanded as macro arguments.
The arguments themselves are expanded, but the number of arguments must match the macro definition. You’ll need an extra macro to make it work:
More info here and here.