Skip to content

Instantly share code, notes, and snippets.

@pankkor
Last active May 15, 2024 20:12
Show Gist options
  • Save pankkor/edfbde50dcce45152f6458f6f1f1595b to your computer and use it in GitHub Desktop.
Save pankkor/edfbde50dcce45152f6458f6f1f1595b to your computer and use it in GitHub Desktop.
Example of using gcc inline assembly syntax. NOTE: last time I checked clang, didn't support Intel assembly syntax, so AT&T it is.
static void test_asm(u8 *data, u64 bytes) {
#if defined(__aarch64__)
__asm__ volatile (
"cbz %[bytes], 1f\n\t"
"mov x9, #0\n\t"
"0: strb w9, [%[data], x9]\n\t"
"add x9, x9, #1\n\t"
"cmp %[bytes], x9\n\t"
"b.ne 0b\n\t"
"1:\n\t"
: /* no output */
: [data] "r" (data), [bytes] "r" (bytes)
: "memory", "x9");
#elif defined(__x86_64__)
__asm__ volatile (
"testq %[bytes], %[bytes]\n\t"
"je 1f\n\t"
"xorl %%eax, %%eax\n\t"
"0: movb %%al, (%[data],%%rax)\n\t"
"incq %%rax\n\t"
"cmpq %%rax, %[bytes]\n\t"
"jne 0b\n\t"
"1:\n\t"
: /* no output */
: [data] "r" (data), [bytes] "r" (bytes)
: "memory", "rax");
#else
#error Not implemented
#endif // #if defined(__aarch64__)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment