If you're writing assembly, does it matter which registers you allocate values to?
For 80x86; cases where it can matter which register/s you use include:
complying with someone else's calling conventions (passing values in the right registers, avoiding stack use for "callee saved" by preferring "caller saved" registers)
using an instruction that has implied registers (MUL, DIV, MOVSQ/D/W/B, STOSQ/D/W/B, XLATB, AAA, CWD, ... - there's lots of them)
trying to avoid the cost of a segment register prefix when segments aren't all the same (e.g. mov [ds:bp], ...
vs. mov [bx],...
).
avoiding address calculations that can't be encoded due to restrictions of "MOD/RM" fields (e.g. mov [di+si], ...
) Mostly irrelevant for 32/64-bit code, any reg can be a base or (except ESP/RSP) index.
avoiding REX prefixes in 64-bit code (e.g. mov ebx,1
vs. mov r8d,1
)
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 general; it won't matter (isn't bad practice and won't effect performance); however this can depend on the surrounding code (how the value is used later) and may improve performance or reduce performance.
More specifically, optimal register allocation is difficult to achieve (an NP-complete problem) even when all registers are the same; and 80x86 (where all registers aren't the same in some cases) makes it much harder to achieve optimal register allocation. (And ties register allocation to instruction scheduling, like doing operations in a different order to minimize mov of data into / out of registers where a certain instruction needs them.)