2

Why do we have a scratch register in ARM Architecture? How the processor uses it, I mean what is the purpose of this register?

fcdt
  • 2,371
  • 5
  • 14
  • 26
Jigar Agrawal
  • 119
  • 2
  • 3

2 Answers2

6

From the Procedure Call Standard for the Arm Architecture:

Scratch register / temporary register A register used to hold an intermediate value during a calculation (usually, such values are not named in the program source and have a limited lifetime).

If you call a function, the values in the scratch regsisters may have been changed after the function call. The caller of the function must therefore ensure that these values are saved if they are still needed. They are also called caller-safe registers, in contrast to the callee-safe registers. These are saved by the called function. The scratch registers are therefore used first if values have to be stored temporarily because they do not have to be saved beforehand.

fcdt
  • 2,371
  • 5
  • 14
  • 26
5

Why do we have a scratch register in ARM Architecture?
I mean what is the purpose of this register?

It is efficient because scratch registers do not need to be saved before use. You need at least one scratch register to do many function prologue and epilogue activities.

How the processor uses it

It is just a regular register to the processor.


There is no 'scratch register' in the ARM. You might refer to it as a 'caller' saved register; but that is the EABI/AAPCS or whatever you might conform to. It is not inside the 'architecture'. It is a convention used for code to inter-operate. This can include OS calls, library calls and tool inter-operation (cross language calls). Different, but very similar, standards may be in effect depending on the software infra-structure/tools you are using.

For traditional ARM, there are four 'caller' saved registers. They are r0-r3. They are also parameters to function and the return value. A routine that is made to inter-operate may use r0-r3 for any purposes even if they are not part of the parameter list. For instance a routine like, void foo(void) will not even need r0, but the routine may use r0 without saving it. The routine that calls foo() must save r0 if it is important to preserve. In this case, the r0-r3 are short lived registers or scratch registers. Contrast to registers r4-r11note which must be saved (on stack) if they are to be used.

The r12 or ip which is generally not special to assembler and can also be treated as a scratch register. r12 is a scratch, but for different reasons than r0-r3. r12 is not used as a parameter register, so it can be used by prologue code (start of routine).

There maybe additional restriction if you want your routine to be used in a 'back trace'. You can mark your routine as 'untraceable' for stack back traces with the pseudo-op .cantunwind. This is not ARM assembler but will be 'meta-data' in an ELF file. In this case, you can use r14 (lr) as a saved register; not a scratch.

In a pure assembly project, all register can be scratch registers. For example the initial code that boots a processor doesn't usually need to save anything and all registers are scratch. An exception might be the stack pointer.

Note: Some systems will special purpose r9, but it is rare. One example might be u-boot. r9 is never a scratch register.

References

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • Also [ARM R12 APCS](https://stackoverflow.com/questions/45359408/r12-in-the-arm-procedure-call-standard) may be interesting to readers of this question. – artless noise Feb 22 '21 at 21:49