Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an assembly language function causes "floating point stack check" exception

Executing following code:

function ABCD32(Value: Cardinal): Single; register;
asm
  BSWAP  EAX
end;

function HexToFloat(hexValue: string; fmt: THexFloatFormat): Single;
var
  c: Cardinal;
  Err: Integer;
begin
  Result := NaN;
  c := HexToCardinal(hexValue, Err); //DCBA format
  if Err <> 0 then Exit();
  case fmt of
    hfABCD: Result := ABCD32(c); //Here, after return from ABCD32
    hfBADC: Result := BADC32(c);
    hfCDAB: Result := CDAB32(c);
    hfDCBA: Result := DCBA32(c);
  end;
end;

causes a run-time errror:

Project HexFloat.exe raised exception class $C0000092 with message 'floating point stack check at 0x004e9903'.

What is this and how to handle it?

Update

Here is the CPU window output:

HexFloat.dpr.162: hfABCD: Result := ABCD32(c);
004E98F8 8B45F0           mov eax,[ebp-$10]
004E98FB E894FFFFFF       call ABCD32
004E9900 D95DF4           fstp dword ptr [ebp-$0c] //WTF?
004E9903 9B               wait //Exception happens here
004E9904 EB28             jmp $004e992e
like image 331
Paul Avatar asked Sep 10 '18 10:09

Paul


1 Answers

Your function does not respect the ABI. Floating point values should be returned in ST(0) on the x87 unit.

function ABCD32(Value: Cardinal): Single; register;
asm
  BSWAP  EAX
  PUSH   EAX
  FLD    [ESP]
  ADD    ESP,4
end;
like image 138
David Heffernan Avatar answered Oct 31 '22 21:10

David Heffernan