Solved

Passing const strings to asm routine

Posted on 1997-09-09
4
483 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calling Tcolordialog in dll make the form disappear 8 114
Help on project with Soap 10 54
Delphi application Soap connection 5 105
Magic Software info 18 132
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…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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