2

I'm trying to understand some assembly code with AT&T syntax.

Here is a snippet:

"mov %eax, %ebx; "\
"mov %eax, %ecx;"\
"fxch %st(1);"\

This is what I understood from it.

the mov copies (Am I correct?, or does it move?) the data from the source register to the destination register

In line one: we copy the data from registry eax to ebx.

Similarly, we copy the data from registry eax to ecx.

However, what I failed to understand is the following. How does fxch work? Here is a link that gives an example.

   fxch    st(2)
   fsqrt
   fxch    st(2)

It says that this above code takes the sqrt of st(2). Correct me if I am wrong. It swaps the top of the stack with st(2) and then takes the sqrt of what? I don't understand that clearly.

Can you please help me out? How does that work in my case and in the above case?

1 Answer 1

3

mov instructions indeed copy a value and fsqrt takes the square root of the top of the stack and replaces the top of the stack with its result. So the given code sequence effectively takes the square root of st(2) and puts it back at the same place.

In answer to your question below. The two mov instructions copy the value in register %eax to %ebx and %ecx. So if you add another mov %eax,%edx, then this value (from %eax) is also copied to %edx.

Note that this holds for AT&T assembly. In Intel assembly the values are copied the other way around. In that case %eax was, quite uselessly, changed repeatedly to contain the value of the other registers.

The fxch st(1) exchanges the top of the stack, which is st(0) with the element just below the top st(1). Similarly st(2) is just below st(1). Contrary to the integer registers, the floating point registers on the x86 are organized in a stack, reducing the instruction length of operations on those floating point registers as they always work on the top element(s) of the stack. This comes with the overhead of having to use fxch instructions to put the right values on the top of the stack.

The integer registers %eax, %ebx etc. are distinct from the floating point stack/registers st(0), st(1) etc. So the mov instructions are not related to the fxch instructions. The order of these instructions could be changed without effecting the result.

3
  • Thank you. That is clear now. In the first code snippet, what does it do there? if it is followed by another move command like this. `"mov %eax, %edx;"`.
    – pistal
    Jun 3, 2013 at 20:52
  • then what role is fxch st(1) playing there? Edit: Sorry for the confusion. "mov %eax, %ebx; "\ "mov %eax, %ecx;"\ "fxch %st(1);"\ "mov %eax, %edx"
    – pistal
    Jun 3, 2013 at 20:58
  • Yes. The question has been answered completely. Thanks a lot.
    – pistal
    Jun 3, 2013 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.