简体   繁体   English

了解汇编中的cmp

[英]Understanding cmp in assembly

I am looking at an assembly code that looks like the following: 我正在查看如下汇编代码:

cmp dword [rbp-0x4 {var_c}], 0x0
jne 0x12c0

To me, that reads: 对我来说,这是:

Compare the value of something and null, and if there is no error (ie they match), jump to 0x12c0. 比较something和null的值,如果没有错误(即它们匹配),则跳转到0x12c0。

Is that correct? 那是对的吗? I don't understand what [rbp-0x4 {var_c}] is or why we would be comparing that to null. 我不知道什么是[rbp-0x4 {var_c}]或为什么我们将其与null进行比较。

I did try and follow a graph to learn what these variables were and I got the following: 我确实尝试并按照图表来了解这些变量是什么,并且得到了以下信息:

  • 0x4 = uint8_t file_class = 0x2
  • var_c = In Set of Values {0, 1}
  • rbp seems to be what is pushed in from main rbp似乎是从main推入的

Any help on understanding this would be appreciated. 任何帮助理解这一点将不胜感激。 I am looking for clarification on what is being compared in the cmp statement. 我正在寻找有关在cmp语句中进行比较的说明。

Not quite, it's more: 不完全是,更多:

Compare the value of something with zero and, if they are not equal, jump. 将某物的值与零进行比较,如果不相等,则跳转。

Assembly language has no concept of "null" and a cmp is generally the same as a sub (subtract) but without actually changing the value. 汇编语言有没有“空”的概念和cmp通常是相同的一个sub (减法),但没有实际改变值。 Basically, it's: 基本上是:

Set the flags as if I had subtracted zero from something. 将标志设置为好像我从某物减去零。

In more pseudo-code fashion, your two instructions amount to: 以更伪代码的方式,您的两条指令总计为:

if var_c <> 0 then goto label_12c0

rbp seems to be what is pushed in from main rbp似乎是从main推入的

rbp is one CPU register that (like all CPU registers) stores a value. rbp是一个CPU寄存器(与所有CPU寄存器一样)存储一个值。

I don't want to go too deep into detail here, but most compilers use the register rbp to store the information where the local variables (and sometimes function arguments) of the function are stored in the RAM memory: 我不想在这里过分详细,但是大多数编译器使用寄存器rbp来存储信息,该信息用于将函数的局部变量(有时是函数参数)存储在RAM存储器中:

The address (location in RAM memory) where a certain local variable is stored is typically calculated by subtracting some constant value from the value stored in the rbp register. 通常通过从rbp寄存器中存储的值中减去一些常数来计算存储某个局部变量的地址(RAM存储器中的位置)。

I don't understand what [rbp-0x4 {var_c}] is ... 我不明白[rbp-0x4 {var_c}]是什么...

dword [rbp-0x4] means: A 32-bit value stored at the address rbp-4 : The address which is calculated by subtracting the value 4 from the value stored in the register rbp . dword [rbp-0x4]表示:一个存储在地址rbp-4上的32位值:该地址是通过从存储在寄存器rbp的值减去值4得出的。

The compiler has put additional information for a debugger into the binary file. 编译器已将调试器的其他信息放入二进制文件中。 This information says that the address of the local variable var_c is calculated by rbp-4 and the address of the local variable some_other_variable is calculated by rbp-10 and so on... 这个信息说,局部变量的地址var_c被计算rbp-4和局部变量的地址some_other_variable被计算rbp-10等等...

The disassembler has read this information and prints {var_c} after rbp-0x4 to show that the variable var_c is located at the address rbp-0x4 . 反汇编程序已读取此信息,并在rbp-0x4之后打印{var_c} ,以表明变量var_c位于地址rbp-0x4 So the 32-bit value " dword [rbp-0x4] " probably is the variable " var_c ". 因此,32位值“ dword [rbp-0x4] ”可能是变量“ var_c ”。

0x4 = uint8_t file_class = 0x2 0x4 = uint8_t file_class = 0x2

I don't know which information this is. 我不知道这是什么信息。 But the value 0x4 here has nothing to do with the value 0x4 in the disassembly line ( rbp-0x4 ). 但价值0x4这里无关与价值0x4在拆卸线( rbp-0x4 )。

... and if there is no error (ie they match), jump to 0x12c0. ...,如果没有错误(即它们匹配),则跳至0x12c0。

jne means: " J ump if n ot e qual". jne意思是: “ĴUMP如果n OTËQUAL”。

This means that the CPU will jump if the variable var_c was not equal to 0. 这意味着如果变量var_c 等于0,则CPU将跳转。

Note that in compiled code representing an if() branch, the jump instruction will normally jump if the condition was false : 请注意,在表示if()分支的已编译代码中,如果条件为false ,则跳转指令通常将跳转:

If the condition is false , the CPU jumps to the else part or to the first instruction after the if() part. 如果条件为false ,则CPU跳转到else部分或if()部分之后的第一条指令。 If the condition is true , the CPU does not jump but execute the first instruction of the if() part which follows the jne (or similar) instruction. 如果条件为 ,则CPU 跳但执行的第一指令if()以下的部分jne (或类似的)指令。

Because your example jumps if var_c is not zero, it is probable that the source code was something like if(var_c == 0) . 因为如果var_c 为零,则示例会跳转,因此源代码很可能类似于if(var_c == 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM