简体   繁体   English

理解/编写 C 代码,用于单精度浮点数的汇编

[英]Understanding/Writing C code for Assembly with single precision floating point numbers

I am having trouble deciphering this given Assembly code:我无法破译这个给定的汇编代码:

1 unknown5:
2    pxor   %xmm0, %xmm0
3    movl   $0, %eax
4    jmp    .L13
5  .L14:
6    movss  (%rdx,%rax,4), %xmm1
7    mulss  (%rsi,%rax,4), %xmm1
8    addss  %xmm1, %xmm0
9    addq   $1, %rax
10 .L13:
11   cmpq   %rdi, %rax
12   jb .L14
13   rep ret

What I think is going on:认为正在发生的事情:

  • 1: function named unknown5() 1: function 命名为 unknown5()
  • 2: sets the floating point return register to 0 2:设置浮点返回寄存器为0
  • 3: sets the 32-bit return register to 0 3:设置32位返回寄存器为0
  • 4: jumps to 10: 4:跳转到 10:
  • 11: if %rdi > %rax, go to 5:, else go to 13: 11:如果 %rdi > %rax,则 go 为 5:,否则 go 为 13:
  • 6: %xmm1 = %rdx+%rax+4 6: %xmm1 = %rdx+%rax+4
  • 7: %xmm1 *= %rsi+%rax+4 7: %xmm1 *= %rsi+%rax+4
  • 8: %xmm0 += %xmm1 8: %xmm0 += %xmm1
  • 9: %rax += 1 9: %rax += 1
  • 13: return %eax, %rax, or %xmm0?? 13:返回 %eax、%rax 或 %xmm0??

What I have no clue of is what the possible parameters are of the function, nor what each register is representing.我不知道 function 的可能参数是什么,也不知道每个寄存器代表什么。 I know %rdi, %rsi, and %rdx are first, second, and third parameters (that might be floating point pointers due to single precision operations?), and %xmm0, %xmm1 are first and second floating point parameters (OR local variables?).我知道 %rdi、%rsi 和 %rdx 是第一个、第二个和第三个参数(由于单精度操作可能是浮点指针?),而 %xmm0、%xmm1 是第一个和第二个浮点参数(或本地变量?)。 I also know %rax is the 64-bit return register, but am unsure why it is used here as such.我也知道 %rax 是 64 位返回寄存器,但我不确定为什么在这里使用它。 Could someone please elaborate and correct my comprehension?有人可以详细说明并纠正我的理解吗? Thank you.谢谢你。

A few hints:一些提示:

  • %xmm0, %xmm1 are used to pass floating-point parameters, if the function actually takes any. %xmm0, %xmm1用于传递浮点参数,如果 function 实际上需要任何参数。 But are the values they had on entry to the function actually used in any way?但是他们在进入 function 时所拥有的值是否真的以任何方式使用过?

  • Don't think about %rax as inherently being "a return register".不要认为%rax天生就是“返回寄存器”。 It's just a register.这只是一个寄存器。 If the function is meant to return an integer or pointer then yes, %rax (or one of its sub-registers) is where that return value will go, but in the meantime it's just a register that the code can use as it likes, like a local variable as you say. If the function is meant to return an integer or pointer then yes, %rax (or one of its sub-registers) is where that return value will go, but in the meantime it's just a register that the code can use as it likes,就像你说的局部变量。 Same goes for %xmm0 . %xmm0

  • Your guess about %rdx and %rsi being pointers is a good one, given how they are used in the movss/mulss memory operands.考虑到它们在movss/mulss memory 操作数中的使用方式,您对%rdx%rsi作为指针的猜测是不错的。 Where exactly are these instructions loading values from?这些指令究竟是从哪里加载值的? On the other hand, %rdi does not appear in such a context;另一方面, %rdi不会出现在这样的上下文中; how is it used instead?它是如何使用的?

    Note movss (%rdx,%rax,4), %xmm1 doesn't set %xmm1 equal to the value gotten by multiplying %rax by 4 and adding %rdx .注意movss (%rdx,%rax,4), %xmm1不会设置%xmm1等于通过将%rax乘以 4 并添加%rdx得到的值。 Rather, it uses %rdx+%rax*4 as an address , fetches a single-precision float from that address in memory, and loads it into %xmm1 .相反,它使用%rdx+%rax*4作为地址,从 memory 中的该地址获取单精度浮点数,并将其加载到%xmm1中。 That's what the ( ) signify - an indirect memory reference.这就是( )的含义——间接的 memory 参考。 If you like, it's the rough equivalent of the unary * dereference operator in C.如果您愿意,它大致相当于 C 中的一元*解引用运算符。

    Likewise, the value being multiplied in the following mulss is also fetched from memory.同样,以下mulss中相乘的值也取自 memory。

  • There's no certain way to tell from the code alone whether it's intended to return an int (return value in %eax ) or long ( %rax ) or float/double ( %xmm0 ).仅从代码中没有确定的方法来判断它是打算返回一个int (在%eax中的返回值)还是long%rax )或float/double%xmm0 )。 But you can take a very good guess.但你可以做出一个很好的猜测。 Think about the values that each of those registers has when the function returns.想想当 function 返回时每个寄存器的值。 Which seems more plausible as a value for the function to be computing for its caller? function 为其调用者计算的值似乎更合理?

    Notice there are no instructions in this function that write to memory, so it has no "side effects";请注意,此 function 中没有写入 memory 的指令,因此它没有“副作用”; its sole purpose must be to compute and return some value.它的唯一目的必须是计算并返回一些值。

  • This function appears to be performing a familiar mathematical operation.这个 function 似乎正在执行熟悉的数学运算。 Can you tell what it is?你能说出它是什么吗?

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

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