10

CPU registers can be classified as volatile and non-volatile by calling convension, how does does the meaning of word volatile implies the classification?

Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
Thomson
  • 20,586
  • 28
  • 90
  • 134

3 Answers3

11

From http://www.techopedia.com/definition/8591/non-volatile-register

Volatile registers' content may change over a subroutine call.

A non-volatile register is a type of register with contents that must be preserved over subroutine calls. Whenever the value of a nonvolatile register is changed by the routine, the old value has to be saved on the stack prior to changing the register and that value has to be restored before returning. A register is similar to a variable, except that there is a fixed number of registers. Every register is a unique location in the CPU in which a single value is saved. A register is the one and only place where mathematical functions, such as addition, multiplication, subtraction, etc., can be carried out. Registers often hold pointers that refer to the memory. Moving values between memory and registers is a common phenomenon.

Achint
  • 136
  • 1
  • 6
  • I never knew there are volatile and non-volatile registers within CPU, What is it used for ? Which CPU has it ? – ART Aug 14 '15 at 04:30
  • 1
    In computer architecture, a processor register is a very fast computer memory used to speed the execution of computer programs by providing quick access to commonly used values-typically, the values being in the midst of a calculation at a given point in time. All CPU have these registers. – Achint Aug 14 '15 at 04:35
5

In other words volatile registers are caller saved registers, as opposed to callee saved. See https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019#callercallee-saved-registers

djsavvy
  • 106
  • 1
  • 2
  • 9
Jacek Tomaka
  • 422
  • 7
  • 15
0

Think about main() in C/C++. You start writing code in main(). After completion of code you compile & link to get the executeable (.exe) file.

In the linking process the c/c++ linker you are using includes the CRT library in the PE File, which provides its own startup code in the mainCRTStartup() function. This startup code initializes the CRT library, calls global initializers, and then calls the user-provided main() function for console applications

You can change the entry point function to something else using proper linker flags. But in case of standard C/C++ file you don't provide that linker flag. In that case the default entrypoint is mainCRTStartup(). Basically mainCRTStartup() initializes the C-Runtime so that you can use standard C/C++ functions like printf puts etc. You cannot use these standard C/C++ function until you intialize the C-Runtime. You can also use the CRT_INIT() function to initialize the CRT manually.

After these initializations mainCRTStartup() calls your main(), upon returning from main() it also does the uninitialization/cleanup of the CRT. mainCRTStartup() is "caller" and main() is "calle" being called by mainCRTStartup().

Even before calling main() the value in RBX let say was 3, to clean the CRT it may need the value 3 in the RBX register. If that value is not being saved then CRT will not get cleaned up. To ensure the cleanup happens main() ie. calee pushes the RBX in the stack so that it can use it and before returning to mainCRTStartup() it pops the value from stack into RBX making sure RBX is preserved across the call. Any register that is being saved by the calee function is called a non-volatile register. In this case RBX is a non-volatile register.

Note that I use RBX here to just explain things. Every ABI has their own set of volatile and non-volatile registers.

Hope I make you understand the concept clearly. I have given this long explanation so that reader can get the importance of making a register as non-volatile.

  • https://stackoverflow.com/questions/9268586/what-are-callee-and-caller-saved-registers#:~:text=Callee%2Dsaved%20registers%20(AKA%20non,should%20be%20preserved%20across%20calls. check this out too – Aniket Bose Jan 17 '23 at 18:58