Skip to content

Instantly share code, notes, and snippets.

@joastbg
Created October 4, 2016 20:19
Show Gist options
  • Save joastbg/c00524e8b888bedb9fedd24be008dd52 to your computer and use it in GitHub Desktop.
Save joastbg/c00524e8b888bedb9fedd24be008dd52 to your computer and use it in GitHub Desktop.
Fun with x87 instructions using inline assembly (Intel syntax)
// g++ fun.cc -masm=intel
float fpzero = 0;
unsigned long pfpzero = (unsigned long)&fpzero;
__asm__ volatile("xorps xmm1, xmm1");
__asm__ volatile("fldpi"); // load PI
__asm__ volatile("fchs"); // change sign
__asm__ volatile("fst dword ptr [%0]":"=&r"(pfpzero));
// this should be -3.14159...
std::cout << fpzero << std::endl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment