Changing floating point value in exe
Asked Answered
C

1

5

I have a Windows program (exe) that was written in Delphi (Delphi 7 I think) several years ago and that program is still used occasionally. It contains a Single type variable that is multiplied by 0.9 at some point in the code.

I would like to change 0.9 to 0.8 right in the exe file using a hex editor and I need help to find the relevant part of the code to change.

The variable is declared as

private  myValue: Single;

and when a button is clicked a component's edit value gets set to Round(myValue * 0.9)

procedure MyForm.buttonClick(Sender: TObject);
begin
  if button.Down then
    myEditComponent.EditValue := Round(myValue * 0.9);
end;

The value 0.9 is only used once in the whole code so I thought it should be easy to find it. I learnt that the hex representation of 0.9 as a DWORD should be 0x3f666666 but I could not find that value in the exe.

So it's either that I am wrong and 0.9 is represented with a different hex string or the compiler formulated this calculation in a different way (like myValue * 9 / 10 or some other way) or ..

Before you suggest that I recompile the project: I have the source code but it is a large project with several dependencies. The code relies on multiple component pack libraries and dozens of third party components. It could take a couple of days just to get and install an old version of Delhi and register all the components in order to recompile the code. Not something I would like to do for such a small change of an infrequently used program.

If I had Delphi installed I would just write these few lines of code and disassemble / debug it in order to see the assembly code. Having the assembly I could figure out what needs to be changed in the exe. I hope someone could point me in the right direction so I do not have to install an old version of Delphi.

Colophony answered 11/6, 2016 at 16:44 Comment(4)
You did search for it as 66 66 66 3f in the file?Elusion
0.9 cannot be exactly represented. The closest single is indeed 0x3f666666. But how can you know that the constant is stored as a single. The obvious way to work this out is to compile the code yourself and see what is omitted. Try that.Psycho
@SamiKuhmonen Tried it that way too as I was not sure about the byte order.Colophony
@DavidHeffernan I made the (possibly false) assumption that the constant and the variable it is multiplied by is stored as the same type.Colophony
T
9

Floating point literal constants are stored as 80-bit extended in the 32 bit versions of Delphi.

A value 0.9 is stored as 66 66 66 66 66 66 66 E6 FE 3F

For 0.8 replace it with CD CC CC CC CC CC CC CC FE 3F

Tess answered 11/6, 2016 at 18:42 Comment(1)
OK, I found that sequence. Strangely I found two instances. I will change them one by one to see what happens. I have to connect the program to the database server to test the changes and it might take some time before I had a chance to do that. If it works (I believe it will) I will accept your answer.Colophony

© 2022 - 2024 — McMap. All rights reserved.