How to avoid dollar prefix in inline assembly?

I’m trying to rewrite my C code to Zig.

But this part with the inline assembler causes problems.

...
asm volatile(
    "movq $0,-%c0(%%rsp)"
    ::
    "i"(sizeof(CalleeRegs) + sizeof(ScratchRegs) + sizeof(uint64_t))
);
...

To compile it I used GCC, which supports the use of the c prefix, which removes the dollar sign before the operand. As far as I know, the Zig compiler uses clang as a frontend for compiling C\C++ and apparently the inline assembler too. Clang does not support such a prefix and does not have a similar alternative.

At the moment, my Zig code looks like this:

...
asm volatile(
    "movq $0,-%[size](%%rsp)"
    ::[size]"i"(size)
);
...

And it does not work:

error: error: <inline asm>:1:2: expected relocatable expression
        movq $0,-$136(%rsp)
        ^

I tried to use the Intel syntax (.intel_syntax), but it seems that it does not support the use of operands for the inline assembler.

Maybe someone has encountered a similar problem and knows a solution, I will be very grateful for any help.

I’m assuming size is comptime-known, otherwise this instruction wouldn’t make any sense. I think what you meant to do was this:

asm volatile(std.fmt.comptimePrint(
  \\ movq $0,-{}(%rsp),
  .{size},
));
4 Likes

Wow, didn’t know there was a functionality to format string literals at compile time. Looks like this is exactly what I need.

The only thing that confuses me is that the code is getting more complicated, I mean it looks like a patch and not a good solution.

Anyway, thanks for your answer.

1 Like