Menu

Memory Safe Inline Assembly

fil-c.org·4 months ago
#EMC761Yz
#undef#assembly#inline#instructions#memory
Reading 0:00
15s threshold

Since Fil-C 0.685, memory-safe inline assembly is supported on both X86_64 and ARM64.

GCC and clang both support an incredibly powerful inline assembly syntax. For example, on X86_64:

unsigned rotate(unsigned x, unsigned char c) { asm("roll %1, %0" : "+r"(x) : "c"(c) : "cc"); return x; }

Instructs the compiler to emit assembly based on the roll %1, %0 template, where %1 is filled in with %cl , %0 is filled in with whichever register holds x , and c is moved into the %ecx register just before the roll instruction. Additionally, the compiler is told that the instruction will change the value of x and change the value of control flags.

The same kind of thing works on ARM64:

unsigned rotate(unsigned x, unsigned c) { asm("ror %w0, %w0, %w1" : "+r"(x) : "r"(c)); return x; }

Instructs the compiler to emit assembly based on the ror %w0, %w0, %w1 template, where %w0 is filled in with the w register (the 32-bit view of an x register) that holds x , and %w1 is filled in with the w register that holds c .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.