Pasting constant into asm code block

394 views Asked by At

I have this C macro:

#define syscall(number) \
({ \
    asm volatile ( \
        ".set noreorder\n" \
        "nop\n" \
        "syscall "#number"\n" \
    );\
})

It works great when I call it with integer:

 syscall(5);

However when I do something like this:

 #define SYSCALL_PUTC 5

 syscall(SYSCALL_PUTC);

I get this error:

Error: Instruction syscall requires absolute expression

How do I work around this? I don't want to have my code littered with magic numbers.

2

There are 2 answers

0
ouah On BEST ANSWER

Use stringification like this:

#define xstr(s) str(s)
#define str(s) #s
#define syscall(number) \
({ \
    asm volatile ( \
        ".set noreorder\n" \
        "nop\n" \
        "syscall "xstr(number)"\n" \
    );\
})

For more info on stringification, see this gcc page:

http://gcc.gnu.org/onlinedocs/cpp/Stringification.html

1
Scott Hunter On

A bit of a hack, but not THAT much: make a copy of your original syscall macro for each syscall number, naming each for that syscall. Thus, you'd have a macro named SYSCALL_PUTC which would expand into the same as syscall(5) would have.