Recent

Author Topic: Running 32-bit assembler code on 64-bit lazarus (Linux)  (Read 6743 times)

MainMeat

  • New Member
  • *
  • Posts: 18
Running 32-bit assembler code on 64-bit lazarus (Linux)
« on: November 16, 2012, 02:35:20 pm »
Hi Everyone,

I have a function from the faststrings.pas unit that I use within my code. (http://www.swissdelphicenter.ch/torry/showcode.php?id=1857)

 I want to try and compile my code on a 64-bit lazarus platform but this function causes an Exception when called. Compiling on a 32-bit version of Lazarus and running the code has not issues. My assembler knowledge is somewhat minimal and I was wondering if someone can please guide me as to what I need todo to get this section of code to compile and run on a 64-bit CentOS linux platform:

Code: [Select]
procedure FastCharMove(const Source; var Dest; Count : Integer);
begin
  {$ASMMODE intel}
  asm
  //Note:  When this function is called, the parameters are passed as follows
  //ECX = Count
  //EAX = Const Source
  //EDX = Var Dest

        cmp   ECX,0
        Je    @JustQuit

        push  ESI
        push  EDI

        mov   ESI, EAX
        mov   EDI, EDX

    @Loop:
        Mov   AL, [ESI]
        Inc   ESI
        mov   [EDI], AL
        Inc   EDI
        Dec   ECX
        Jnz   @Loop

        pop   EDI
        pop   ESI
    @JustQuit:
  end;
end;


Any help is appreciated as always

Bart

  • Hero Member
  • *****
  • Posts: 5677
    • Bart en Mariska's Webstek
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #1 on: November 16, 2012, 03:36:51 pm »
I do not have an anwer, but a question: Is this faster than standard fpc system.move()?

Bart

MainMeat

  • New Member
  • *
  • Posts: 18
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #2 on: November 16, 2012, 03:52:24 pm »
Bart,

This code comes from Kylix days where MidStr was slower and this assembler function did make a significant difference in copying data within strings.

Do you know if MidStr in FreePascal is improved - because then I simply continue using it and need not worry about assembler code maintenance.

In the meantime, if there are any assembler knowledge out there, please feel free to give your inputs!


Bart

  • Hero Member
  • *****
  • Posts: 5677
    • Bart en Mariska's Webstek
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #3 on: November 16, 2012, 04:38:23 pm »
I do know that in Delphi many asm procedures/functions have been replaced by plain pascal, without speed loss (in many cases speed gain), probably because the compiler generates better optomized asm code...

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5677
    • Bart en Mariska's Webstek
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #4 on: November 16, 2012, 05:11:56 pm »
Just tested FastCharMove against System.Move on win32 (fpc 2.6.0).
Moving 1024*1024*256 bytes:

FastCharMove: 328 (TickCounts)
System.Move: 78 (TickCounts)

I would ditch the FastCharMove.
As an advantage System.Move is portable to all supported OS's and architectures.

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #5 on: November 16, 2012, 06:07:01 pm »
The function assumes 32-bit (Object Pascal / Delphi) fastcall calling convention, which is why it crashes on 64-bit. I don't have any document pointing to the corresponding ABI but AFAIK 64-bit has only 1 convention.

My test using both Windows.GetTickCount and SysUtils.Now + DateUtils.MilliSecondSpan shows the same result as Bart. Indeed System.Move is faster than this FastCharMove, with portability as a bonus.

Rekumkacz

  • New Member
  • *
  • Posts: 21
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #6 on: June 26, 2015, 09:03:08 am »
And now for something completely different.

In the code, I want to reuse are registers esi and edi . The program compiles, but appears
"access violation" error

How to eat this cake?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #7 on: June 26, 2015, 09:55:26 am »
Does your code restore the values esi and edi held before you filled them with different values?

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Running 32-bit assembler code on 64-bit lazarus (Linux)
« Reply #8 on: June 26, 2015, 10:27:01 am »
It's dereferencing a 32bit pointer. If your pointer is pointing to 0xFFFFFFFF12345678 you will be trying to read 0x0000000012345678.

You eat the cake by either using Pascal or learning assembly :P

 

TinyPortal © 2005-2018