c - 粘贴 ""Pushl $""和 ""1 5""时出错,未给出有效的预处理 token

标签 c gcc assembly compilation c-preprocessor

我试图编译这个源代码时遇到了这个恼人的错误,但我不知道为什么会发生这些错误。我已经搜索过了,但没有找到任何有用的东西。

源文件

#include <ddk/ntddk.h>
#include <internal/ntoskrnl.h>
#include <internal/ke.h>
#include <internal/i386/segment.h>


#define _STR(x) #x
#define STR(x) _STR(x)


#define IRQ_HANDLER_FIRST(x,y)  \
      void irq_handler_##y (void);   \
       __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
        "pusha\n\t"                     \
        "pushl %ds\n\t"                 \
        "pushl %es\n\t"                 \
        "pushl %fs\n\t"                  \
        "movl  $0xceafbeef,%eax\n\t"      \
        "pushl %eax\n\t"                \
        "movw  $"STR(KERNEL_DS)",%ax\n\t"       \
        "movw  %ax,%ds\n\t"             \
        "movw  %ax,%es\n\t"             \
        "inb   $0x21,%al\n\t"               \
        "orb   $1<<"##x",%al\n\t"       \
        "outb  %al,$0x21\n\t"               \
        "pushl %esp\n\t"                \
        "pushl $"##x"\n\t"              \
        "call  _KiInterruptDispatch\n\t"\
        "popl  %eax\n\t"                \
        "popl  %eax\n\t"                \
        "popl  %eax\n\t"                \
        "popl  %fs\n\t"                 \
        "popl  %es\n\t"                 \
        "popl  %ds\n\t"                 \
        "popa\n\t"                      \
        "iret\n\t")

#define IRQ_HANDLER_SECOND(x,y)  \
      void irq_handler_##y (void);   \
       __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
        "pusha\n\t"                     \
        "pushl %ds\n\t"                 \
        "pushl %es\n\t"                 \
        "pushl %fs\n\t"                 \
        "movl  $0xceafbeef,%eax\n\t"      \
        "pushl %eax\n\t"                \
        "movw  $"STR(KERNEL_DS)",%ax\n\t"       \
        "movw  %ax,%ds\n\t"             \
        "movw  %ax,%es\n\t"             \
        "inb   $0xa1,%al\n\t"               \
        "orb   $1<<("##x"-8),%al\n\t"       \
        "outb     %al,$0xa1\n\t"               \
        "pushl %esp\n\t"                \
        "pushl $"##x"\n\t"              \
        "call  _KiInterruptDispatch\n\t"\
        "popl  %eax\n\t"                \
        "popl  %eax\n\t"                \
        "popl  %eax\n\t"                \
        "popl  %fs\n\t"                 \
        "popl  %es\n\t"                 \
        "popl  %ds\n\t"                 \
        "popa\n\t"                      \
        "iret\n\t")


IRQ_HANDLER_FIRST ("0",0);
IRQ_HANDLER_FIRST ("1",1);
IRQ_HANDLER_FIRST ("2",2);
IRQ_HANDLER_FIRST ("3",3);
IRQ_HANDLER_FIRST ("4",4);
IRQ_HANDLER_FIRST ("5",5);
IRQ_HANDLER_FIRST ("6",6);
IRQ_HANDLER_FIRST ("7",7);
IRQ_HANDLER_SECOND ("8",8);
IRQ_HANDLER_SECOND ("9",9);
IRQ_HANDLER_SECOND ("10",10);
IRQ_HANDLER_SECOND ("11",11);
IRQ_HANDLER_SECOND ("12",12);
IRQ_HANDLER_SECOND ("13",13);
IRQ_HANDLER_SECOND ("14",14);
IRQ_HANDLER_SECOND ("15",15);

然后我得到了类似的东西

ke/i386/irqhand.c:25:9: error: pasting ""orb   $1<<"" and ""5"" does not give a
valid preprocessing token
         "orb   $1<<"##x",%al\n\t"       \
         ^
ke/i386/irqhand.c:72:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("5",5);
 ^
ke/i386/irqhand.c:28:9: error: pasting ""pushl $"" and ""5"" does not give a val
id preprocessing token
         "pushl $"##x"\n\t"              \
         ^
ke/i386/irqhand.c:72:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("5",5);
 ^
ke/i386/irqhand.c:14:16: error: pasting ""\n\t.global _irq_handler_"" and ""6""
does not give a valid preprocessing token
        __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
                ^
ke/i386/irqhand.c:73:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("6",6);
 ^
ke/i386/irqhand.c:14:46: error: pasting ""\n\t_irq_handler_"" and ""6"" does not
 give a valid preprocessing token
        __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
                                              ^
ke/i386/irqhand.c:73:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("6",6);
 ^
ke/i386/irqhand.c:25:9: error: pasting ""orb   $1<<"" and ""6"" does not give a
valid preprocessing token
         "orb   $1<<"##x",%al\n\t"       \
         ^
ke/i386/irqhand.c:73:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("6",6);
 ^
ke/i386/irqhand.c:28:9: error: pasting ""pushl $"" and ""6"" does not give a val
id preprocessing token
         "pushl $"##x"\n\t"              \
         ^
ke/i386/irqhand.c:73:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("6",6);
 ^
ke/i386/irqhand.c:14:16: error: pasting ""\n\t.global _irq_handler_"" and ""7""
does not give a valid preprocessing token
        __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
                ^
ke/i386/irqhand.c:74:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("7",7);
 ^
ke/i386/irqhand.c:14:46: error: pasting ""\n\t_irq_handler_"" and ""7"" does not
 give a valid preprocessing token
        __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
                                              ^
ke/i386/irqhand.c:74:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("7",7);
 ^
ke/i386/irqhand.c:25:9: error: pasting ""orb   $1<<"" and ""7"" does not give a
valid preprocessing token
         "orb   $1<<"##x",%al\n\t"       \
         ^
ke/i386/irqhand.c:74:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("7",7);
 ^
ke/i386/irqhand.c:28:9: error: pasting ""pushl $"" and ""7"" does not give a val
id preprocessing token
         "pushl $"##x"\n\t"              \
         ^
ke/i386/irqhand.c:74:1: note: in expansion of macro 'IRQ_HANDLER_FIRST'
 IRQ_HANDLER_FIRST ("7",7);
 ^
ke/i386/irqhand.c:41:16: error: pasting ""\n\t.global _irq_handler_"" and ""8""
does not give a valid preprocessing token
        __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
                ^
ke/i386/irqhand.c:75:1: note: in expansion of macro 'IRQ_HANDLER_SECOND'
 IRQ_HANDLER_SECOND ("8",8);
 ^
ke/i386/irqhand.c:41:46: error: pasting ""\n\t_irq_handler_"" and ""8"" does not
 give a valid preprocessing token
        __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
                                              ^
ke/i386/irqhand.c:75:1: note: in expansion of macro 'IRQ_HANDLER_SECOND'
 IRQ_HANDLER_SECOND ("8",8);
 ^
ke/i386/irqhand.c:52:9: error: pasting ""orb   $1<<("" and ""8"" does not give a
 valid preprocessing token
         "orb   $1<<("##x"-8),%al\n\t"       \
         ^
ke/i386/irqhand.c:75:1: note: in expansion of macro 'IRQ_HANDLER_SECOND'
 IRQ_HANDLER_SECOND ("8",8);
 ^
ke/i386/irqhand.c:55:9: error: pasting ""pushl $"" and ""8"" does not give a val
id preprocessing token
         "pushl $"##x"\n\t"              \
         ^
ke/i386/irqhand.c:75:1: note: in expansion of macro 'IRQ_HANDLER_SECOND'
 IRQ_HANDLER_SECOND ("8",8);
 ^
ke/i386/irqhand.c:41:16: error: pasting ""\n\t.global _irq_handler_"" and ""9""
does not give a valid preprocessing token
        __asm__("\n\t.global _irq_handler_"##x"\n\t_irq_handler_"##x":\n\t" \
                ^
ke/i386/irqhand.c:76:1: note: in expansion of macro 'IRQ_HANDLER_SECOND'
 IRQ_HANDLER_SECOND ("9",9);
 ^

如何解决这个问题?

最佳答案

去掉宏定义中的##。在这种情况下,它们是不必要的且非法的。由于错误表明 ## 运算符的结果必须是单个有效的预处理标记,而 "pushl $""6" 是两个标记。

如果没有##,预处理器将不会尝试将两个字符串粘贴在一起以形成单个标记。它们将保留为单独的字符串标记,并在稍后的翻译阶段合并在一起。

关于c - 粘贴 ""Pushl $""和 ""1 5""时出错,未给出有效的预处理 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585558/

相关文章:

gcc - GNU LD : How to override a symbol value (an address) defined by the linker script specified using -T

C11 _Generic 用法

在 C 中构造字符串

c - 从c中的文件中读取字符串

c++ - vector 异常处理程序中的 EXCEPTION_ACCESS_VIOLATION 异常

c++ - 可以从文件执行机器代码吗?

c - GameBoy Advance Homebrew 开发的语言选择

c++ - pjsip 新调用错误...无法找到默认音频设备 (PJMEDIA_EAUD_NODEFDEV)

c - 为什么 gcc 将一个大常量数组放置到 ".rwdata"部分而不是 ".rodata"部分?

gcc - AARCH64 上的 memcpy 产生未对齐的数据中止异常、ARM GNU 工具链或 newlibc Bug?