Add a variable to the stack in x86 assembly -
i wonder, how set local variable in asm's procedure ?
thanks!!
if want store variable on stack, need reserve space it, done sub esp,xxx sequence, xxx size of "variable" want make space for, aligned stack alignment (generally 4 bytes, can 8 or 16). exception rule when variable in register, in case can perform push on register.
this space needs cleaned on function exit, if pushed register, should pop or, add esp,xxx xxx size sub'ed/the size of register pushed aligned stack size.
reading , writing done using mov, gets little tricky, have 2 cases, stack frames, , without stack frames.
without stack frames requires more math, need compensate function arguments on stack, if our function takes 2 args, , allocate space integer on stack, can write via mov [esp + 0xc],value, reading same mov eax,[esp + 0xc].
with stack frame, arguments take positive index ebp , allocated memory negatively indexed ebp, same example above, you'd mov eax,[ebp-4].
as can see gets little tricky, better way create c or c++ code represents want, compile -o0 (we compile no optimization prevent elision of stack vars registers) dissassemble it, , see how compiler it.
Comments
Post a Comment