how do I call routines made with TurboPascal from TASM? 
Author Message
 how do I call routines made with TurboPascal from TASM?

Does anybody know subj.?

regards
Tobias

--
-------------------------------------------------------------------------
From the weird brain of Tobias Skytte



Sat, 16 Nov 2002 03:00:00 GMT  
 how do I call routines made with TurboPascal from TASM?
Tom Swan's "Mastering Turbo Assembler" book has a chapter "12 - Mixing
Assembly Language With Pascal" on the subject. You better get hold of
this book.

Anyway: I don't think you can write an assembler program that uses TP
routines, because TP doesn't provide  .obj or .lib files containing
them. But you can write a TP program, and link to it assembler object
files which contain calls to TP routines. I mean, "Main" must be
written in TP. Please, forgive me if I make some mistake, because I
have not used TP since a long time, but I think you could do:

Program MyProgram;

BEGIN
  MyAsmMain;
END.

in TP. MyAsmMain would be the "Main" function of your assembly module
(excuse me for using "Main", as in C, but I feel it gives the idea as
no other thing would). You start your program from TP, but transfer
execution to you assembly module.

Then, in the assembly module, do as shown in the following snippet
from Swan's:

SEGMENT CODE byte public
ASSUME cs:CODE ds:DATA
EXTRN Pause:NEAR
PROC MyProc:NEAR
  Call Pause
  ret
ENDP MyProc
ENDS

Here you have called Pause (a TP procedure) from your assembly module.

If you are calling TP functions, remember that result are returned in
ax. For instance, in your assembly module you can write code such as:

Call a_TP_Function      ; ---> AX = result from a_TP_Function

If you need to pass parameters to the Function/Procedure, do that
through the stack according to TP rules.

You can use the TPASCAL memory model from TASM, so that simplified
segment directives may be used, and the end result shall be compatible
with TP memory model.

A limitation you'll have is that you cannot declare initialized
variables in the data segment of the assembly module. Either you make
them uninitialized, or you make them in the code module, and then

push ds
push cs
pop ds
..... ; do something with the variables at the code segment
pop ds

I have never tried this myself. I'm just telling you what I see from
Swan's at first glance.

Manuel Algora



Sun, 17 Nov 2002 03:00:00 GMT  
 how do I call routines made with TurboPascal from TASM?
Quote:
>Anyway: I don't think you can write an assembler program that uses TP
>routines, because TP doesn't provide  .obj or .lib files containing
>them.

Ok. but then theoretically it should be possible to make an exe file from
tp, then run it through a disassembler and then assemble it again to obj
format, and then call it from tasm...? or is there an easier way? are you
sure TP can''t make obj files? even Delphi has an option to create .obj
files. (I don't have TP installed at the moment so cant check)

[snip]
[codesnip]

Quote:
>Here you have called Pause (a TP procedure) from your assembly module.

ok. it looks fairly straight forward (hmm). I just found that EXTERN keyword
in my tasm book also, and that explains a bit. I suppose when I link it
(using tlink) then i just specify all the TP obj files as well as my main
asm obj file and then it should link.. (i hope!)?

Quote:
>If you are calling TP functions, remember that result are returned in
>ax. For instance, in your assembly module you can write code such as:

>Call a_TP_Function ; ---> AX = result from a_TP_Function

ok noted

Quote:

>If you need to pass parameters to the Function/Procedure, do that
>through the stack according to TP rules.

check.

Quote:

>You can use the TPASCAL memory model from TASM, so that simplified

[snippetysnip]
ok. except i don't understand these memory models very well. <g>

Quote:

>I have never tried this myself. I'm just telling you what I see from
>Swan's at first glance.

Ok thanks a lot, it doesn't seem that scary after all!

regards,
Tobias



Sun, 17 Nov 2002 03:00:00 GMT  
 how do I call routines made with TurboPascal from TASM?

Quote:

> >Anyway: I don't think you can write an assembler program that uses TP
> >routines, because TP doesn't provide  .obj or .lib files containing
> >them.
> Ok. but then theoretically it should be possible to make an exe file from
> tp, then run it through a disassembler and then assemble it again to obj
> format, and then call it from tasm...? or is there an easier way? are you
> sure TP can''t make obj files? even Delphi has an option to create .obj
> files. (I don't have TP installed at the moment so cant check)

TP cannot compile to .obj. The procedure you describe is very complicated
and you can better make a small wrapper in your .pas file like this:

{$F+}

procedure assemblerprogram; external;
{$L ASMPROG.OBJ}
{$F-}

begin
  assemblerprogram;
end.

This way your .pas becomes the "main" program.

Bart



Mon, 18 Nov 2002 03:00:00 GMT  
 how do I call routines made with TurboPascal from TASM?


Quote:
>are you  sure TP can''t make obj files?

Units are compiled to .tpu.

This is the output one gets from tpc.exe as ran from the command line
without any arguments:

Turbo Pascal  Version 7.0  Copyright (c) 1983,92 Borland International
Syntax: TPC [options] filename [options]
  -B = Build all units                -L = Link buffer on disk
  -D<syms> = Define conditionals      -M = Make modified units
  -E<path> = EXE/TPU directories      -O<path> = Object directories
  -F<seg>:<ofs> = Find error          -Q = Quiet compile
  -GD = Detailed map file             -T<path> = TPL/CFG directory
  -GP = Map file with publics         -U<path> = Unit directories
  -GS = Map file with segments        -V = Debug information in EXE
  -I<path> = Include directories      -$<dir> = Compiler directive
Compiler switches: -$<letter><state>  (defaults are shown below)
  A+ Word alignment       I+ I/O error checking   R- Range checking
  B- Full boolean eval    L+ Local debug symbols  S+ Stack checking
  D+ Debug information    N- 80x87 instructions   T- Typed pointers
  E+ 80x87 emulation      O- Overlays allowed     V+ Strict
var-strings
  F- Force FAR calls      P- Open string params   X+ Extended syntax
  G- 80286 instructions   Q- Overflow checking
Memory sizes: -$M<stack>,<heapmin>,<heapmax>  (default:
16384,0,655360)

I don't see in there any switch which may allow you to compile to .obj

Delphi allows you to compile Units to C, or C++ .obj files (Project
options | linker | linker output) I would have thought it should be
considered a compiler option, not a linker one ...

DCC32.exe has the switches:

-J: generate .obj file (a C .obj file?)
-JP: generate C++ .obj file

Quote:
> I suppose when I link it
>(using tlink) then i just specify all the TP obj files as well as my main
>asm obj file and then it should link.. (i hope!)?

If you can manage to get .obj files from TP it should be so.

Quote:

>>You can use the TPASCAL memory model from TASM, so that simplified
>[snippetysnip]
>ok. except i don't understand these memory models very well. <g>

That's the good thing about using TPASCAL: you need not know the name
and class of TP segments, in order to declare your assembly segments
in a suitable manner. You just use simplified segments directives, and
TASM takes care of it all. Of course, if you need to have greater
control over the segments declarations, and use the old style SEGMENT
directive, you'll need to know about the TP memory model. Swan's gives
a template with the declarations

IDEAL

SEGMENT DATA word public
  ...
ENDS DATA

SEGMENT CODE byte public
ASSUME cs:CODE, ds:DATA
  ...
ENDS CODE

He further gives the following indications, which may be worth
mentioning:

- TP initializes DS already as a pointer to the global data segment,
so there's no need to do this in the assembly module.

- ES is not preserved between calls to TP routines.

- He says: "the global data segment is a phantom in a compiled Pascal
program, existing only when the program is executed; therefore, you
cant declare preinitialized variables in the external module". He
suggests a way out of this problem by declaring the variables in the
TP program as typed constants. such as

CONST a_byte: byte = 5;

Then you import them to the assembly module by means of EXTRN:

SEGMENT DATA word public
        EXTRN a_byte: Byte
ENDS DATA

- You can link the assembly modules from within a TP program by means
of {$L}. But this directive should appear AFTER the declaration (even
if it's just a forward declaration) of any `TP procedure/function
which may be called from the assembly module. For instance, for the
procedure Pause which I mentioned in a snippet from Swan's, he uses

PROCEDURE Pause; FORWARD;
{$L ANYCODE.OBJ}

where Anycode is the assembly module.

- In the snippet I gave, EXTRN Pause:NEAR was used. If the {$F+}
directive is active, or the procedure is listed in the interface
section of a unit, one would need to do EXTRN Pause: FAR

- You can also access variables of a TP program if you declare them as
EXTRN in the assembly module.

- If you don't use TPASCAL model in the assembly module, you should
use, in the assembly module, as the following snippet indicates:

PROC Share NEAR
        ARG his: WORD, hers: WORD =ArgSize
        ......
        RET ArgSize

to restore the stack at the end of the procedure; whereas, if using
TPASCAL memory model, just put a RET without any operand, as TASM
shall take care of the stack restoration by itself.

- Because TP doesn't have an EXTRN directive, assembly variables
cannot be exported to a TP module, but you could write some
function/procedure in the assembly module, to be called from TP, which
would pass to it the addresses of the assembly variables, and then act
upon them from TP (by assigning them to pointer variables, I guess).

Manuel Algora



Tue, 19 Nov 2002 03:00:00 GMT  
 how do I call routines made with TurboPascal from TASM?
AFAIK Turbo Pascal doesn't allow this.
However, Delphi does have an option to compile
Object Pascal Units to "C .OBJ files" so there's
a small chance you can compile your Pascal
code with Delphi and produce an OBJ file
you can link with your ASM code.  I've only
run the compiler to verify that it produces
an OBJ output, I haven't actually tried to
link the output with anything.
Randy Hyde


Quote:
> Does anybody know subj.?

> regards
> Tobias

> --
> -------------------------------------------------------------------------
> From the weird brain of Tobias Skytte




Tue, 19 Nov 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Need help making device driver call C routine

2. Routine call in Routine

3. Interfacing C routine with CVF routine called from VB

4. tasm doing word allignment?

5. C++ Call to DLL made in LabVIEW Fails the second time it is called

6. IMSL routines: what are they doing?

7. New proram made in tasm, (Not a Virus) please examine

8. TASM routines anyone?

9. Clipper extended and TurboPascal child

10. TurboPascal to Modula 2 translator

11. ASM and TurboPascal 7

12. looking for source of TurboPascal 5.5 or its editor (ED.ASM) /DOS

 

 
Powered by phpBB® Forum Software