Skip to content

Instantly share code, notes, and snippets.

@op06072
Created January 1, 2024 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save op06072/b92689d61b0ddba355e649d058218a52 to your computer and use it in GitHub Desktop.
Save op06072/b92689d61b0ddba355e649d058218a52 to your computer and use it in GitHub Desktop.
Test for showing bug of Rosetta2
#include <stdint.h>
#include <stdio.h>
int main() {
uint64_t dividend = 0x7c0f45f1;
uint64_t divisor = 0x52e0a6998;
uint32_t quotient, remainder;
__asm__ volatile("mov %%rsp, %%r11\n\t"
"mov %%rbp, %%r12\n\t"
"mov $0x79216054, %%rbp\n\t"
"mov $0, %%rdx\n\t"
"mov %2, %%rsp\n\t"
"mov %3, %%rax\n\t"
"sub $0, %%rsp\n\t"
"mov $0x000000000000004C, %%rcx\n\t"
// section start
"dec %%rdx\n\t"
"neg %%r9d\n\t"
"rorq %%cl, -0x79216054(%%rsp,%%rbp,1)\n\t"
"bswap %%r9d\n\t"
"btc %%edx, %%ebp\n\t"
"cdq\n\t"
"divl 4(%%rsp,%%rdx,1)\n\t" // Execute the div instruction
"neg %%r9d\n\t" // no effect
"cwd\n\t" // <- fucks up divl
"dec %%r9d\n\t" // no effect
"cwd\n\t" // <- fucks up divl
// section end
"movl %%eax, %0\n\t"
"movl %%edx, %1\n\t"
"mov %%r12, %%rbp\n\t"
"mov %%r11, %%rsp"
: "=r"(quotient), "=r"(remainder)
: "r"(&divisor), "r"(dividend)
: "rax", "rdx", "rsp");
printf("Quotient: 0x%x, expect 0\n", quotient);
// if you comment out cwd, you get 0x7c0f45f1 (and cwd should gives 0x7c0f0000)
printf("Remainder: 0x%x, expect 0x7c0f0000\n", remainder);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment