Solved

Passing const strings to asm routine

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

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!

Join & Write a Comment

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…
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 video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

10 Experts available now in Live!

Get 1:1 Help Now