?
Solved

Passing const strings to asm routine

Posted on 1997-09-09
4
Medium Priority
?
495 Views
Last Modified: 2011-08-18
In a class called TShareBlock, with members

ptr : Pointer;
hMem : PChar;
FSize : Integer;

There is a method Add which increments the ptr member by n bytes after doing some range checking. (the add method source is shown below)

Should the range check fail, Add preempts the _RaiseException routine, which merely raises a new exception with the error string passed into it.
_RaiseException is not a member function. It is declared in the same scope as the class itself.

My problem is that the assembler add routine needs to call _RaiseException with a constant string. How do I pass the constant string into _RaiseException.

SMemError is declared in the implementation section of the unit as a const

const
        SMemError = 'Memory Overrun in Share Block.'

When SMemerror is declared as a var (as opposed to const) in the unit,
_RaiseException collects the variable (in EAX) correctly, and shows the
exception string.
when SMemError is declared as a const there is an access violation in the
_RaiseException code.

How are const strings passed into procedures using the inline assembler in
Delphi 3.00?

Note that the declaration of the _RaiseException routine can be _RaiseException ( const s : String) or.
_RaiseException ( s : String ) the const here makes no difference to the success of the asm call.

Awaiting your kind resposes.
Regards
Craig


(* Code snippits *)
Procedure TShareBlock.Add( n : Integer );
        begin
                asm
                MOV  ECX, [EAX].&ptr
                ADD  ECX, EDX
                MOV  EDX, [EAX].hMem
                ADD  EDX, [EAX].FSize
                CMP  EDX, ECX
                JAE  @@Z
                MOV  EAX, SMemError
                CALL _RaiseException       // Raise Exception
                JZ   @@E
            @@Z:
                MOV  [EAX].&ptr, ECX
            @@E:
        end;
end;

Procedure _RaiseException(const s : String );
// Collects s in EAX, as it is not a member procedure, otherwise it would
// be in EDX.
        begin
                raise Exception.Create(s);
        end;
0
Comment
Question by:cmain
[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
  • 2
  • 2
4 Comments
 
LVL 1

Author Comment

by:cmain
ID: 1344543
Edited text of question
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 340 total points
ID: 1344544
Untyped constants are effectively treated as textual replacement by the compiler. That is why it doesn't work.

If you define the SMemError to be a constant string i.e :
const
  SMemError : string = 'memory ...';

Then you can use
  Mov EAX , SMemError.

Otherwise the compiler doesn't really know what SMemError is, so the assignment to EAX gives you a pointer that is only valid at compile time, or something ...




0
 
LVL 1

Author Comment

by:cmain
ID: 1344545
Great Answer,
I am suitably impressed.

One thing I would like to add though.
Your textual replacement justification for your answer raises an interesting point.
The delphi help file documentation shows an example of a local constant (admitedly an integer constant) that is referenced by it's name in the assembler. In this case it cannot be mere textual replacement?

I have tested my code with your example, and it works perfectly.
Your answer was excellent. Thanks.

0
 
LVL 12

Expert Comment

by:andrewjb
ID: 1344546
Hmmm. I noticed that, too. It isn't quite textual replacement.

If you've an untyped string constant, then you seem to get a spurious pointer when you assign it in assembler. I'd guess that the string is stored somewhere for the compilation process only, so it doesn't work when you run the program.

Who knows how Borland think ?!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

649 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