Solved

Built-in assembler problem

Posted on 2002-06-28
6
237 Views
Last Modified: 2010-04-04
I'm writing a little graphical effect, and to speed things up a bit, I tried to replace some code by assembler. The problem is: how do you call another function using assembler. I already tried using "call @myFunction" but it says "@myFunction undeclared" or something. I only know the basics of assembler, so keep that in mind :)
0
Comment
Question by:herr_apfelschnitt
6 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7115790
A simple trick:
Debug your application and have a look at the CPU window.
0
 
LVL 7

Accepted Solution

by:
Cynna earned 200 total points
ID: 7115812
herr_apfelschnitt,

> ... I already tried using "call @myFunction"...
Did you try "call myFunction"   ?:)

If your function has arguments, they are stored
in EAX, EDX and ECX in that order.
Function result is stored in EAX.

Simple demonstration:
-------------------------------


function bla(a,b: Integer): Integer;
begin
  Result:=a+b;
end;

function Assem: Integer;
asm
  mov EAX, 8;
  mov EDX, 3;
  call bla;
  mov Result, EAX;
end;

//---------------------------------------

procedure TForm1.Button11Click(Sender: TObject);
begin
  Label1.Caption:=IntToStr(Assem);
end;


0
 
LVL 1

Author Comment

by:herr_apfelschnitt
ID: 7115928
wow! This is the first time something is easier than I thought it would be :)

You wouldn't mind me asking an additional question (or two) would you :) (since 200 pts is quite a lot, and I haven't got too many of them left)
(if you don't know the answer to these, don't worry, you get the points anyway)

1. How do you access the coprocessor's stack (ST(x))?
2. If the function has more than three arguments, are the remaining arguments simply put on the stack?

Thanks


0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:herr_apfelschnitt
ID: 7115931
And to Robert:

I 've never seen a CPU window in Delphi. Could this be because i'm still running Delphi 3?
0
 
LVL 2

Expert Comment

by:bugroger
ID: 7116069
Hi,

how to Enable the CPU window in delphi 3?
-----------------------------------------
Simply go in the registry at:
HKCU\Software\Borland\Delphii\3.0\Debugging
then create a string key named "EnableCPU" and set the value to "1".
It's all.

it's from
http://www.delphipages.com/tips/thread.cfm?ID=10

0
 
LVL 7

Expert Comment

by:Cynna
ID: 7116249
herr_apfelschnitt,

> 1. How do you access the coprocessor's stack (ST(x))?


Example (uses st(0) for floating point operations)
----------------------------------------------------------------

function FloatSubtract(a,b: Double): Double;
asm
 // Load first argument(memory) on top of stack, in st(0):
    fld a;
 // Subtract b(memory) from st(0) and place the result back in st(0):
    fsub b;
 // Pop st(0) to Result(memory)
    fstp Result;
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
  Label1.Caption:=FloatToStr(FloatSubtract(4.5, 2.2));
end;


You can find further info in "Art Of Assembly Language":

* http://webster.cs.ucr.edu/Page_AoAWin/0_AoAHLA.html

The best thing would be to download the whole book.
Then go to  Chapter 11, "Real Arithmetic" (page 595).
Read up, you'll find quite detailed info on what you
seem to be interested in...





> 2. If the function has more than three arguments, are the remaining arguments simply put on the stack?

Generally, yes.
Here is what Delphi Help ("Program control" -> "Parameter passing") has to say about this:

"Under the register convention, up to three parameters are passed in CPU registers, and the rest (if any) are passed on the stack. The parameters are passed in order of declaration (as with the pascal convention), and the first three parameters that qualify are passed in the EAX, EDX, and ECX registers, in that order. Real and method-pointer types do not qualify as register parameters, but all other parameters do. If more than three parameters qualify as register parameters, the first three are passed in EAX, EDX, and ECX, and the remaining parameters are pushed onto the stack in order of declaration. For example, given the declaration

procedure Test(A: Integer; var B: Char; C: Double; const D: string; E: Pointer);

a call to Test passes A in EAX as a 32-bit integer, B in EDX as a pointer to a Char, and D in ECX as a pointer to a long-string memory block; C and E are pushed onto the stack as two double-words and a 32-bit pointer, in that order."




But, you can directly access your arguments and not worry
about this:

Example:
----------------

function MassSum(a,b,c,d,e: Integer): Integer;
asm
  mov EAX, a;
  mov EDX, b;
  add EAX, EDX;
  mov EDX, c;
  add EAX, EDX;
  mov EDX, d;
  add EAX, EDX;
  mov EDX, e;
  add EAX, EDX;
  mov Result, EAX;
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
  Label1.Caption:=IntToStr(MassSum(1,2,3,4,5));
end;

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now