Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Assembler with Delphi - a few questions

  1. #1

    Assembler with Delphi - a few questions

    Hello.

    I want to learn Assembler, but I don't want to use compilers like MASM or something like that. I want to use Assembler with Delphi. Well, let's start off with the first question.

    .: First question :.
    Let's assume I've got a function called AddThree. What should it do? It just sums up three integers given as the parameters. Suppose the function's header is:
    [pascal]
    function AddThree(A, B, C: Integer): Integer; assembler;
    [/pascal]
    How would I know which register contains the address of A, which one contains B, and which one contains C? :?

    .: Second question :.
    Let's assume I've got the TVector2f type declared as follows:
    [pascal]
    type
    TVector2f = array[0..1] of Single;
    [/pascal]
    How do I initialize this array with Assembler's instructions? :?

    Any good tutorials about using Assembler with Delphi are greatly appreciated.

    Regards!

  2. #2

    Assembler with Delphi - a few questions

    Same questions here
    From brazil (:

    Pascal pownz!

  3. #3

    Assembler with Delphi - a few questions

    It all depends on your calling convention

    Most Object Pascal compilers, by default, uses fastcall. The first few parameters go in the registers eax,edx,ecx and the rest goes on the stack.

    cdecl which is the standard convention of C, and stdcall pass all the parameters on the stack in a specific order

    If you just use fastcall, which supposedly is the fastest, then you can use the following example:

    [pascal]function test(a,b,c,d,e,f,g: cardinal): cardinal; assembler;
    asm
    mov edi, eax //edi := a
    mov edi, edx //edi := b
    mov edi, ecx //edi := c
    mov edi, [ebp+20 //d
    mov edi, [ebp+16] //e
    mov edi, [ebp+12] //f
    mov edi, [ebp+8] //g
    mov eax, edi //eax is the return register
    end;[/pascal]

    The last two lines in the upper example could also be written as:
    Code:
       mov edi, f
       mov result,edi
    But that wouldn't have been as fun! :lol:

    To call something in assembler. Fx. the upper example
    [pascal]
    asm
    mov eax, 13
    mov edx, 53
    mov ecx, $F242191
    push 123
    push 0
    push 23
    push 24
    call test
    //do something with eax
    end;
    [/pascal]
    This bit equals:
    [pascal]
    test(13, 53, $F242191, 123,0,23,24);
    [/pascal]

    You can ofcourse throw stuff around by their names, but knowing the inside things is a bit more fun
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  4. #4

    Assembler with Delphi - a few questions

    Wow, thanks a lot! Could you also explain me, how to initialize an array using Assembler? Also, how do I add two vectors using Assembler? I mean:
    [pascal]
    type
    TVector2f = array[0..1] of Single;

    function VectorAdd(const V1, V2: TVector2f): TVector2f;
    [/pascal]

    And again, thank you a lot for your previous reply!

  5. #5

  6. #6

    Assembler with Delphi - a few questions

    Very interesting! But I can't understand what is what there. I suppose I should have a look for a beginner's Assembler tutorial.

    Anyway, thanks!

  7. #7

    Assembler with Delphi - a few questions

    Quote Originally Posted by Brainer
    Wow, thanks a lot! Could you also explain me, how to initialize an array using Assembler? Also, how do I add two vectors using Assembler?
    Arrays are a bit tricky. Static arrays supposedly doesn't keep information about size, while dynamic arrays do

    Initialization of dynamic array:
    [pascal]
    procedure FillArray32(var arr: array of integer; value: integer); assembler;
    asm
    cld //clear direction
    mov edi, eax //eax contains pcardinal(@array)^ so to speak
    mov eax, value
    mov ecx, [edi-4] //get pcardinal(edi-4)^ which contains the same as high(array)
    rep stosd //repeat stosd, ecx times. stosd stores eax in edi, and then increases edi as we cleared the direction flag
    end;
    [/pascal]
    Please note that this code is only a few clocks faster than doing in a for loop. And it's very unsafe. If you call it with an array with length=0 your program will crash

    Your example with vectorial addition, would probably look something like this:
    [pascal]
    type
    TVector2f = array[0..1] of Single;

    function VectorAdd(const V1, V2: TVector2f): TVector2f; assembler;
    asm
    fld [v1] //load v1[0]
    fld [v2] //load v2[0]
    faddp st(1),st //add the two lowest fpu stack elements
    fst [result] //store the stack element in result[0]
    //Figure the rest out yourself
    fld [v1+4]
    fld [v2+4]
    faddp st(1),st
    fst [result+4]
    end;
    [/pascal]

    Nothing ordinary there.. The fpu is a bi**h to work with, or personally I don't like the stack based approach..

    Let's crank up some 3DNow! which should speed things a bit up. The only downside is that it seems to only work on some AMD processors
    [pascal]asm
    movq mm0, [v1]
    movq mm1, [v2]
    pfadd mm0, mm1
    movq [result], mm0
    emms
    end;[/pascal]
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  8. #8

    Assembler with Delphi - a few questions

    You should use SSE instead. Works on Intel and AMD

  9. #9

    Assembler with Delphi - a few questions

    I've only started my adventure with Assembler. I don't know what SSE or FPU are, yet. :?

  10. #10

    Assembler with Delphi - a few questions

    FPU = Floating point unit (a part of the CPU that is responsible for floating point operations)
    SSE = a processor instruction set. Thought it was created by intel. It is an extension to the x86 instructions. SSE instructions can be used to speed up certain things. maybe for fast acces, moving data efficiently, and floating point stuff.... Correct me if i'm wrong
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •