Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FPU and MMX registers as "general registers"

Most assembly programs make use of the 4 general purpose registers eax ebx ecx 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 to 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 xmm0 for a loop increment counter freeing up the ecx register to do other things.

like image 600
user99545 Avatar asked Feb 23 '13 05:02

user99545


2 Answers

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.

like image 128
Alexey Frunze Avatar answered Oct 30 '22 18:10

Alexey Frunze


How often do you need full 32 bits of a register? For things like small counters, feel free to use byte-sized quarters of general purpose registers: AH/AL, BH/BL, CH/CL, DH/DL. With some bitwise trickery, you can also use upper 16 bits of general purpose registers as an intermediate storage for word-sized variables.

In real mode (read: under DOS), you can also use segment registers ES, FS, and GS for intermediate value storage. Under a protected-mode OS (Windows, Linux, *nix) the code will crash, though.

like image 24
Seva Alekseyev Avatar answered Oct 30 '22 19:10

Seva Alekseyev