1
votes

I'm initiating me in programming in assembler, I'm making some algorithms and the amount of general purpose registers is not enough for that

Is there any way to use the special registers (eip, eflags) as general purpose registers in asm?

I need them only for a few operations but all general registers are already occupied, I do not like to store values ​​in memory that would decrease the performance of the algorithm.

Is it possible?

5
You can write to eip by doing jmp value. And you can read from it by call foo; foo: pop eax; But the first one probably has side-effects you don't want. :) In short, no you won't be able to use eip and eflags for other purposes. (and most arithmetic instructions will implicitly write to eflags)Mysticial
Pretty much you can't use registers for other than their intended purpose in a practical way. If you are new to assembler, you probably don't know yet how to organize your code to minimize register pressure. And in some places, you're just plain stuck with the demands of the specific algorithm. As a general rule, if you code carefully, your code will mostly run fast enough. Code reasonably, get it running first, then worry about whether it is too slow.Ira Baxter
You can store some bits of ah to eflags with sahf and you can load them to ah with lahf. You can also store data into flags with popf, but this may have unwanted side-effects. And in real-mode code (legacy or boot loader) you can block interrupts with cli, then store esp somewhere, use esp as general-purpose register, load the original value of esp and allow interrupts with sti. You can use eip to store some data with eg. with jmp eax/call eax, if all used values (jump targets) have valid code. Anyway, for practical purposes, you'd better follow Ira Baxter's advice.nrz
While saying "all" general registers, do you mean MMX/SSE ones? E.g. using movq from/to mm0, mm1, etc. is a common trick in BIOS at its early stage when RAM isn't configured yet.Netch

5 Answers

3
votes

Judicious reuse of the general purpose registers already available to you is likely to be your best bet. Think very carefully about your algorithm.

Repurposing EIP isn't possible and using EFLAGS for something else, while perhaps possible, just has too many side effects to be generally useful.

Better would be to actually time your algorithm using memory. You may find that because of cacheing, the access is not as slow as you might have thought.

Another option is to use the math coprocessor's register as simply a small private stack for storage, but this, too is likely to be less satisfactory than simply using memory.

3
votes

Short answer : No. Those registers aren't meant to be written to explicitly.

You'd have to either rewrite your algorithm, better allocate your registers, deal with intelligently spilling/reloading registers to memory to minimize performance impact or, if possible, use XMM registers to either move INT registers to/from, or do chains of instruction all on the XMM side. Another option is to use 64bit to utilize the extra registers made available.

1
votes

The short answer is no.

If you are running a program, using eip as a general purpose register is definitely not possible as if you do "write" to eip, your program will jump into running instructions at that address (roughly), which will probably not work.... As for EFLAGS, it has a number of bits that are either reserved or control various things you dont want changing, meaning again, it will not work as a general purpose register. As you seem to be intent on not using memory to retain values, you could use esp and ebp (being careful if you are at all using the stack). Other than that, most of the registers I can think you using require permission level 0 to write to and again, would have very likely negative consequences if you were to use them as general purpose registers.

There MIGHT be some very special registers that are specific to your processor, but the code would not be portable between processors if you were to use them.

0
votes

I remember that i used the debug register dr0 to dr3 for storing data. But i am not sure how fast they are to use. Maybe the register renaming inside the CPU(for to build micro ops) let it be performant too.

0
votes

Register File is small storage that provides support to just one instruction and is used for intermediate computations. And if you want to extensively use registers, it would kill the purpose of having fast storage such as the registers.

On the other hand, CACHEs are built for the 2nd level of the memory hierarchy that you would like to use. Therefore you need to design algorithms that take full benefit of data locality that are favored by these CACHEs. There are separate caches for instructions that reuse instruction, such as in loops, which are very efficiently implemented.

Even then if one wants to use registers intensively, then intelligent compilers are another option in which case intelligent register allocation techniques are used. A recent hybrid technique uses an off-line machine learning algorithm to build a heuristic function (which is used in run-time) that determines and switches between different allocation methods that will perform better in light of code behavior. I hope it helped. Regards