将变量添加到x86程序集中的堆栈中 [英] Add a variable to the stack in x86 assembly

查看:91
本文介绍了将变量添加到x86程序集中的堆栈中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在ASM的过程中设置局部变量?

I wonder, how to set a local variable in ASM's procedure ?

谢谢!

推荐答案

如果要在堆栈上存储变量,则需要为其保留空间,通常使用SUB ESP,xxx序列完成,其中是您要为其留出空间的变量"的大小,与堆栈对齐对齐(通常为4个字节,也可以为8或16).该规则的唯一例外是变量位于寄存器中时,在这种情况下,您可以在该寄存器上执行PUSH.

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,xxx sequence, where xxx is 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 a PUSH on that register.

此空间需要在函数退出时清理,因此,如果PUSH添加了寄存器,则应POP使用它,或者,ADD ESP,xxx,其中xxx是您最初由SUB编辑的大小/PUSH要对齐的寄存器大小与堆栈大小对齐.

This space needs to be cleaned up on function exit, so if you PUSHed a register, you should POP it or, ADD ESP,xxx where xxx was the size you originally SUB'ed/the size of the register you PUSHed aligned to the stack size.

使用MOV进行读写,但这在这里有些棘手,因为我们有两种情况,带堆栈框架和不带堆栈框架.

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.

没有堆栈帧需要更多的数学运算,因为您需要补偿堆栈上的函数参数,因此,如果我们的函数需要2个args,并且我们为堆栈上的整数分配了空间,则可以通过,读取的是相同的MOV EAX,[ESP + 0xC].

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 same MOV EAX,[ESP + 0xC].

具有堆栈框架,您的参数将对EBP的索引为正,而对您分配的内存从EBP的索引为负,因此在上面的相同示例中,您将执行MOV EAX,[EBP-4].

with a stack frame, your arguments take a positive index to EBP and your allocated memory is negatively indexed from EBP, so with the same example above, you'd do MOV EAX,[EBP-4].

如您所见,这有点棘手,所以更好的方法是创建表示所需内容的C或C ++代码,并使用-O0对其进行编译(我们进行了无优化编译,以防止堆栈变量被忽略到寄存器中) ),然后将其反汇编,并查看编译器是如何做到的.

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.

这篇关于将变量添加到x86程序集中的堆栈中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆