Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Calling C function from Assembly Code

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Aug 28, 2009 5:20 pm    Post subject: Calling C function from Assembly Code Reply with quote

I'm trying to call a function in one of my C programs from an assembly program. But when trying to compile I get an error from NASM:

Code:

stub.asm:25: error: binary output format does not support external references


Ok, so my output format doesn't support external references. My output format is a flat binary. Is there any way to call the function or do I need to switch output formats?

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Fri Aug 28, 2009 6:19 pm    Post subject: Reply with quote

Post the code, maybe?
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Aug 28, 2009 6:27 pm    Post subject: Reply with quote

Why? I just want to know how to call an external function when I'm using the flat binary format. I don't see why you need to know the code within the functions?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25953
Location: The netherlands

PostPosted: Fri Aug 28, 2009 6:33 pm    Post subject: Reply with quote

what is your current compile line for the asm file ?

Anyhow, just make sure it's outputting as an object file that can be used by the linker you use. (so don't output as a .com/.exe, etc...)

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Aug 28, 2009 7:59 pm    Post subject: Reply with quote

Code:

nasm stub.asm -f bin -o stub.o


But like I said, NASM is saying:

Code:

stub.asm:25: error: binary output format does not support external references


So I was wondering how to call external functions when compiling a flat binary?

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25953
Location: The netherlands

PostPosted: Fri Aug 28, 2009 8:16 pm    Post subject: Reply with quote

What kind of context are you running it ?
16 bit dos?(entry point starts at 0x100) 16 bit bootloader ? (entry point starts at 0:7c00) 32 bit windows ? (entry point can be anywhere)
also, is the c file compiled as 16-bit or 32-bit ?
is that stub.asm the first code being executed, or it it called from another sourcefile ?

anyhow, you must change the "-f bin" to another format, e.g -f obj or -f win32 and then link the .obj files together (in any case, if you want to use multiple sourcefiles and have them interact, you must link them, so -f bin isn't possible)

and to call a function declared elsewhere you have to use the line "extern myfunctionname"

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Aug 28, 2009 8:23 pm    Post subject: Reply with quote

I'll try changing it to -f obj. And I know about the extern thing. I have it as:

Code:

main:
   extern _main
   call _main


gcc prepends the underscore to all function and variable names lol.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25953
Location: The netherlands

PostPosted: Fri Aug 28, 2009 8:27 pm    Post subject: Reply with quote

oib111 wrote:
gcc prepends the underscore to all function and variable names lol.

Hmm, must be a windows version of gcc thing. I never had to do that (or the linker does it automatically for me)

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Aug 28, 2009 8:30 pm    Post subject: Reply with quote

I think the linker is doing it automatically for you. New problem, windows is refusing to execute gcc?

Code:

The system cannot execute the specified program.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25953
Location: The netherlands

PostPosted: Fri Aug 28, 2009 9:48 pm    Post subject: Reply with quote

if you're using a 64-bit version of windows, and this is a 16-bit version of gcc, then yes, it can't execute it
otherwise, I have no idea

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Aug 28, 2009 9:51 pm    Post subject: Reply with quote

32-bit version of windows, and I don't believe it's the 16-bit version anyways. It's just odd because gcc was just working.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25953
Location: The netherlands

PostPosted: Fri Aug 28, 2009 9:54 pm    Post subject: Reply with quote

did you change the compile parameters for gcc as well ? If so, try changing them back. (perhaps it wants to execute another program which doesn't work)
_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Aug 28, 2009 10:03 pm    Post subject: Reply with quote

Yeah I tried specifying multiple files in one command (which was working before, oh well), so I just had to split it up into two commands.

EDIT:

Ok...here's a slightly related problem. I defined a prototype to a function in a .c file and then had stub.asm define the actual code for the function with this:

Code:

global _gdt_flush
extern _gp
_gdt_flush:
   ; define code here


But I'm getting this output from tlink:

Code:

gdt.o:gdt.c:(.text+0xa0): undefined reference to `_gdt_flush'


Am I doing something wrong?

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25953
Location: The netherlands

PostPosted: Sat Aug 29, 2009 6:24 am    Post subject: Reply with quote

Try it without adding a _ in front

Also, in your gdt.c (or .h) did you define gdt_flush properly ?
e.g: extern void gdt_flush(void);

and when compiling gdt.c use the option -c so it only compiles and doesn't link
Later when all .c files are compiled, just link them all together in one go

http://cheatengine.org/download/dbvmsrc.rar
In the vmm folder check the makefile , main.h and vmma.asm for an example

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping


Last edited by Dark Byte on Sat Aug 29, 2009 9:30 am; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Aug 29, 2009 8:36 am    Post subject: Reply with quote

In gdt.c I do have it defined with the extern prefix, and I'm already using the -c flag in gcc so that it doesn't link them, just compile. Removing the prepended underscore doesn't work either Confused

EDIT:

Turns out since I was using nasm to assemble to a win32 object file ld couldn't "use it" correctly or something. Changing the format to aout worked.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites