assemblyx86cpu-registersregister-allocation

Using FPU and MMX registers as "general registers"


Most assembly programs make use of the 4 general purpose registers eax, ebx, ecx, and edx but I find that quite often I need to use more than 4 registers to accomplish my task easily without having to push and pop from the stack too much. Since my program has no intentions of using the FPU or MMX registers for floating point calculations or their "intended use", is it considered acceptable to use these extra registers in your program?

Eg. using mm0 for a loop increment counter freeing up the ecx register to do other things.


Solution

  • Why four? You can use all of these: eax, ebx, ecx, edx, esi, edi and ebp. That's seven. Or is that not enough either?

    FPU and MMX registers are somewhat awkward to work with since they can only be loaded from themselves and memory and stored only to themselves and memory. You cannot freely move data between them and general purpose registers, nor there are instructions capable of operating on both kinds of registers at the same time.

    If seven general purpose registers aren't enough, use local/on-stack variables. For example, you can decrement a counter variable in memory directly and you can also directly compare it with a constant or another register. Chances are, this is going to be no slower (likely, faster) than using FPU or MMX registers in strange ways.