Difference Between Addq and Movl, Then Addq

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?

1

1 Answer

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 :-)

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest