Cpu Instruction Types


A microcontroller CPU will execute a range of instructions, some of which manipulate data, some of which affect program control or CPU behavior, and some of which perform other actions.  Here is a brief overview of the main types of CPU instructions.  There will of course be variations in the instruction details, how the source operands are accessed, and where the result ends up.  The following list is pretty minimal – different CPU architectures may execute a much richer set of instructions, but will always execute variants of these basics.

DATA MANIPULATION INSTRUCTIONS

Arithemetic Instructions

Arithmetic instructions allow the CPU to add and subtract numbers, and in many cases to multiply and divide numbers. These numbers may be constants, fixed for all time as the program runs, or they may be variable, modifiable values held in data memory or registers.  Signed numbers are just about universally represented in 2s complement form.

• ADD

A=B+C

• SUBTRACT

A=B-C

• COMPARE

B-C

The instruction to compare two values is actually a subtraction instruction, but one in which only the flags are set, with no values being altered.

• NEGATE

A= -B

Takes a signed number and converts it to the negative of that number. Equivalent to A=0-B.

It is important to understand that, in the great majority of cases, your CPU will only have instructions for such arithmetic operations up to its natural word size.  So an 8-bit AVR will have an instruction to add 17+43 (both values fit into 8 bits, as does the result), but not 170,000,000+43,000,000.  The AVR is perfectly capable of performing the latter addition, but it just requires a sequence of instructions, each working on individual 8-bit pieces of the numbers.  If writing in ASM the programmer will have to explicitly write this sequence of instructions.  If writing in C or another language, the compiler will automatically generate the instruction sequence (which you can see by looking at the ASM listing output of the compiled program).

It is also common for CPUs to have increment and decrement instructions, which are shorthand instructions for adding or subtracting 1 (or, in some cases, a small number up to +/- 7 or +/- 15).  Since these operations are so common in programs, it is worthwhile for a CPU to support efficient instructions for them.

• INC and DEC

A = A+1

A = A-1

Logic Instructions

Like the arithmetic instructions, these instructions will typically work on data that is the natural word size of the CPU, or smaller, e.g. 8 bits on an AVR, and 32, 16 or 8 bits on an ARM Cortex M3.  To perform logic operations on larger data requires a sequence of instructions just as is required for arithmetic operations.

Note that all the following examples show data in binary format (a 1 or 0 in each bit position), not decimal format!

• COMPLEMENT (NOT)

X = ~A  (sets every 1 bit in A to 0, and every 0 bit in A to 1)

A=00110110

X=11001001

• AND

X = A & B

produces the bitwise AND of A and B, e.g.

A=00110110

B=01101100

X=00100100  :  1 in every bit position where both A and B are 1, 0 elsewhere

• OR

X = A | B

produces the bitwise OR of A and B, e.g.

A=00110110

B=01101100

X=01111110  :  1 in every bit position where either A and B are 1, 0 elsewhere

• EXCLUSIVE OR

X = A ^ B

produces the bitwise EXCLUSIVE OR of A and B, e.g.

A=00110110

B=01101100

X=01011010:  0 in every bit position where A and B are the same, 1 where they are different

Related Posts

© 2024 Electronics Engineering - Theme by WPEnjoy · Powered by WordPress