Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the EAX register optimized for calculations on modern processors?

The article The Art of picking Intel Registers reads:

[Because] most calculations occur in the accumulator, the x86 architecture contains many optimized instructions for moving data in and out of this register. To start, the processor has sixteen byte-sized XCHG opcodes for swapping data between the accumulator and any other register. These aren't terribly useful, but they show how strongly the Intel engineers preferred the accumulator over the other registers. For them, it was better to swap data into the accumulator to than to work with it where it was. Other instructions that move data in and out of the accumulator are LODS, STOS, IN, OUT, INS, OUTS, SCAS, and XLAT. Finally, the MOV instruction has a special one-byte opcode for moving data into the accumulator from a constant memory location.

In your code, try to perform as much work in the accumulator as possible.

As the the article is dated 2003 I'm curious to know if this is true modern processors (hello from 2017). Points given for references to Intel documentation.

like image 205
Olumide Avatar asked Oct 16 '25 01:10

Olumide


1 Answers

Modern Intel processors use a technique called register renaming. In a nutshell, the CPU has a bunch of registers (~100) that are disconnected from the registers you see in your assembly. The CPU manages that pool of registers and maps them to register names as needed. Thus, all general purpose registers are indistinguishable as far as the CPU is concerned.

When programming assembly from hand, register choice can be important as some instructions (like single-operand imul) only operate on fixed registers. However, these instructions are rare and most of them are obsolete, rendering this choice much less important in practice.

Another issue that depending on your calling convention, some registers must be preserved when returning, so before using them for your own purpose, you have to save their value. If you only need a few registers in your function, it is useful to avoid such registers so you can get rid of the corresponding pushes and pops.

like image 194
fuz Avatar answered Oct 19 '25 13:10

fuz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!