Reverse Engineering challenge #60.

Tags: X86 FPU ASM L1 .

This piece of code...


#include <stdio.h>

double d_max (double a, double b)
{
	if (a>b)
		return a;

	return b;
};

int main()
{
	// test
	printf ("%f\n", d_max (1.2, 3.4));
	printf ("%f\n", d_max (5.6, -4));
};

... is compiled by optimizing GCC 4.8.1 into the following piece of 32-bit x86 assembly code:


	fld	QWORD PTR [esp+4]
	fld	QWORD PTR [esp+12]
	fxch	st(1)
	fucomi	st, st(1)
	fcmovbe	st, st(1)
	fstp	st(1)
	ret

Try to eliminate the FXCH instruction, and test it.

More challenges: challenges.re; about solutions: challenges.re/#Solutions.