Man's hand holding CPU chip


Central Processing Unit (CPU) is the brain of computers, it’s what execute programs.

Microcontrollers or personal computers don’t save programs in CPU, programs are saved in memory. Whether SSD, Hard Drive (HD), Read-Only Memory or anywhere else. When you want to execute programs, first you upload the program from memory to Random Access Memory (RAM). CPU then access the instructions from RAM.

Why don’t access programs from memory directly? Why add extra step of loading programs to RAM then access it? Because CPU is very fast, accessing programs from RAM is the fastest way to enhance performance. I mean accessing programs from RAM is faster than HD or even SSD which is considered considerably faster than HD. RAM is 10 to 30 times faster than SSD depending in the models you compare.

In this post I want to talk about registers, which are very important components to execute programs.

I am not here to talk about how programs are executed, I want to talk about registers in CPUs. However I just gave a brief how programs are loaded and executed. I might talk about this process in details in the future.

Also note that I am talking here about ARM processors. Because currently I am studying ARM these days. What you read here could apply to other manufacturers as well to some extent. CPUs differs in many ways like, Von Neuman vs Harvard architecture, RISC vs CISC, bits like 8 or 16 or 32 or 64 bits and other characteristics which define CPU features and capabilities.

So lets talk about registers now.  

What do registers do

This is a picture I took from Arm website. If you look at the far left, R0-R12, SP, LR, PC and CPSR. R0 to R12 are general purpose registers. Stack Pointer (SP), Link Register (LR), Program Counter (PC) and Current Program Status Register (CPSR) are called Special Function Registers (SFRs).

SFRs are set aside from designers to do the functions they are designated to do.

ARM registers are 32 bits long. There bits are either 0 or 1. These bits in a register could hold a value of 74023 for example. This value could mean anything, maybe a variable value in your program, maybe result of 5+48, maybe an address to somewhere inside or outside microcontroller, or maybe an instruction.

Let’s talk about each register type next

General Purpose Register R0-R12

These registers can hold values temporarily. If you have experience in programming, when you define variables and give them values they are saved in registers during execution. The CPU will choose a register and hold the value there. Registers in ARM are 32 bits and unsigned values can range from 0 to 4,294,967,295.

Registers can also hold an address to memory, RAM or other peripherals. Let’s say you want to move a value from somewhere in memory and copy it to CPU, in this case you could use R0 to point to source address where you want to extract a value from, and R1 to copy the value to as a destination.

Program Counter Register (PC)

This is maybe the most important register of all. This register is used to hold the address of the next instruction or code to be executed. You can rephrase that to “this register is used to point to the next instruction.” Which what I am going to use through this article.

When you power on the CPU, PC will start counting. lets say it starts from 0x0000 0020, the CPU will go to address 0x0000 0020 and start fetching instruction from that address. After the execution of the instruction the PC will increment to point to the next address.

But what if we want to get the address from somewhere far from the current PC count? For example, when you call a function, which is usually outside from the main function, you need the PC to point to that function then make the PC return where you left off.

You can use Link register for this purpose.

Link Register (LR)

When PC is incrementing its count to execute instructions one by one, you might want to jump to another location then return to where you left off. The CPU will copy the PC value to LR then in the next step change the PC to the address you want to jump to (maybe a function), once function is executed the LR value is copied back to PC. This last instruction is the last instruction in the function.

Which means when you call a function you must insure that the last instruction is to return where you left off.

Current Program State Register (CPSR)

I will not go to into details in this, but in short this registers hold values related to some arithmetic calculations. For example when calculation happens, is there a carry or not? Depending on that a carry bit will get 0 or 1.

Also a Z bit can test if the a certain value is either 0 or non-zero.

CPSR is also used for if … then instructions and logic operators (greater than >, less than <, equal to = and so on).