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 


[ASM] How do functions work/look like?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hitmetwice
Advanced Cheater
Reputation: 0

Joined: 20 Nov 2012
Posts: 63

PostPosted: Thu Oct 03, 2013 12:09 pm    Post subject: [ASM] How do functions work/look like? Reply with quote

Could you give me some examples of how a function looks like in assembly and what the code that calls it would look like?

For example:

Code:
int Add(int x, int y) {
    return x + y;
}


and

Code:
int result = Add(12, 3);
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Thu Oct 03, 2013 12:56 pm    Post subject: Reply with quote

Procedures / Functions in assembly are wrapped between 'proc' <funcName> at start
and 'endp' at the end of the procedure / function.
Code:

proc Add
  you code here
endp

as far for the code well, it would be something like
Code:

PROC Add
  push ebp
  mov ebp,esp
  mov eax,[ebp+8h]
  add eax,[ebp+0ch]
  pop ebp
  ret
ENDP

for standard call the parameters are passed through the stack
and keep in mind the return value returned in eax register.

That's pretty much the general idea.
for more detailed information google it, there's plenty of documentation Smile

_________________
Stylo
Back to top
View user's profile Send private message
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Sat Oct 05, 2013 5:01 pm    Post subject: Reply with quote

Most C and C++ compilers also provide a switch that will cause them to generate assembly code, even if it isn't an explicit intermediate step. For simple examples, like yours, it may be useful for study purposes.
Back to top
View user's profile Send private message
hitmetwice
Advanced Cheater
Reputation: 0

Joined: 20 Nov 2012
Posts: 63

PostPosted: Sat Oct 12, 2013 2:06 am    Post subject: Reply with quote

Thank you guys, this helps a lot.
Does a function in assembly always start with "PROC "?
I'm just wonderung because when I browse through the memory with CE and rightclick different addresses and say "Select current function", the selected functions have very different beginnings like cmp, push, mov.
This feature of CE does really confuses me and I'm not sure if I use it correctly.
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Sun Oct 13, 2013 6:38 am    Post subject: Reply with quote

The 'PROC' & 'ENDP' are just keywords for the compiler to know whether a new procedure started / ended.
when the code is assembled into a binary format there are no 'proc' or labels etc... they translated into addresses
In the same manner i could write a function like that
Code:


 my_function:
    push ebp
    mov ebp,esp
    // my_function code
    leave
    retn

 // my main
 // some code
 call my_function
 // continue with rest of the code

when you disassemble the code using CE you won't see 'my_function' label, but an address that my_function is located at.
and for the push ebp, mov ebp,esp instructions, every function starts in it's own way, it could start however the programmer wants.
the popular way is storing ebp register so you won't lose it's value and then make him point to the top of the stack because esp always changes and you want a a nice and tidy access to the parameters that are stored in the stack instead of calculating every time where the parameters are relatively to esp.

_________________
Stylo
Back to top
View user's profile Send private message
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