Solved

Passing const strings to asm routine

Posted on 1997-09-09
4
481 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
  • 2
  • 2
4 Comments
 
LVL 1

Author Comment

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

Accepted Solution

by:
andrewjb earned 170 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

23 Experts available now in Live!

Get 1:1 Help Now