I wonder, how to set a local variable in ASM’s procedure ?
thanks!!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to store a variable on the stack, you need to reserve space for it, this is generally done with the
SUB ESP,xxxsequence, wherexxxis the size of the “variable” you want to make space for, aligned to the stack alignment (generally 4 bytes, can also be 8 or 16). The only exception to this rule is when the variable is in a register, in which case you can perform aPUSHon that register.This space needs to be cleaned up on function exit, so if you
PUSHed a register, you shouldPOPit or,ADD ESP,xxxwherexxxwas the size you originallySUB‘ed/the size of the register youPUSHed aligned to the stack size.Reading and writing are done using
MOV, but this is where it gets a little tricky, as we have two cases, with stack frames, and without stack frames.without stack frames requires more math, as you need to compensate for the function arguments on the stack, so if our function takes 2 args, and we allocate space for an integer on the stack, we can write to it via
MOV [ESP + 0xC],value, reading is the sameMOV EAX,[ESP + 0xC].with a stack frame, your arguments take a positive index to
EBPand your allocated memory is negatively indexed fromEBP, so with the same example above, you’d doMOV EAX,[EBP-4].As you can see this gets a little tricky, so a better way is to create C or C++ code that represents what you want, compile it with
-O0(we compile with no optimization to prevent elision of stack vars to registers) then dissassemble it, and see how the compiler does it.