Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using delphi progedures in assembly

Posted on 1999-06-28
11
Medium Priority
?
159 Views
Last Modified: 2010-04-06
Hi,
i'm tring to optime a delphi source with basm.
i need to call a function that has 3 parameters and returns a value.
The parameters are:
integer,string,record
and it returns a longint.

How do i do this.

Thanks
                LR
0
Comment
Question by:lfrodrigues
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 100 total points
ID: 1384944
When you work with ASM, the first 3 parameters and stored in EAX, EDX and EBX, in this order. Other parameters will be stored in the data segment and must be referenced to by ESP and EBP.

The result is returned in EAX.

In your specific case, for working with string, you must copy EDX into EDI and/or ESI (I beleive you already know how to work with strings using Assembly), but I won't cover that since that goes much beyond the scope of this answer.

As of the record parameter (EBX), you can use it right away:

function MyAsmFunction(ParInt : integer; ParStr : string; ParRec : TRecord) : longint;
asm
  add EAX, ParRec.Field2;
end;

In the example above, Result will be ParInt (=EAX) + ParRec.field2. Always remember that EAX is both the first parameter AND the resulting value of the function.

Yours,

Alex
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1384945
simonet: The delphi help for the register calling convention mentions that up to four parameters will be passed as register values. Which is the fourth one?

Cheers,

Raymond.

0
 
LVL 15

Expert Comment

by:simonet
ID: 1384946
The 4th parameter and beyond will be available from offsets to ESP, having EBP as base.

Alex

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Lischke
ID: 1384947
oh, oh, oh, oh, ho, Alex, be careful! Read the statement from the Delphi help:

Register use

In general, the rules of register use in an asm statement are the same as those of an external procedure or function. An asm statement must preserve the EDI, ESI, ESP, EBP, and EBX registers, but can freely modify the EAX, ECX, and EDX registers. On entry to an asm statement, BP points to the current stack frame, SP points to the top of the stack, SS contains the segment address of the stack segment, and DS contains the segment address of the data segment. Except for EDI, ESI, ESP, EBP, and EBX, an asm statement can assume nothing about register contents on entry to the statement.
---- end of quote ---

You can never (!) generally assume how paramters are passed to an assembler routine (or any routine for that matter). The register usage depends heavily on the paramters passed. Assume you pass two Double paramters. In this case they are passed on the FPU stack not in registers.

There're rules of thumb, though, which can help getting started (for default calling convention, other conventions might reverse the order paramters are passed):

1) If the first paramter fits in a 32 bit register (byte..integer, pointer) then it is passed in EAX.
2) If the first paramter is an open array then EDX contains the highest index of this array else
3) If the first paramter is an Int64 then it is passed in EAX:EDX else
4) If the second paramter fits in a 32 bit register then it is passed via EDX
5) If the third paramter fits in a 32 bit register then it is passed in ECX (not EBX!)
6) The result is passed also depending on its size (32 bit in EAX, pointers sometimes in EDX, real values in ST(0) of the FPU stack)

Although single values are 32 bit wide they are still passed via CPU stack or FPU stack. NOTE: You should ALWAYS save and restore registers when you modify them (exception: EAX, EDX, ECX). In particular when modifying EDI you will get very, very hard to find AVs as this register is very often used to reference classes or window procedures.

Generally speaking, there's no default way how paramters are passed. lfrodrigues, I recommend that you first create your function with correct paramters but empty body. Then call it from a test routine and use the CPU window to see how the paramters are passed to the function. Depending on that you can access them when implementing the body. BTW: Instead of using ESP:[EBP] combinations you can use

  LEA ECX, [MyVar]

to get a reference to the variable (local, global or paramter). Additionally, you can use constructs like:

type
  TMyRecord = record
    a, b: Integer;
  end;


  LEA ECX, [MyRecordVariable]
  MOV EAX, ECX.TMyRecord.a

to access record members. Working with strings is a bit more complicated and I recommend using a PChar instead. Then you don't have to worry about short strings, long strings etc. (but DBCS strings will still need much extra work).

Ciao, Mike
0
 
LVL 1

Author Comment

by:lfrodrigues
ID: 1384948
Could you also please tell me how to load the stings into a register.(i'm only learning how to use basm), do i usr ofsset???
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1384949
If you don't have already the string reference passed in one of the registers (you can check this with the CPU window) then simply use

  PUSH ESI
  LEA ESI, [MyString]
  :
  POP ESI

This works for both long strings and PChars. Actually all long strings are automatically terminated by a #0 to indicate their end and the type cast PChar(MyLongString) actually returns the start address of the string, nothing else (so there's almost no time penalty to convert to PChar). Native long strings have an additional field just before the actual string which denotes the length of the string. This avoids scanning through the string when searching substrings etc. This value is located at [ESI - 4] (to use the sample above).

What are you going to do with the string?

Ciao, Mike
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1384950
What's happening here??? This is the third time this week that I gave the correct answer, but someone other collected the points. This does really not motivate to answer any further question!

Very distracted, Mike
0
 
LVL 1

Author Comment

by:lfrodrigues
ID: 1384951
Hey,
i only gave a pontuadion of d to simonet.
If you had qritten a anwser insted of a comment i would credit you
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1384952
:-) You haven't fully understood the system here so it seems. I CAN'T provide an answer when another one is waiting for grading. If you want to give the points not to the one who answered the question at first, but someone else, then you have first to reject the given answer and post a comment asking the other for a new answer.

That's the way here at E-E. And if you want further answers in the future then you should a little bit more carful.

Ciao, Mike
0
 
LVL 15

Expert Comment

by:simonet
ID: 1384953
lfrodrigues, I agree with Lischke here.

***YOU*** don't understand how things work here.

If you don't want an specific answer, reject it. I'd rather have my answer rejected than get a lousy D. You could have rejected my answer and asked Lischke to post his comment as an answer.

Now, before reject an answer, you must be absolutely sure that it doesn't answer your question somehow. Besides, case you reject an answer, explain why that answer was reject.

Alex
0
 
LVL 1

Author Comment

by:lfrodrigues
ID: 1384954
The answer was not complete but it answeed my question in some way. So i accepted it.

Sorry Lischke
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question