Storing variables in CPU registers

1k Views Asked by At

I have read that CPU registers are limited and the value can also get stored in auto.

How to confirm if the variable is stored in a register in C?

What kind of variables can be stored?

1

There are 1 best solutions below

0
Davislor On

Most compilers support a flag such as -S or /S that generates assembly-language output. You can inspect this code to see whether your compiler stores a given variable in a register at a given point.

There is no type guaranteed to fit into a register. In fact, some stack-based machines, including the hardware implementation of JVM in Andrew Tannenbaum’s textbook, do not have explicit registers at all. However, an int usually is the same size as a register (64-bit environments that define int and long as 32-bit for backward compatibility are exceptions), a pointer usually holds a machine address and therefore is usually the same size as a register (segmented memory models such as 16-bit x86, where addresses fit into two registers, are exceptions), and size_t and ptrdiff_t hold array indices and are therefore usually the same size as a register (The x32 target, which has 64-bit code but a 32-bit memory space, is an exception). Most CPUs have floating-point registers that can each hold a double to do math on it, but some don’t.

If what you want is fast portable code, your best bet is to use the types such as int_fast16_t and uint_fast32_t from <stdint.h>. These are guaranteed to be the usually-fastest size that’s at least wide enough. and on normal targets, that’s going to be the size of a machine register.