Accumulators and General Purpose Registers
Have you ever heard the terms accumulator machine and register machine?
The origin of the microcomputer is the accumulator machine.This is a method of temporarily storing data used in CPU operations in a register called an accumulator.The register has only one accumulator, so the other side of the operation is the memory (or peripheral module).The result of the operation is again stored in the accumulator and used for the next operation.An accumulator-based CPU is called an accumulator machine.(The name “accumulator” comes from accumulate.)
Unlike this, the register machine has a group of registers called general-purpose registers, and it is possible to use them in the following ways The data to be operated can be stored in any register in the register group.The registers that store operation results can also be selected from a group of registers.
It is obvious that a register machine with more registers for arithmetic can perform arithmetic more efficiently.However, accumulator machines are suitable for inexpensive microcomputers because they are lighter in hardware.The Cortex-M3 is a CPU that pursues high performance, so it is a register machine.It has multiple registers, and these registers can be used for data processing, or indirectly for addressing the memory.
Cortex-M3 registers
The Cortex-M3 processor has special registers in addition to the general-purpose registers. The special registers are mainly used to handle and set interrupts.There are 13 registers that are used purely for data processing and indirect addressing.In addition, there is a program counter (PC), a link register (LR), and a stack pointer (SP).
The special registers include program status registers (xPSR), the interrupt mask registers PRIMASK, FAULTMASK, and BASEPRI, and the control register CONTROL.The details will be explained in a later chapter, but here is a brief description of each register.
General-purpose registers R0 to R12
It is used for data processing and indirect addressing.
Stack Pointer (SP) R13
Indicates the location of the stack.
Link Register (LR) R14
If the subroutine (function is called), the return address is entered.
Program Counter (PC) R15
The address of the instruction that is currently being executed.
Program status registers (xPSR)
The state of Cortex-M3 is shown.The interior is divided into three parts. The application PSR indicates the state of the operation result (zero, negative value, etc.), the interrupt PSR indicates the state of the interrupt, and the execution PSR indicates the state of the execution of the Thumb-2 instruction.
Interrupt Mask Register (PRIMASK, FAULTMASK, BASEPRI)
This is used to disable interrupts (exceptions).
Control Register (CONTROL)
This is used to select the privilege level and stack pointer.
Stacking individual registers
In the chapter on stacking, I mentioned that not all general purpose registers are stacked, but the PUSH and POP instructions can be used to stack arbitrary registers.For example, if you write the following, you can PUSH and POP multiple registers with a single instruction.
PUSH { R3-5 , R11 }
:Stack R3, R4, R5, R11.
POP { R3-5 , R11 }
:R3, R4, R5, R11 back from the stack.
general-purpose register
Because the microcomputer handled for the first time was MC68000 of Motorola, I thought that the general-purpose register method was a matter of course though it was a personal story and I was afraid. (At the time, there was no Arm yet.)
However, I remember being surprised to learn that the accumulator method was the original method for microcomputers.Since programs are written in C these days, users can use accumulators or general-purpose registers without worrying too much about them.When I was making programs in assembler, I used to make programs while thinking about how to use the general-purpose registers, such as “Put this data in R1, then add with R2…” C has become very convenient.
There are 16 general purpose registers of MC68000 in total, and they are used in the same way as Cortex-M3, but the general purpose registers of MC68000 have a division of roles, and D0-7 is for data, and A0-7 is for addressing, etc. There were restrictions on how to use them.However, the general purpose registers in Cortex-M3 have no such limitations as those for data or addressing. Any register can be used for either purpose.
In addition, the register has a convenient side when there is a register with various functions, but there is also a side that is complicated and difficult to use.Conventional Arm registers are shown in the figure below.In Cortex-M3, you can see that it’s very simplified.The simple register configuration is also advantageous because it makes it easier for the compiler to assign variables.
Low Register, High Register
The processor has 13 32-bit general-purpose registers, R0 to R12. There are no special uses defined in the architecture.Most instructions that designate general-purpose registers can use R0 to R12; R0 to R7 are called low registers and R8 to R12 are called high registers.
- Low Register
- R0 to R7 can be accessed by any instruction that specifies a general-purpose register (i.e., a 16-bit instruction in a Thumb-2 instruction or a 32-bit instruction).
- High Register
- R8 to R12 can be accessed by any 32-bit instruction that specifies a general-purpose register. Unlike the low register, however, it cannot be accessed by 16-bit instructions. The default value is indefinite.
R13 (Stack Pointer SP)
R13 is the stack pointer. We discussed this in more detail in the chapter on stacks, so we won’t do it here.
R14 (Link Register LR)
Register R14 is the Link Register (LR) for linking to the subroutine.LR receives a return address from the PC when Branch with Link (BL) or Branch with Link and State Transition (BLX) instructions are executed.LR is also used for exception return. For example, if you create a function in C, a subroutine will be created. The R14 comes in handy in such a situation.Otherwise, R14 can be used as a general-purpose register.
R15 (Program Counter PC)
R15 is the program counter (PC). It refers to the address of the executing instruction, and Cortex-M3 is a three stage pipeline.It performs three stages of processing (fetching, decoding, and execution) in parallel, apparently in one instruction and one cycle.The PC refers to the address of the instruction processing the decoding in the three stage pipeline.
General-purpose registers for various microcomputers
The register configuration differs from microcontroller to microcontroller.In the case of Cortex-M3, there is no distinction between data registers and address registers, so R0 to R12 can be used freely for either purpose.However, some microcomputers have separate data registers and address registers.There are also microcomputers that have address registers even though they are accumulator type. I think that the register configuration is the specification that most expresses the personality of the microcontroller (CPU).Conversely, it is not an exaggeration to say that the composition of the general-purpose register becomes the decisive factor of usability.
“もっと見る” カテゴリーなし
Mbed TLS overview and features
In this article, I'd like to discuss Mbed TLS, which I've touched on a few times in the past, Transport …
What is an “IoT device development platform”?
I started using Mbed because I wanted a microcontroller board that could connect natively to the Internet. At that time, …
Mbed OS overview and features
In this article, I would like to write about one of the components of Arm Mbed, and probably the most …