PDA

View Full Version : Assembler with Delphi - a few questions



Brainer
14-01-2008, 11:45 PM
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:

function AddThree(A, B, C: Integer): Integer; assembler;

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:

type
TVector2f = array[0..1] of Single;

How do I initialize this array with Assembler's instructions? :?

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

Regards! :)

arthurprs
15-01-2008, 04:01 AM
Same questions here :)

JSoftware
15-01-2008, 05:25 AM
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:

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;

The last two lines in the upper example could also be written as:

mov edi, f
mov result,edi
But that wouldn't have been as fun! :lol:

To call something in assembler. Fx. the upper example

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;

This bit equals:

test(13, 53, $F242191, 123,0,23,24);


You can ofcourse throw stuff around by their names, but knowing the inside things is a bit more fun

Brainer
15-01-2008, 08:17 AM
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:

type
TVector2f = array[0..1] of Single;

function VectorAdd(const V1, V2: TVector2f): TVector2f;


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

Mirage
15-01-2008, 10:13 AM
See
http://dennishomepage.gugs-cats.dk/BASM-filer/BASMForBeginners.htm

Brainer
15-01-2008, 04:32 PM
See
http://dennishomepage.gugs-cats.dk/BASM-filer/BASMForBeginners.htm
Very interesting! :) But I can't understand what is what there. :S I suppose I should have a look for a beginner's Assembler tutorial.

Anyway, thanks! :D

JSoftware
15-01-2008, 05:33 PM
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:

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;

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 :P

Your example with vectorial addition, would probably look something like this:

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 :P
fld [v1+4]
fld [v2+4]
faddp st(1),st
fst [result+4]
end;


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
asm
movq mm0, [v1]
movq mm1, [v2]
pfadd mm0, mm1
movq [result], mm0
emms
end;

waran
15-01-2008, 10:00 PM
You should use SSE instead. Works on Intel and AMD :D

Brainer
15-01-2008, 10:05 PM
I've only started my adventure with Assembler. I don't know what SSE or FPU are, yet. :?

chronozphere
16-01-2008, 09:00 AM
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 ;)

Brainer
16-01-2008, 09:40 AM
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....

I started playing with SSE and it seems pretty easy to learn. ;) Do you know where I can find any tutorials (or mnemonics list)?

chronozphere
16-01-2008, 10:12 AM
No sorry.. i've never done anything with it. Only know what it is. :)

Ask google (http://www.google.nl/search?hl=nl&client=firefox-a&channel=s&rls=org.mozilla%3Anl%3Aofficial&hs=mB5&q=assembly+SSE+tutorials&btnG=Zoeken&meta=) ;)

imcold
17-01-2008, 10:06 AM
You can find a list of sse1/2 instructions wikipedia (http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) or in intel/amd manuals - for example here http://www.intel.com/products/processor/manuals/index.htm. Some sse/mmx tutorials are here (http://www.angelcode.com/dev/mmx/mmx.asp) and here (http://www.tommesani.com/SSEPrimer.html).

imcold
17-01-2008, 10:14 AM
You can find a list of sse1/2 instructions wikipedia (http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) or in intel/amd manuals - for example here http://www.intel.com/products/processor/manuals/index.htm. Some sse/mmx tutorials are here (http://www.angelcode.com/dev/mmx/mmx.asp) and here (http://www.tommesani.com/SSEPrimer.html).
It's quite easy to learn, indeed... but sse usage often requires changes in non-sse code too (data alignment, suitable structures) and parallelization of some algorithms is not quite easy. On the other side, it's often easier to use SSE instructions instead of FPU instructions to work with floating point values, and a bit faster, too.

imcold
17-01-2008, 10:14 AM
You can find a list of sse1/2 instructions wikipedia (http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) or in intel/amd manuals - for example here http://www.intel.com/products/processor/manuals/index.htm. Some sse/mmx tutorials are here (http://www.angelcode.com/dev/mmx/mmx.asp) and here (http://www.tommesani.com/SSEPrimer.html).
It's quite easy to learn, indeed... but sse usage often requires changes in non-sse code too (data alignment, suitable structures) and parallelization of some algorithms is not quite easy. On the other side, it's often easier to use SSE instructions instead of FPU instructions to work with floating point values, and a bit faster, too.

imcold
17-01-2008, 10:15 AM
You can find a list of sse1/2 instructions wikipedia (http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) or in intel/amd manuals - for example here http://www.intel.com/products/processor/manuals/index.htm. Some sse/mmx tutorials are here (http://www.angelcode.com/dev/mmx/mmx.asp) and here (http://www.tommesani.com/SSEPrimer.html).
It's quite easy to learn, indeed... but sse usage often requires changes in non-sse code too (data alignment, suitable structures) and parallelization of some algorithms is not quite easy. On the other side, it's often easier to use SSE instructions instead of FPU instructions to work with floating point values, and a bit faster, too.

JSoftware
17-01-2008, 12:10 PM
C-c-c-combobreaker!! :P

imcold
18-01-2008, 09:32 AM
damn, it said "General Error" on posting >_< lmao