assemblyx86cpu-registersaccumulator

Does it matter which registers you use when writing assembly?


If you're writing assembly, does it matter which registers you allocate values to? Say, you store an accumulated/intermediate value in %ebx instead of %eax, which was traditionally used for that purpose. Is that bad practice? Will it affect performance?

In other words, can you treat them equally as storage space, or should you stick to using them for specific purposes?


Solution

  • First and foremost, you have to use registers that support the instructions you want to use.  Many instructions on x86 (and other architectures, though less so) have some restrictions on how registers are supported.

    Take certain double register multiply and divide instructions, for example, which specifically involve eax and edx in particular uses.

    Next, you want to use registers that are efficient, i.e. registers: