Hand coded assembly - practical register allocation?

311 Views Asked by At

I've never written any long programs in assembly. But from my superficial experience it appears not as hard as people make it seem like.

The only thing I can't quite wrap my head around is: How does one practically carry out register allocation? While in x86 there is not much space, x64 and RISC designs (AVR, ARM) give you a plenty of registers.

How do assembly programmers choose which variables should stay in the registers, when to transfer them from/to memory and finally, how do they keep track of every variable?

1

There are 1 best solutions below

2
On BEST ANSWER

How do assembly programmers choose which variables should stay in the registers

Since registers are (almost always) faster than memory access, variables which are used (read or written) frequently should generally go into registers. An example is the index variable of a loop.

A counterexample is a variable of which you will take the address. That should go into memory since you can't (generally) grab a pointer to a register.

when to transfer them from/to memory

Don't, unless absolutely necessary.

and finally, how do they keep track of every variable?

Hardly. Joke aside, frequent commenting, consistent naming and register allocating conventions, the use of some sort of macro processor (either the assemblers own one or the C preprocessor) and disciplined coding in general make things a little bit easier.