#cdecl

81 messages · Page 1 of 1 (latest)

glossy crow
#

What do these operations do ? Dont they cancel each other

mov DWORD PTR [ebp-4], eax
mov eax, DWORD PTR [ebp-4]

obsidian vaporBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

thorny prism
#

they don't cancel each other out

#

its just not necessarily important

#

they second instruction is irrelevant

#

well so its the first one but technically it does something

#

you copy a value from one place to another. Therefore both locations contain the same value
Therefore copying in the reverse is pointless since we already know both values contain the same value

#

this is unoptimized code

#

the first store is storing to sum

#

the load is loading from sum

#

into eax, which is the return register

glossy crow
#

i guess its a trick operation

thorny prism
#

but the value was in eax originally so this is pointless

glossy crow
#

the next question ask the state of the stack before leave instruction

thorny prism
glossy crow
#

i still dont understand how it comes to this

thorny prism
#

what are you confused by?

glossy crow
#

ebp+8 is where the x is

#

but dont we push y to the stack first

#

by cdecl

thorny prism
#

no?

#

usually they go left to right in order

glossy crow
#

in the CDECL calling convention the following holds:

Arguments are passed on the stack in Right-to-Left order, and return values are passed in eax.

#

okay lets say its saved like that

#

when we do

#

add eax,edx than the block where y is should have the value 12 right?

thorny prism
#

hmm

#

okay maybe they are right to left

#

AH

#

wait yes I misphrased

#

right to left and left to right depends on your perspective

#

they are pushed in right to left order

#

which means the most right will be deepest on the stack

glossy crow
#

you mean on the highest adress?

thorny prism
#

it will be the highest address yes because the stack grows downwards

#

hence why y is at the top

#

personally I think of that as left-to-right because of how Ive implemented it before, hence why I may have confused you

glossy crow
#

mov edx, DWORD PTR [ebp+8] so this saves x

thorny prism
#

yes

glossy crow
#

why is return address right before saved frame pointer(ebp?) is it the norm?

thorny prism
#

how could it not be?

#

the call instruction is what places that on the stack

#

before you even get to the current stack frame

glossy crow
#

call instruction

#

oh okay

#

so its always hhere

#

what did mov ebp,esp do

thorny prism
#

call pushes the return address onto the stack
ret pops it off the stack

glossy crow
#

they point to different spaces

thorny prism
#

ebp points to the bottom of the stack

#

esp points to the top

glossy crow
#

oh

thorny prism
#

(which is actually below ebp but nevermind)

glossy crow
#

the bottom?

thorny prism
#

in a single stack frame btw, not necessarily the entire stack

glossy crow
#

oh the more you learn

thorny prism
#

well

#

when you enter a function you know 1 thing about the stack

#

esp points to the top of it

#

(which is now the bottom of your current stack frame)

glossy crow
#

yep

thorny prism
#

so you copy that to ebp so now ebp contains the bottom of your stack frame

#

and then you modify esp to grow your stack frame

#

(see the sub instruction)

glossy crow
#

so the boxes are called a stack frame i always wondered what a frame is

#

im a hopeless case

#

thanks a lotttt

thorny prism
#

then at the end you want to restore the stack frame when you return (because you don't want the stack frame messed up every time you call a function)
so you move the value in ebp back into esp moving all the values back to their original locations as if the call never happened

thorny prism
#

but assembly stack management is entirely based on stack frames in modern programming

thorny prism
#

there is also an enter instruction

glossy crow
#

so many words..

thorny prism
#

which does some of the stuff you see at the top, but in 1 instruction

glossy crow
#

thank you ill read all that tomorrow morning i cant take this anymore

thorny prism
#

wikipedia has a nice annotated assembly example

#

helps explain what each instruction is doing

obsidian vaporBOT
#

@glossy crow Has your question been resolved? If so, type !solved :)