0

I ran into this issue using 64-bit inline assembler in Delphi XE3 that I don't understand.

I tried this, and it works on both 32-bit and 64-bit

function test(a, b: integer): integer; assembler; register;
asm
  mov eax, a
  add eax, edx
end;

However, this only works on 32-bit but not 64-bit, in 64-bit it compiles, but did not return correct result of the sum of two integers.

function test(a, b: integer): integer; assembler; register;
asm
  add eax, edx
end;

I know previous FPU code such as FLD, STP works on 32-bit but it will give compilation error on 64-bit compiler. Any idea how to handle floating numbers in 64-bit ?

PhiS
  • 4,540
  • 25
  • 35
justyy
  • 5,831
  • 4
  • 40
  • 73
  • 1
    "Could any one give me a quick tutorial" is not a good Stack Overflow question. What's more the code in the question uses 32 bit integer units, but you are actually interested in FPU code. Which is radically different in 64 bit. My strongest advice is to port all your assembler code to Pascal and let the compiler do the heavy lifting. – David Heffernan Mar 22 '13 at 14:08
  • Actually, pal! when you see thios things you just have to open CPU window and compare in which registers are parameters going in. If you work with assembler - then debuugging in assembler is your home! If not - then you'd better ask someone who knows what CPU, registers and assembler are. – Arioch 'The Mar 22 '13 at 14:14
  • @Arioch'The Trial and error in the debugger is not the way to learn the MS x64 ABI. The official documentation is how to do it. – David Heffernan Mar 22 '13 at 14:16
  • BTW, there are bugs in XE2 x64 asm and some of them still are not fixed in XE3. Especially including using 32-bit registers. So, if you want to port asm code to x64 - start with making a LOT of DUnit tests and really grasp a bit of asm. If you started using x64 asm in Delphi you can not be sure it would generate correct code. Usually yes. But sometimes not. – Arioch 'The Mar 22 '13 at 14:17
  • @David - official documentatio nmay err too, especially EMBA one. And looking at compiler output is nothing close to trial and error. It is just looking and seeing how it is implemented. You may stretch it to name "reverse engineering" if you like, but t&e has nothing common with it. – Arioch 'The Mar 22 '13 at 14:19
  • @Arioch'The Emba don't document the MS x64 ABI. I'm not sure why you want to use Emba documentation for that. And the official documentation for the MS x64 ABI does not err. And no, trial and error should never be your main source of information. – David Heffernan Mar 22 '13 at 14:21
  • @David http://docwiki.embarcadero.com/RADStudio/XE3/en/Program_Control http://docwiki.embarcadero.com/RADStudio/XE3/en/Procedures_and_Functions http://docwiki.embarcadero.com/RADStudio/XE3/en/Calling_Procedures_and_Functions I cannot see here nothign about RCX or RAX, nor any link to MSDN page with complete specifications of *Delphi* compiler. So perhaps you can spend quite a time and google inside DocWiki a carefully hidden fine-print referencing MS Win64 convention then google it then... Well. You can do the same by looking once or twice into Debug-CPU window. – Arioch 'The Mar 22 '13 at 14:23
  • @Arioch'The Indeed. Exactly as I said, Emba don't document the MS x64 ABI. Why would they? There is a single ABI on MS x64. If you think you can learn it comprehensively by trial and error, then that's fine. I don't think that's the best way to learn anything. – David Heffernan Mar 22 '13 at 14:25
  • > Emba don't document the MS x64 ABI Well, maybe i also should look in IBM docs for EMBA compiler specs ? Or maybe Oracle/Sun ones ? Or GCC docs? those compielr makers are nothing worse than Microsoft. The ONLY official documentation for EMBA compiler is EMBA documentation. – Arioch 'The Mar 22 '13 at 14:25
  • @David EMBA should document their compiler. If they want to reference MSDN - then they should DO it. And then i would open DocWiki page, click over MSDN link and happily read Microsoft docs how does Win64 ABI passes AnsiString and TCustomForm. Hmm, maybe you saw that link at the DocWiki pages i mentioned above ? And maybe you know where does Micorosoft document passing AnsiString in Win64 ? BTW, EMBA did documented OffsetRect despite this being function from Win32 API. Why did they, if they should not repeat MSDN by your words? – Arioch 'The Mar 22 '13 at 14:30
  • @Arioch'The It doesn't sound as though you care about the documentation. You appear to think that trial and error is better. – David Heffernan Mar 22 '13 at 14:31
  • !st time you could misread. Now you are lying. I did not suggested trial and error. And i told you it already. You pretend you cannot tell reading example code from blind random trials, but i don't buy it. – Arioch 'The Mar 22 '13 at 14:33
  • Yes, giving quality of EMBA docs i tend to recheck everything i found there, and i tend to never wonder if there's nothing to find. And when i needed to port functions liek that - i surely started with DocWiki, and surely found NOTHING there, just like you. And then i started debugging and googling. And finally i got that very WIKIPEDIA page that MBo linked below. But it is not my fault that EMBA did not documented their compiler. WIKIPEDIA, not even MSDN. – Arioch 'The Mar 22 '13 at 14:36
  • @Arioch'The Documentation is here: http://msdn.microsoft.com/en-us/library/ms235286.aspx – David Heffernan Mar 22 '13 at 14:47
  • @DoctorLai it's not an inherently bad question but you did not formulate it propely. In particular, you should have detailed what you mean by "not working". – Igor Skochinsky Mar 22 '13 at 15:01
  • Thanks, much better now! – Igor Skochinsky Mar 22 '13 at 15:24
  • 1
    @David nice link, but i don't see any reference to Delphi there. Nor can i find sets, strings, TObject and TCLass at http://msdn.microsoft.com/en-us/library/02c56cw3.aspx And then i Google for `link:msdn.microsoft.com/en-us/library/ms235286.aspx site:docwiki.embarcadero.com` and... find nothing again. There seems to be no DOCUMENTATION that Delphi programs should rely on that MSDN page. The only OFFICIAL documentation for Delphi is the one at DocWiki. If there is none, then there is none. And reading unrelated to Delphi MSDN is no less "trial and error" like looking into Delphi debugger. – Arioch 'The Mar 22 '13 at 15:26
  • 1
    @David, Sorry for being harsh above, but finding in my mouth someone's words about nonsense i did not wrote, that hurts. – Arioch 'The Mar 22 '13 at 15:27
  • @Arioch'The Which words did I put in your mouth? You suggested learning about the ABI by inspecting the register view under the debugger. That's trial and error in my book. The problem with that approach is that you'll never know whether or not you've found all the corner cases. You'll also find it very hard to understand the stack alignment issues of the x64 ABI. Not to mention table based exception handling. ABI's are complex and I simply disagree with you that inspecting register view under a debugger is an appropriate way to learn them. – David Heffernan Mar 22 '13 at 17:12
  • So "trial" is compiling the function for you, ok. But how in your book reading sample sources or reading reference code qualifies as "error" ? exception handling is hard, but how does it relate to calling convention ? Procedures that complex that their paramlist would exceed free Win64 registers... Those procedures IMHO are target to refactoring anyway, and especially there is very little reason to turn them assembler. ASM sinppets are to be small and simple, and that implies rather restricted params list. Sure, reading documentation is better than reverse engeneering, but EMBA did not do it. – Arioch 'The Mar 22 '13 at 20:49
  • All you need are the MSDN doc that I linked to and the knowledge of the Delphi implicit parameters. Self passed as first param. Result passed as final var param. And knowledge that reference types are passed as pointers. – David Heffernan Mar 22 '13 at 21:48
  • MSDN docis not related to Delphi. Binding them is that "trial and error" you talked so against. Same goes for applying x86 Delphi knowledge to x64 Delphi. – Arioch 'The Mar 26 '13 at 07:45
  • btw, FPU asm code does compile in Delphi x64, but my recommendation is that you only touch this if you know exactly what you are doing – PhiS Apr 02 '13 at 10:06

1 Answers1

7

It is because 64-bit system uses own calling convention, and these parameters are in RCX and RDX registers. More explanations in MSDN.

About handling floating numbers - read Intel developer documentation.

MBo
  • 77,366
  • 5
  • 53
  • 86