How does a zero register improve performance?

前端 未结 4 708
后悔当初
后悔当初 2020-12-03 23:42

In the MIPS ISA, there\'s a zero register ($r0) which always gives a value of zero. This allows the processor to:

  1. Any instruction which produces r
4条回答
  •  误落风尘
    2020-12-03 23:56

    For each of your items, here's an answer.

    1. Consider instructions that compulsory take a register for output, where you want to discard this output. Normally, you'd have to make sure that you have a free register available, and if not, push some of your current registers onto the stack, which is a costly operation. Evidently, it happens a lot that the output of operations is discarded, and the easiest way to deal with this is to have a 'unused' register available.
    2. Now that we have such an unused register, why not use it? It happens a lot that you want to zero-initialize something or compare something to zero. The long way is to first write zero to that register (which requires an extra instruction and the literal for zero in your machine code, which may be of the form 0x00000000 which is rather long) and then use it. So, one instruction shaved off and a little bit of your program size as well.

    These optimizations may seem a bit trivial and may raise the question 'how much does that actually improve anything?' The answer here is that the operations described above are apparently used a lot on your MIPS processor.

提交回复
热议问题