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 


This might be difficult, but what does this code mean?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Mar 01, 2016 6:46 pm    Post subject: This might be difficult, but what does this code mean? Reply with quote

Code:

push ebp
mov ebp,esp
push esi
push edi
mov esi,edx
mov edi,ecx
call ...............
push [ebp+10]
mov edx,00000001
push [ebp+0c]
mov ecx,edi
push [ebp+08]
push esi
push eax
call ..........
add esp 14
pop edi
pop esi
pop ebp
ret


It seems that it has something to do with the stack, but I'm confused. I would like to know what does this piece of code do, Any information is appreciated. Thanks a lot.
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Tue Mar 01, 2016 7:06 pm    Post subject: Reply with quote

That appears to be a function calling two other functions.
Not sure what you hope us to discover through that code.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Mar 01, 2016 7:23 pm    Post subject: Reply with quote

Zanzer wrote:
That appears to be a function calling two other functions.
Not sure what you hope us to discover through that code.


I wanna know how ebp and esp works, especially something like this:
Code:
mov edx,[ebp-44]


1."-44" is an offset, right? If so, is 44 in hex or decimal? Is ebp 16-bit or 32-bit?
2.what is a "stack"? How long can it be?
Sorry for the dumb questions..
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4718

PostPosted: Tue Mar 01, 2016 11:15 pm    Post subject: Reply with quote

It's usually safe to assume everything in ASM is in hex. The only exception I can think of off the top of my head is the size of memory allocated via alloc(...) in an AA script. Note that when writing you own ASM, you can prepend some numbers with a # symbol to turn them into decimal. For example, "mov eax,10" would move 16 (decimal) into eax, while "mov eax,#10" would move 10 (decimal) into eax. It will still be translated into its hex equivalent, however, so that #10 would turn into "A" when looking at it in the disassembler.

Registers beginning with "e" (ebp) are 32-bit. If they begin with "r" (rbp), they're 64 bit. If they don't begin with anything, they're 16-bit.

ebp is usually used with controlling and accessing the stack (it can also be used as a general-purpose register). Basically, the stack is an area of memory. While it does have a fixed amount of memory it can store, you generally won't have to worry about that unless you have some major bug in any code you write. esp always points to the top of the stack, and ebp points to some address in the stack (when it's not being used as a general-purpose register).

To understand why ebp is useful, you first need to know about a couple things- subroutines and stack frames. A subroutine is a set of contiguous instructions that can be called to run and expected to return eventually. If you ever see a "call" instruction, that's calling a subroutine to run. I'll refer to a "calling subroutine" as one that calls another "called subroutine".

Most subroutines have a "stack frame" - basically a section of the stack that they're using. The bottom (aka base) of the stack frame is generally fixed, but the top can change as it pushes/pops stuff on/of the stack. A calling subroutine's stack frame shouldn't change, however, since a called subroutine shouldn't mess with a calling subroutine's stack frame. Once the called subroutine returns, then it's stack frame is freed and the calling subroutine is free to modify its stack frame as it wants to (even if it overrides the old called subroutine's stack frame).

If you see the sequence of instructions "push ebp / mov ebp,esp", those two instructions set up the stack frame. "push ebp" saves the base address of the calling subroutine's stack frame. "mov ebp,esp" moves esp (the top of the stack) into ebp, effectively establishing the base address of it's stack frame.

To tear a stack frame down, you should first make sure the stack is at where it was after it set up the stack frame. Sometimes, it already is this way, and other times, you may need to do something like "mov esp,ebp". After that, just pop the calling subroutine's stack frame back into ebp via "pop ebp", then return.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Mar 01, 2016 11:36 pm    Post subject: Reply with quote

@ParkourPenguin,
Thank you so much, man! Your explanation is very helpful.
I have some following up questions:
1. I know a bit about some high-level programming languages, such as Lua , Swift. Can I think of subroutine as functions, just like in Lua?

2. the last paragraph of your post:
ParkourPenguin wrote:

To tear a stack frame down, you should first make sure the stack is at where it was after it set up the stack frame. Sometimes, it already is this way, and other times, you may need to do something like "mov esp,ebp". After that, just pop the calling subroutine's stack frame back into ebp via "pop ebp", then return.


can you explain more about the first and last sentence? Making sure which stack is at where it was? I kinda get the idea, but still wanna confirm with you.

Thank you again.
PS: can you recommend some books or tutorials for me to read to get a better understanding of assembly language? I really want to learn it. I know some basic programming stuff, like a bit of Lua, C++, Swift.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4718

PostPosted: Wed Mar 02, 2016 12:13 am    Post subject: Reply with quote

1: Sure. You can call both of them to run. When both of them end, they go back to where they were called and continue from there. You can pass arguments to subroutines too (via the stack for 32-bit processes).

2: Basically, subroutines should clean up whatever they did to the stack when they return.


For example, let's say a subroutine is called to run. After establishing the stack frame, esp (the top of the stack) and thus ebp is at 00241DE0. That means the base of the calling subroutine's stack frame is stored at that address ([ebp] == ebp of calling subroutine).

Assume the called subroutine modifies the stack; namely, it pushes something onto it. Now, the stack is 4 bytes off of where it needs to be in order to pop the right value back into ebp. So, you can either pop whatever you pushed onto the stack back off, or modify esp directly to move it back to 00241DE0 (e.g. mov esp,ebp). Then it's safe to pop the value at the top of the stack back into ebp since you're certain it's the same thing you pushed onto the stack at the beginning of the subroutine.

Thus, ebp is safely reverted back to what it was before the call, and everything is happy.


I learned ASM primarily by screwing around in games and googling specific stuff I didn't know. This YouTube playlist helped me get a kickstart into it, but it's not completely beginner-friendly. With your experience, however, you should be fine. If you do want something more beginner friendly, there's this CEF topic, but it oversimplifies a few things IIRC. I suppose I've neglected to mention a few important things too (e.g. how the top of the stack grows downward), so I shouldn't be one to speak.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Wed Mar 02, 2016 12:47 am    Post subject: Reply with quote

ParkourPenguin wrote:
1: Sure. You can call both of them to run. When both of them end, they go back to where they were called and continue from there. You can pass arguments to subroutines too (via the stack for 32-bit processes).

2: Basically, subroutines should clean up whatever they did to the stack when they return.


For example, let's say a subroutine is called to run. After establishing the stack frame, esp (the top of the stack) and thus ebp is at 00241DE0. That means the base of the calling subroutine's stack frame is stored at that address ([ebp] == ebp of calling subroutine).

Assume the called subroutine modifies the stack; namely, it pushes something onto it. Now, the stack is 4 bytes off of where it needs to be in order to pop the right value back into ebp. So, you can either pop whatever you pushed onto the stack back off, or modify esp directly to move it back to 00241DE0 (e.g. mov esp,ebp). Then it's safe to pop the value at the top of the stack back into ebp since you're certain it's the same thing you pushed onto the stack at the beginning of the subroutine.

Thus, ebp is safely reverted back to what it was before the call, and everything is happy.


I learned ASM primarily by screwing around in games and googling specific stuff I didn't know. helped me get a kickstart into it, but it's not completely beginner-friendly. With your experience, however, you should be fine. If you do want something more beginner friendly, there'sthis CEF topic[/url], but it oversimplifies a few things IIRC. I suppose I've neglected to mention a few important things too (e.g. how the top of the stack grows downward), so I shouldn't be one to speak.


Thanks a lot, I really appreciate your help. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine 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