flat assembler
Message board for the users of flat assembler.

Index > Windows > How to execute COFF OBJ code directly

Author
Thread Post new topic Reply to topic
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 05 Oct 2004, 02:26
Hello masters. Smile
Some one know how is the method to execute directly a function inside an obj file?
I mean, for example, i whant to make a program and call one function inside one or various ms coff obj files and return the results.. I hear theres some winapi functions, called mapfile or something, but i dont see anything clear... Sad

Thanks!

_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 05 Oct 2004, 02:26
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 05 Oct 2004, 10:34
Hi metalfishx,

Have a look at my inc2inc tool packages, they contain examples of calling external functions from static libraries.

http://board.flatassembler.net/topic.php?t=588

_________________
Code it... That's all...
Post 05 Oct 2004, 10:34
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 05 Oct 2004, 15:39
Hello Vortex. Thanks for your reply.
Well, i saw your packages, but i still dont know how to call functions inside obj files dinamically. I want for example, think in an interpreter, well, take in runtime an user input like for example:
myvarint = Callobjfunc("testobj.obj","retsumtest",2,100,120)
In this case i asume the file that have the functions is "testobj.obj" and the function name im trying to call is "retsumtest" with take two parameters "2" and the two parameters are integer type "100,120" and after the funcion return the two integers sumed. For this reason im trying to find how to call one function inside an ms coff object. But i cannot imagine how... complicated huh? Confused

Thanks! Smile
Post 05 Oct 2004, 15:39
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 05 Oct 2004, 17:42
Hi metalfishx,

You are welcome. Here is an example for you. Before, I would like to recommend you that you should keep your procedures in separate source files to avoid some technical problems.

From Asmintro.hlp coming with Hutch's masm32 package:
Quote:

This problem in libraries is called "granularity" and it can be avoided by writing each procedure in its own module unless other procedure are ALWAYS used with it and are not use by any other procedure.


main.asm calling functions from an external file named testobj.asm
Code:
format MS COFF

public start

extrn   ExitProcess:dword
extrn  locate:dword
extrn       StdOut:dword

Include '%fasminc%\win32a.inc'
Include '%fasminc%\macro\if.inc'

section '.data' data readable writeable

msg   db 'Console application',0

section '.code' code readable executable

start:
    stdcall locate,10,1
 stdcall StdOut,msg
  invoke  ExitProcess,0
    


testobj.asm
Code:
format MS COFF

public StdOut
public locate

extrn  GetStdHandle:dword
extrn  SetConsoleCursorPosition:dword
extrn  WriteFile:dword
extrn 'lstrlenA' as lstrlen:dword

Include '%fasminc%\win32a.inc'

proc StdOut,lpszText

hOutPut          dd ?
bWritten                dd ?
sl              dd ?
enter
       invoke  GetStdHandle,STD_OUTPUT_HANDLE
      mov     [hOutPut], eax
      invoke  lstrlen,[lpszText]
  mov     [sl],eax
    lea     eax,[bWritten]
      invoke  WriteFile,[hOutPut],[lpszText],[sl],eax,NULL
        mov     eax,[bWritten]
      return

endp

proc locate,x,y

_hOutPut  dd ?
xyVar dd ?
enter

  invoke  GetStdHandle,STD_OUTPUT_HANDLE
      mov     [_hOutPut], eax

 mov     ecx,[x]
     mov     eax,[y]
     shl     eax, 16
     mov     ax, cx

  invoke  SetConsoleCursorPosition,[_hOutPut],eax
     return

endp
    


Building the project:
Code:
set fasminc=\fasmw\include
\fasm\fasm main.asm
\fasm\fasm testobj.asm
\goasm\golink /console main.obj testobj.obj kernel32.dll user32.dll
    


GoLink available from:

http://godevtool.com


Description:
Download
Filename: extfunc.zip
Filesize: 2.1 KB
Downloaded: 319 Time(s)


_________________
Code it... That's all...
Post 05 Oct 2004, 17:42
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 05 Oct 2004, 18:02
If I understand properly, metalfishx wants to call dinamically a function inside an object file in a similar way a function is called in a dll.

The problem could be solved reading the object file (with ReadFile) and then doing the relocations needed (respect to the memory location where the file was stored) The memory location must be allocated as readable writeable executable (with VirtualAlloc).
The following step is to locate the starting point of the procedure (via the exported name) and then call it.

Question But the question is: dlls do this in a standard (safe) way and without much overhead, so why not use them?
Post 05 Oct 2004, 18:02
View user's profile Send private message Yahoo Messenger Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 05 Oct 2004, 18:20
Thanks Vortex for your code, i really know this. Smile
But exactly what i want to do is what pelaillo say.. call obj functions dinamically..
My purpose is to add this functionality to an interpreter... this way work faster than a dll call, and the size is small too. For example, one function in a dll to join two string needs 3k more or less.. in obj format, need 350bytes more or less.. and is more "SPEEDY" Smile this is my main reason... I think this is good for others purposes too. Smile

Pelaillo, or Vortex can you giveme please a little more trick code about the idea?

Thanks for the help!


Last edited by metalfishx on 05 Oct 2004, 22:04; edited 2 times in total
Post 05 Oct 2004, 18:20
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 05 Oct 2004, 19:52
This is an intersting topic, I've prepared a testing ground from a template.

Let us see what comes out... Smile

[edit] Full includes to avoid interference with standard macros v.1.56.[/edit]
[edit] New version: Working! It reads object files and show first two symbols.[/edit]


Description: Testing ground for running directly from obj files
Download
Filename: obj_run.zip
Filesize: 47.74 KB
Downloaded: 621 Time(s)



Last edited by pelaillo on 08 Oct 2004, 15:08; edited 1 time in total
Post 05 Oct 2004, 19:52
View user's profile Send private message Yahoo Messenger Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 05 Oct 2004, 22:00
Wow, looks good.. ill study the files... buffffff im new programing in fasm.. ill need some time i thing.. hehe

i like this topic too.. is good if this issua can be done ass well Smile
i hope every one like too and help a little , even me as newbye Smile hehe

Thanks!
Post 05 Oct 2004, 22:00
View user's profile Send private message Visit poster's website Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav 06 Oct 2004, 05:34
yes, topic is great and i feel, that fasm will be soon slightly powerful Very Happy

_________________
[not enough memory]
Post 06 Oct 2004, 05:34
View user's profile Send private message Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 08 Oct 2004, 13:55
Hey!
I have a little time today and i try to play with your code toda, but, with no results Sad
Pelaillo, why you dont simply use standart fasm macros and separate the extra functions, structs and the code in an include file to be more modular, compatible and undersandable by people with not much experience with fasm? Smile im ussing fasmw 1.56 and i wass totally unable to compile your code; when i try to fix one part, the compiler giveme error in another part.. even the first time i use the untouched code, the compiler result is an error in the kernel.inc...

Thanks for your help and contribution Smile

_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 08 Oct 2004, 13:55
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 08 Oct 2004, 15:00
My source code compiles directly from the standard out-of-the-box fasm 1.54 distribution. I kept the "non-standard" features in separate includes in a local inc folder.

Is not my fault that standard macro distribution changes more often than my coding style. I cannot change every source code file in my disk suddenly at every fasm version. Normally I migrate slowly to avoid errors.

For example: the problem is with the new approach to struct ... ends. I'm still at struc { ... } struct (I will use it but migration is slow)

On the other hand, I find it more comfortable to use call for every call and I won't use invoke never. I don't impose my coding style to nobody so I expect the same treatment. This is the only non standard feature I use, the rest are additions to winapi structures and constants.

p.s. The attachment above includes all files needed to compile it properly.
Post 08 Oct 2004, 15:00
View user's profile Send private message Yahoo Messenger Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 08 Oct 2004, 15:57
Ok no prob, but notice i just suggest you to use the standart compatible with the latest version that will help newbies (like me), no to impose you the use of the new code. In the other hand i saw the include files, but anyway im receiving an error in the %fasminc% files you have used in your win32.in file.

So, thanks anyway.

Cheers.
Post 08 Oct 2004, 15:57
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 08 Oct 2004, 16:54
Have you downloaded again the example?

I have compiled it using standard (out-of-the-box, recently downloaded) Fasm1.56 and it compiles properly. There is no need neither to define %fasminc%

ps. No hay problema, solo me interesa poder ayudar.
Post 08 Oct 2004, 16:54
View user's profile Send private message Yahoo Messenger Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 08 Oct 2004, 18:18
Okes ill try to download again.. maybe ihave some unknown problem... you know, im newbie with fasm (i have used masm some time before, not so much diferences but, there are) Smile


ps. Bueno, gracias, por un momento pense que lo habias tomado como algun tipo de insulto o algo, porque dijiste que no imponias tu estilo de codificacion y que esperabas que te trataran igual; entonces, pense que mi sugerencia la habias tomado como que queria imponer en vez de sugerir Smile
Gracias nuevamente... (Esto de poder ejecutar obj esta de pelos! ) Smile

Salutes.
Post 08 Oct 2004, 18:18
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.