program asmpas;

{$APPTYPE CONSOLE}
{$L asmmodule.obj}

uses
  SysUtils;
      var x,var1:integer;

begin
  var1:=666;
  writeln(var1);

  myasm();        //ERROR NOT FOUND!
  writeln(var1);
end.

----

.386
.model flat, stdcall  ; 32 bit memory model
option casemap :none  ; case sensitive
    extern  var1:dword

.data
align 4
   
.code

myasm proc public
mov edx,7
mov var1,edx
        ret
myasm endp

end

---

Please help.

Thanks.
Posted on 2008-01-21 14:35:49 by sittingduck
You have linked the object file with your project but you also need a prototype for your function. I have written an entire chapter about Assembly and Object Pascal in my book Object Pascal Phreak. Here is an example:

Suppose this is your assembly file:

PUBLIC Sum
CODE SEGMENT PARA PUBLIC 'CODE'
  ASSUME  CS:CODE
  Sum PROC NEAR
    PUSH    BP
    MOV    BP , SP
    XOR    DX , DX
    MOV    AX , WORD PTR
    ADD    AX , WORD PTR
    ADC    DX , DX
    POP    BP
    RET    0004h
  Sum ENDP
CODE ENDS
END


You assemble it in a file called ASMSUM.OBJ for example. Then this should be your pascal program:

Program Program26;
{$L ASMSUM.OBJ}
Function Sum (Word1, Word2 : Word) : LongInt; Near; External;
(* -------------- *)
begin
WriteLn(Sum($FFFF,Word(Not(0))));
ReadLn;
end.


See the prototype? That's how your compiler will know what to refer to in the object file. If you need further help, just post back here. Before I forget, Delphi rules (stands up and applauses. Also raises the stars and stripes and sings the national anthem. Then goes Amen and sits down drinking the rest of his cappuccino).
Posted on 2008-01-21 17:05:01 by XCHG
If you use tasm you only need the prototype as XChG said, but If you are using masm you need to convert object files first.
MASM
ml /nologo /c /omf Something.asm
OMF2D by EliCZ - [defendion.com/EliCZ/export.htm]
omf2d Something.obj Something.obj
DELPHI
{$L Something.obj}
Posted on 2008-01-21 20:36:27 by drizz
Agner fog's objconv also seems to be a favorite among some people..
.
Posted on 2008-01-22 01:15:47 by f0dder
Yaiii, most excellent guys :D
I don't know where i'll be without you guys, but i won't be smelling too good that for sure. :lol:

But are you sure about the offsets:
(SetVar works but not Sum)


.386
.model flat,stdcall
option casemap :none

extern var1:dword
code segment para public 'code'

public sum
  sum proc near
    push    ebp
    mov    ebp,esp
    xor    edx,edx
    mov    eax,6
    add    eax,4
    adc    edx,edx
    mov    var1,eax  ; don't work - var1 & return value points to the sky....
    pop    ebp
    ret    0004h
  sum endp


public  setvar
  setvar proc near
    mov    eax,666h
    mov    var1,eax
    ret
  setvar endp

code ends
end
-----

Function Sum (Word1, Word2 : Word) : LongInt; Near; External;
Function SetVar (): LongInt; Near; External;

/Mike
Posted on 2008-01-22 06:11:58 by sittingduck

Function Sum (Word1, Word2 : Word) : LongInt; Near; External;
Function SetVar (): LongInt; Near; External;


if you don't specify calling convention, Delphi convention (register call) is used. So either change the prototype or change the asm source.

------1 way

;;Function Sum (Word1, Word2 : LongInt) : LongInt; Near; External;
;;Function SetVar (): LongInt; Near; External;
.386
.model flat,stdcall
option casemap :none

extern var1:dword
code segment para public 'code'

public sum
  sum proc near
    add     eax,edx
    mov    edx,0
    adc     edx,edx
    mov     var1,eax   ; don't work - var1 & return value points to the sky....
    ret
  sum endp


public  setvar
  setvar proc near
    mov     eax,666h
    mov     var1,eax
    ret
  setvar endp

code ends
end


------2 way

;;Function Sum (Word1, Word2 : LongInt) : LongInt; Near; External; stdcall; (* <---- *)
;;Function SetVar (): LongInt; Near; External;
.386
.model flat,stdcall
option casemap :none

extern var1:dword
code segment para public 'code'

public sum
  sum proc stdcall Word1, Word2
    xor     edx,edx
    mov     eax,Word1
    add     eax,Word2
    adc     edx,edx
    mov     var1,eax   ; don't work - var1 & return value points to the sky....
    ret
  sum endp

public  setvar
  setvar proc near
    mov     eax,666h
    mov     var1,eax
    ret
  setvar endp

code ends
end


Posted on 2008-01-22 09:29:03 by drizz
I think my post about Object Pascal brought more confusion to this post rather than shedding some light on it. I'm sorry about that ;)
Posted on 2008-01-22 19:20:57 by XCHG

// Stdcall
Function Sum2(x,y:integer):integer; Near; stdcall; External;

Thank you very much, now all works.

Posted on 2008-01-23 07:34:56 by sittingduck