Difference between addq and movl, then addq

assembly, x86-64

Solution

The first one pulls a full quadword (64 bits) out of memory at location `rbx` and adds that to the `rax` register.

The second pulls a longword (only 32 bits) from that same location and stores it into `ecx` (zeroing the top half of `rcx`). Then it adds `rcx` to `rax`.

So I would say the main difference is that the second snippet is not adding the full quadword in memory to `rax`, only the longword.

The first (one-liner) code sample would be more akin to:

movq    (%rbx), %rcx
addq    %rcx,   %rax

although even that is not strictly identical since it changes `rcx`. To make it an even closer match, you could save and restore `rcx` as part of the process:

push    %rcx
movq    (%rbx), %rcx
addq    %rcx,   %rax
pop     %rcx

Although then, of course, you've changed `rsp` (albeit temporarily) and it requires you to have a stack actually set up (likely, but not absolutely the case) so you may be better off just sticking with the one-liner :-)

Problem

What is the functional difference between `addq (%rbx), %rax ` and `movl (%rbx), %ecx addq %rcx, %rax` in assembly? I think they do the same thing, but what is the functional difference?

Original source