• Microprocessor Video Tutorials

Instruction Sets of a Microcontroller



Microcontrollers are the key components of several electronic devices and embedded systems, and they are brain behind their automated functioning or operation. Today, microcontrollers are being used in devices, ranging from simple home appliances to complex industrial automation systems.

Microcontrollers use several different types of instructions to perform operations; these instructions are collectively known as instruction set. Read this chapter to learn about the instruction sets of a microcontroller and how they work.

What is an Instruction Set?

In microcontrollers, an instruction set is defined as a collection of codes or commands written in machine-level language and processed by the processor of the microcontroller to perform operations.

What is an Instruction Set?

The primary function of instruction sets is to direct the microcontroller to perform tasks like data processing, comparison of values, shifting or moving data from one register to another, performing arithmetic and logic operations, etc.

Different types of microcontrollers such as ARM, AVR, STM32, etc. employes different types of instruction sets to enable flexible and efficient computing.

Types of Microcontroller Instructions

Microcontroller instructions can be classified into several categories depending on their functionality, they are −

  • Data processing instructions
  • Arithmetic instructions
  • Logical instructions
  • Multiply instructions
  • Comparison instructions
  • Move instructions
  • Barrel shifter instructions

Each of these types of microcontroller instructions are explained below in detail.

Data Processing Instructions

As the name implies, those microcontroller instructions which are used for manipulating data withing registers are referred to as data processing instructions. These instructions make the processor capable to perform operations.

The following is a simple example of data processing instruction −

This is a simple subtraction instruction which subtracts the value stored in the register r2 from a value stored in the register r1, and stores the result of subtraction in the register r0.

Before execution of the instruction, we have,
r0 = 0x00000000; Register r0 is empty before execution of the instruction.
r1 = 0x00000005; Register r1 holds a value 5.
r2 = 0x00000002; Register r2 holds a value 2.
SUB r0, r1, r2; r0 = r1  r2
After execution of this instruction, we get,
r0 = 0x00000003;
This is the output of the instruction.

Arithmetic Instructions

Microcontroller instructions which are used to perform and handle basic mathematical operations, i.e., addition, subtraction, and multiplication are termed as arithmetic instructions.

The following table highlights all the key arithmetic instructions used in microcontrollers −

Instruction Description Operation Expression
ADD Add two values Rd = Rn + N
SUB Subtract two values Rd = Rn N
RSB Reverse subtraction Rd = N Rn
ADC Addition with carry Rd = Rn + N + Carry

Let's see an example of reverse subtraction to understand these instructions.

The reverse subtraction instruction or RSB subtracts the value stored in the register r1 from a constant value, let #0, and write the result to the register r0.

Before execution of the instruction, we have,
r0 = 0x00000000; Empty output register.
r1 = 0x00000058; Register with value to be reverse subtracted.
RSB r0, r1, #0; Rd = 0x- - r1;
After execution of the instruction, we get,
r0 = -r1 = -0x00000058; Reverse output generated and stored in r0.

Logical Instructions

In microcontrollers, logical instructions are used to perform bitwise logical operations or Boolean operations to manipulate bits within two source registers.

The following table gives common logical instructions used in microcontrollers −

Instruction Description Operation
AND Bitwise AND Rd = Rn & N
ORR Bitwise OR Rd = Rn | N
EOR Bitwise XOR Rd = Rn ^ N
BIC Bit clear (AND NOT) Rd = Rn & ~N

The following example explains the working of logical instructions in microcontrollers −

This instruction performs logical OR operation on values stored in registers r1 and r2 and stores the result in r0.

Before execution of the instruction,
r0 = 0x00000000;
r1 = 0x05040207;
r2 = 0x20207050;
ORR r0, r1, r2;
After execution of the instruction, we get,
r0 = 0x25247257;

Multiply Instructions

The microcontroller instructions which are used to perform multiplication of values stored in registers and accumulate results withing another register or across multiple registers.

The following are some common multiply instructions used in microcontrollers −

Instruction Description Operation
MUL Multiply two registers Rd = Rm * Rs
MLA Multiply and accumulate Rd = (Rm * Rs) + Rn
SMULL Signed multiplication long [RdHi, RdLo] = Rm * Rs
SMLAL Signed multiply accumulate long [RdHi, RdLo] = [RdHi, RdLo] + (Rm * Rs)
UMULL Unsigned multiply long [RdHi, RdLo] = Rm * Rs
UMLAL Unsigned multiply accumulate long [RdHi, RdLo] = [RdHi, RdLo] + (Rm * Rs)

Let's understand multiply instructions in microcontrollers with the help of an example.

This example shows a simple multiply instruction of two registers namely, r1 and r2, and it stores the result in another register r0.

Before execution of the instruction,
r0 = 0x00000000;
r1 = 0x00000005;
r2 = 0x00000002;
MUL r0, r1, r2; r0 = r1 * r2;
After execution of the instruction, we get,
r0 = 0x00000010;

Comparison Instructions

Comparison instructions in microcontrollers are used to test or compare values within a register. They also update the cpsr flags in the status register of the processor, without affecting other registers.

After setting up the flag bits, information can then be utilized for altering the flow of program execution. Some common comparison instructions used in microcontrollers are given below −

Instruction Description Operation
CMN Compare negated Flags set as a result of Rn + N
CMP Compare Flags set as a result of Rn N
TEQ Test for quality of two 32-bit values Flags set as a result of Rn ^ N
TST Test bits of a 32-bit value Flags set as a result of Rn & N

Let us see an example to understand comparison instructions in microcontrollers.

Before execution of the instruction,
cpsr = nzcvqift_USER
r0 = 2;
r9 = 2;
CMP r0, r9;
After execution of the instruction, we get,
cpsr = nzcvqift_USER; Output produced after comparison.

Move Instructions

Move instructions are those instructions in microcontrollers which used to copy or transfer data between registers.

The following are some common types of move instructions used in microcontrollers −

Instruction Description Operation
MOV Move value into a register Rd = N
MVN Move NOT of value into register Rd = ~N

Let us see an example to understand the working of move instructions in microcontrollers.

Before execution of instruction,
r1 = 2; value stored in register r1.
r2 = 7; value stored in register r2.
MOV r1, r2; r2 = r1;
After execution of instruction, we get,
r1 = 2;
r2 = 2;

Barrel Shifter Instructions

In microcontrollers, the barrel shifter instruction is used for efficient shifting and rotating bits within a register. The following table provides some common barrel shifter instructions used in microcontrollers −

Mnemonics Description Shift Result
LSL Logical shift left xLSLy x << y
LSR Logical shift right xLSRy (Unsigned) x >> y
ASR Arithmetic right shift xASRy (Signed) x >> y
ROR Rotate right xRORy [(Unsigned) x >> y] | (x << (32 y))
RRX Rotate right extended xRRX (c flag << 31) | [(unsigned) x >> 1]

Here, x is the register being shifted and y is the shift amount.

Here is an example to explain the working of barrel shifter instructions in microcontrollers −

Before execution of instruction,
cpsr = nzcvqiFt_USER
r0 = 0x00000000;
r1 = 0x50000002;
MOVS r0, r1, LSL #1;
After execution of the instruction, we get,
cpsr = nzcvqiFt_USER
r0 = 0x00000005;
r1 = 0x50000002;

Advantages of Microcontroller Instruction Sets

The following are some key benefits of instruction sets of a microcontroller –

  • Microcontroller instruction sets are efficient and enables fast execution or processing.
  • Microcontroller instructions require low power to perform tasks, hence they are best suited for embedded systems.
  • Microcontroller instruction sets are versatile, as they support a wide range of operations.

Disadvantages of Microcontroller Instruction Sets

Despite several advantages, the instruction sets of microcontrollers also have some disadvantages, which are as follows −

  • Microcontroller instruction sets offer limited flexibility. Thus, some operations need more than one instruction.
  • Microcontroller instruction sets are hardware dependent and offer compatibility challenges.

Conclusion

In this chapter, we covered some essential types of instruction sets used in microcontrollers. It is important to learn instruction sets for understanding the working of microcontrollers and optimize their performance.

Advertisements