Solved

Built-in assembler problem

Posted on 2002-06-28
6
241 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 127
select query - oracle 16 100
How to build JSON File in Delphi 6 3 34
How to make Sign in, using Clientdataset? 1 16
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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