1. 程式人生 > 其它 >【深入理解計算機系統】 七:AVR指令集架構

【深入理解計算機系統】 七:AVR指令集架構

技術標籤:stackuibuttontagsh.264windbg

7.1. Types of Instruction Sets

The set of instructions that a microprocessor can execute and their structure is generically called itsinstruction set architectureor ISA. The ISA description contains all the details about the execution of each instruction and the effects in the components of the microprocessor. A microprocessor may have a small number of instruction types but a large number of total possible instructions because of the variations allowed in the operands. For example, an instruction to add two numbers may have variations for signed and unsigned numbers, one of the operands is stored in memory, or the result can be stored in one of the general purpose registers.

Most of the information in this chapter is contained in the document8-bit AVR Instruction Setor itsequivalent HTML version.

The decision about what instructions will be possible to execute in a microprocessors is one of the most important and has a profound effect in the performance when executing programs. Furthermore, the set of instructions and the processor architecture are tightly coupled. For example, the operands for most of the instructions are stored in the register file, thus, it must allow for these operands to be accessed efficiently. The AVR architecture, for example, allows various combinations when accessing the operands in the stageRegister Operand Fetch.

The other criteria that influences the type of instructions supported by a microprocessor is the complexity of the implementation. Ultimately, the instructions need to be sequenced by a digital circuit. The higher the complexity of the tasks carried out by an instruction, the higher the complexity of the design. This complexity usually translates into a large number of gates that require a larger physical space (larger chip) and more power consumption.

Consider the following example. Should a processor include as part of its machine language an instruction that given a real number and the coefficients of a second order polynomial obtains its corresponding value? Suppose we call this instructionSDPE(second degree polynomial evaluation). One possible format of this instruction could be:

SDPE a, b, c, n, dest

The instruction would evaluate the polynomial using the three coefficients and the additional value, and store the result in locationdest. The evaluation would calculate the value with the formula:

LaTeX: dest\:\&=\&a\ast n\wedge2\:+b\ast n+c

Although such instruction could be useful, a processor without such instruction could still calculate the same result executing a sequence of instructions to perform the required additions and multiplications. Thus the trade-off to explore when designing a microprocessor is between the complexity of the architecture, and the performance obtained by including certain operations in the ISA. A microprocessor with a reduced set of instructions may require more operations to perform sophisticated calculations, but they are executed faster due to its simplicity. Conversely, a microprocessor with a large number of instructions may require much shorter instruction sequences, but they may take longer to execute due to their complexity.

Over the years microprocessor manufacturers have explored this trade-off and created two categories of systems depending on the complexity of the machine languages.

7.1.1. Complex Instruction Set Computers (CISC)

The processors that provide a rich and complex set of instructions are calledComplex Instruction Set ComputersorCISC. These instructions typically use several operands and may require multiple memory accesses. There are various examples of CISC microprocessors, but perhaps the most popular architectures of this type are Intel’s x86, IA-32, and IA-64 (also known asItanium,x86_64orAMD64.) Thex86architecture was present in the personal computers in 1970s. The architecture was then evolved to the IA-32 (32 bit bus), and then the 64 bit version. These processors are present in numerous desktop computers, servers, and laptops. Other examples of CISC architectures are those in systems such as IBM’s System/360, DEC PDP-11, DEC VAX, or Motorola 68k.

The machine instructions in a CISC computer allow handling multiple operands, some or all of them in memory, and accessed with address manipulations that may require certain arithmetic operations. For example, the following instruction is part of the IA-32 architecture:

ADD $4, 14(%eax, %ebx, 8)

and adds the value 4 to the data in memory stored in the address obtained by adding the value 14, the content of register%eax, and the content of register%ebxmultiplied by 8. As you can see, the execution of this instruction requires more than one arithmetic operation. The addition denoted by the instruction code (ADD) can be performed once the operands are obtained, and additional additions, and even a multiplication is needed to obtain the second operand.

7.1.2. Reduced Instruction Set Computers (RISC)

RISC architectures appeared as an alternative to existing CISC microprocessors. The philosophy is the opposite, microprocessors have a very reduce set of instructions performing each avery simple operation. The idea is to translate this simplicity in the functionality into a faster execution, and a simpler structure in the microprocessor. A reduced execution time does not necessarily translate into a processor that is faster than a CISC architecture. A simple set of instructions means that complex operations will require sequences of instructions, some of them very large.

Some examples of current RISC architectures are:

  • MIPS (Microprocessor without Interlocked Pipeline Stages): used in some routers, Nintendo consoles, original PlayStation, PlayStation 2 and PSP.
  • ARM: present in numerous computers and personal devices such as digital cameras, mobile phones, iPod, etc.
  • SPARC (Scalable Processor ARChitecture): powered the systems sold by Sun Microsystems (now owned by Oracle).
  • PowerPC (Performance OPtimization with Enhanced RISC - Performance Computing): It was created by a consortium of Apple, IBM and Motorola in the early 1990s to be used in personal computers. Not widely used these days.
  • AVR architecture. This architecture is now included in multiple microcontrollers used in embedded systems. Some of the systems in the Arduino family use microprocessors with this architecture.

The comparison between RISC and CISC architectures cannot be done solely in terms of the number of instructions executed per unit of time. With that measure, RISC processors are clear winners. A more exact comparison is to execute the same high level task in two processors and measure the amount of time it takes to finish the task, instead of the number of instructions executed. Very likely, the CISC architecture will execute a smaller number of instructions, but each of them will take longer than the RISC architecture.

7.1.3. Fixed vs Variable Size Instruction Encoding

Another important feature of a microprocessor that can be used to divide them into two categories is the format in which the instructions are encoded. In processors that use afixed lengthformat, every instruction has exactly the same size. This feature has numerous consequences in the design stage. Typically, a small number of operands are allowed in every instruction. A large number of operands would make the format longer, and perhaps not fully used by all the instructions. The number of different instructions is also reduced as a larger number of instructions translates into a larger number of possible cases to encode, and eventually into a larger format. The main advantage of these instructions is in the decoding stage. That is, when the instruction has been loaded in the instruction register, the processor needs to identify which one it is and the type of operands that are required. A fixed format greatly simplifies this stage as the bits encoding the different elements of the instruction are located in the same positions.

The processors with avariable lengthformat, on the other hand, allow instructions to be encoded with different number of bits. Thus, an instruction may have an arbitrary number of operands as they will be encoded with additional bits. The main disadvantage of this approach is during the decoding stage, that is, when the processor needs to identify the type of instruction to execute and obtain all its operands. In these processors, the decoding stage may require loading additional bytes from memory while the instruction is being decoded.

The following video summarizes the differences between CISC and RISC architecture..

7.2. Instruction Format of AVR Architecture

The AVR architecture has afixed lengthformat. All instructions (with only a few exceptions) are encoded with 16 bits. When describing the different components of an instruction, we will be using the following convention for registers and operands:

  • Rd: A register in the register file that will be the destination of the result derived from the instruction.
  • Rr: A register in the register file which will provide one of the operands for the instruction.
  • R: Result of the instruction after its execution.
  • K: A constant value.
  • k: A constantmemory address.
  • b: A bit in a register in the register file or an input/output register.
  • s: One of the bits of the status register.
  • X,Y,Z: 16 bit registers obtained combining two registers in the register file (X=R27:R26,Y=R29:R28,Z=R31:R30).

The following figure shows some of the instruction encoding schemes used by the AVR architecture.

Some of the Instruction Encoding Schemes in the AVR Architecture

Every instruction must have some bits to encode the type of operations that is required. These bits are called theoperation code. The AVR architecture uses a variable number of bits to encode the operation. In its minimum version, the left-most four bits are used.

Instructions for which the operands are one of the 32 general purpose registers, require five bits per operand (

LaTeX: 2^5\:=\:32 25=32combinations, one per register). These bits are not necessarily in contiguous positions in the instructions but this fact makes no difference when decoding the instructions. For example, the following instruction:

ADD R0, R31

is encoded as shown in the following figure:

Encoding of theADDinstruction

The instruction implements the operation

LaTeX: R0\:=\:R0\:+\:R31 R0=R0+R31.Following the convention to describe the AVR ISA, the ADDoperation would be denoted by ADDRd,Rr. Note that the first operand is both a source operand and the destination in which the result is going to be stored. As a consequence, the content of R0before the execution of the instruction is lost. The top part of the previous figure shows the encoding of these instructions for any register number, whereas the bottom part shows the exact encoding for that instruction. As you can see, the five bits in the format with the letter r(source operand) have value 11111referring to register R31. Analogously, the five bits denoted by d(destination operand) have value 00000referring to register R0. The hexadecimal representation of this instruction is 0x0E0F.

Some instructions allow a number as operand instead of a register. These numbers that appear literally in instructions are calledimmediateoperands. The reason for this name will become apparent when theaddressing modesare described. These operands require the number to be encoded with bits that are part of the instruction format. For example, the instruction

CPI R16, 255

is encoded as shown in the following figure:

Encoding of theCPIinstruction

This type of instruction is generically represented byCPIRd,K. This encoding shows some consequences of trying to maintain the format of the instructions of the same size. The operation code are the four left-most bits with value0011, they encode theCPIpart of the instruction.CPIstands forcompare with immediateand the instruction is going to compare the immediate operand (the number 255) with the content of registerR16. Internally, the microprocessor will carry out the operationR16-255and update the status register to reflect the conditions of the result. In other words, instead of storing the value of the operation, only certain conditions are captured in the status register. The hexadecimal representation of this instruction is0x3F0F

Since the instruction is encoded with 16 bits, and four of them are used for the operation code, this means that there are only 12 bits remaining to encode one register and a number. In principle, if we allow any of the 32 general purpose registers to be specified, there would be only 7 bits remaining to encode the immediate operand, leaving only the range of possible values

LaTeX: K Kto satisfy LaTeX: 0\le K\le127 0≤K≤127.However, the designers decided to expand the range of the immediate operand to LaTeX: 0\le K\le255 0≤K≤255requiring eight bitsto encode that operand. This leaves only four bits to encode the register. As a consequence, the instruction CPIcan only use 16 of the 32 general purpose registers. More precisely, this instruction can specify as first operand a register with index LaTeX: d dthe range LaTeX: 16\le d\le31 16≤d≤31.This is an example of the special requirements derived from a fixed length instruction format.

As a consequence, the instructionCPIR7,255would be ruled incorrect because it does not correspond to the encoding in the AVR instruction format.

The third type of operands allowed in some instructions are values that must be used as memory addresses (not as simple numeric values). As in the case of theimmediateoperands, these values need to be encoded using the bits of the instruction which poses a challenge for the fixed format. For example, the instruction

LDS R12, 12565

loads the content of memory in position12565into registerR12. Its binary encoding must include the operation code, the registerR12and the memory address12565. Memory addresses in the AVR architecture have 16 bits. How is it possible to encode such instruction while remaining within the fixed format of 16 bits per instructions? There is no other solution than create an exception in the encoding. TheLDSinstruction is one of the cases in the AVR architecture that uses 32 bits as shown in the following figure:

Encoding of theLDSinstruction

The operation code in this case is represented by 11 bits, the leftmost seven bits, plus the right most four of the first 16 bits. The second 16 bits encode the memory address. These instructions are generically described asLDSRd,k. The hexadecimal representation of this instruction is0x91C03115.

The set of steps taken by the microprocessor to execute this instruction are different from the previous ones. In this case, after obtaining the first 16 bits (hexadecimal0x91C0), the decoding stage detects that is aLDSinstruction and therefore a second memory word needs to be loaded with the address operand. This double access to memory, to fetch the instruction and to obtain the additional operand, will delay its execution time.

The following video shows some examples of how instructions are encoded in the AVR architecture..

7.3. The Assembly Language

To execute the instructions allowed by the architecture, all of them must be encoded with zeros and ones. The description of how to perform this encoding for each instruction is described in the document8-bit AVR Instruction Set. But writing programs using this language and its binary encoding is extremely inconvenient.

The solution for this problem is to define a language with the same instructions, operands and formats as the machine language but instead of using binary logic, use combinations of letters and numbers so that it is understood by a programmer. This language is calledassembly languageand is the notation shown in the previous sections when describing the instructions.

Thus, the assembly language can be defined as a direct alphanumeric representation of the instructions executed by a microprocessor as part of its machine language. The translation between assembly language and a machine instruction is a straightforward application of simple encoding rules to transform each operand into a set of bits of the instruction format.

Consider again the assembly language instruction encoded as0x0E0F. These 16 bits correspond with the symbolADDR0,R31, and the translation process between them is straightforward as shown in the previous section.

In the rest of this section we describe the syntax to write programs in assembly language for the AVR architecture. Each microprocessor has its own set of instructions and formats, thus, the assembly code is highly dependent on the architecture. However, there are several syntactical features that are common to almost all microprocessors. For example, instructions always start with the instructionmnemonic(ADD,CPI,LDS, etc.) followed by the operands separated by commas. The AVR assembly language has the following additional rules:

  • The first operand after the instruction mnemonic is thedestinationoperand.
  • The remaining operands aresourceoperands.
  • Registers are referred by their names which are made by a number between 0 and 31 with the prefixRorr.
  • The assembly instructions are case insensitive. Bothr1orR1refer to the same register, andaddandADDrefer to the same instruction.
  • Numeric constants in an instruction are simply represented as numbers, with no prefix. For exampleCPIR16,255.
  • Memory addresses are typically referred by itslabelwhich must be previously defined in the data section of an assembly program.

7.3.1. Detailed Description of the AVR Instructions

Writing programs in assembly language requires a detailed description of all the instructions executed by the microprocessor. Such description must include all possible formats, operand combinations, and how the operations are executed. This information is typically included in documents known asprogramming manuals, orinstruction set architecture manual.

In the case of the AVR architecture, this detailed description is included in the 152 pages document titledAtmel AVR 8-bit Instruction Set.

As an example of the information included in this document, the description of the instructionADD(add without carry) includes the following details (see page 16 of the instruction set manual):

Example:

6. ADD - Add without Carry

6.1 Description

Adds two registers without the C Flag and places the result in the destination register Rd.

Operation:

LaTeX: Rd\longleftarrow Rd+Rr Rd⟵Rd+Rr

Syntax:ADDRd,Rr

Operands:

LaTeX: 0\le d\le31,\:0\le r\le31 0≤d≤31,0≤r≤31

Program Counter:

LaTeX: PC\longleftarrow PC+1 PC⟵PC+1

16-bit Opcode:

6.2 Status Register (SREG) and Boolean Formula

H:

LaTeX: Rd3\cdot Rr3+Rr3\cdot \overline{R3}+\overline{R3}\cdot Rd3 Rd3⋅Rr3+Rr3⋅R3¯+R3¯⋅Rd3

Set if there was a carry from bit 3; cleared otherwise.

V:

LaTeX: Rd7\cdot Rr7\cdot \overline{R7}+\overline{Rd7}\cdot \overline{Rr7}\cdot R7 Rd7⋅Rr7⋅R7¯+Rd7¯⋅Rr7¯⋅R7

Set if two’s complement overflow resulted from the operation; cleared otherwise.

N

LaTeX: R7 R7

Set if MSB of the result is set; cleared otherwise.

Z:

LaTeX: \overline{R7}\cdot \overline{R6}\cdot \overline{R5}\cdot \overline{R4}\cdot \overline{R3}\cdot \overline{R2}\cdot \overline{R1}\cdot \overline{R0} R7¯⋅R6¯⋅R5¯⋅R4¯⋅R3¯⋅R2¯⋅R1¯⋅R0¯

Set if the result is$00; cleared otherwise.

C:

LaTeX: Rd7\cdot Rr7+Rr7\cdot \overline{R7}+\overline{R7}\cdot Rd7 Rd7⋅Rr7+Rr7⋅R7¯+R7¯⋅Rd7

Set if there was carry from the MSB of the result; cleared otherwise.

R (Result) equals Rd after the operation.

Example:

add r1, r2   ; Add r2 to r1 (r1 = r1 + r2)
add r28, r28 ; Add r28 to itself (r28 = r28 + r28)

Words:1 (2 bytes)

Cycles:1

As you can see, this page includes comprehensive information about the operation carried out by the instruction, its syntax, binary encoding, and the effect of its execution over the status register. The description also includes the size of its encoding, and the time it takes to execute, in this case, 1 cycle. All of the 124 instructions of the set are described with this level of detail.

A sequence of instructions in an assembly program is simply written with one instruction per line as shown in the following example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
	clr R24
	push R24
	mov R24, R3
	push R24
        ldi R24, lo8(msg)
        ldi R25, hi8(msg)
	push R25
	push R24
        call printf
	pop R0

Although the rules to write programs in assembly code are all contained in the description of its instruction set, there is some additional functionality that is required to define and declare data, variables and functions. These definitions can then be used as symbols within a program as it can be seen in the use of the labelsmsgandprintfin the previous example denoting respectively a variable and a function.

7.4. Subset of Instructions of the AVR Architecture

This section includes the description of a subset of the instructions of the AVR architecture. A description of all the instructions can be found in the document 8-bit AVR Instruction Set. The instructions have been divided in to seven categories: data transfer, arithmetic, logic, shift/rotate, compare, jump/branch, subroutine call/return, and input/output.

7.4.1. Instruction to transfer data

These are instructions to move data from one location to another. The possible locations are memory or general purpose registers.

MOV: Move values between registers

Instruction that makes a copy of one register into another.

Syntax:MOVRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: Rd\longleftarrow Rr Rd⟵Rr

No bits in the status register are modified.

Example:

MOV R1, R2    ;; Copy the value of R2 in R1
mov r1, r2    ;; The instruction is case insensitive

LDI: Load an immediate (constant) in a register

Instruction that loads an eight bit constant value in one of the general purpose registersR16toR31.

Syntax:LDIRd,K, where

LaTeX: 16\le d\le31,0\le k\le255. 16≤d≤31,0≤k≤255.

Operations:

LaTeX: Rd\longleftarrow K Rd⟵K

No bits in the status register are modified.

Example:

LDI r16, 34    ;; Load 34 in r16

LD: Load Indirect from Data Space to Register

Instruction that loads one byte from the memory address in the data space contained in one of the 16 bits registersX,YorZto a register. The value of the memory address can bepost-incremented, orpre-decremented. The register must have the appropriate address loaded before the instruction is executed.

Syntax:

  • LDRd,X;LDRd,Y;LDRd,Z
  • LDRd,X+;LDRd,Y+;LDRd,Z+,
  • LDRd,-X;LDRd,-Y;LDRd,-Z

where

LaTeX: 0\le d\le31 0≤d≤31

Operations:

  • LaTeX: Rd\longleftarrow\left(X\right),Rd\longleftarrow\left(Y\right),Rd\longleftarrow\left(Z\right) Rd⟵(X),Rd⟵(Y),Rd⟵(Z)
  • LaTeX: Rd\longleftarrow\left(X\right),X\longleftarrow X+1;Rd\longleftarrow\left(Y\right),Y\longleftarrow Y+1;Rd\longleftarrow\left(Z\right),Z\longleftarrow Z+1 Rd⟵(X),X⟵X+1;Rd⟵(Y),Y⟵Y+1;Rd⟵(Z),Z⟵Z+1
  • LaTeX: X\longleftarrow X+1,\:Rd\longleftarrow\left(X\right);\:Y\longleftarrow Y-1,\:Rd\longleftarrow\left(Y\right);\:Z\longleftarrow Z+1,\:Rd\longleftarrow\left(Z\right) X⟵X+1,Rd⟵(X);Y⟵Y−1,Rd⟵(Y);Z⟵Z+1,Rd⟵(Z)

No bits in the status register are modified.

Example:

ld r23, x
ld r24, y+
ld r25, -z

LDS: Load Directly from the Data Space

Instruction that loads one byte from the data space directly from a given address. The address is typically given as a string which has been previously defined in the data section of a program.

Syntax:

LDSRd,k

where

LaTeX: 0\le d\le31,0\le k\le65535 0≤d≤31,0≤k≤65535

Operations:

LaTeX: Rd\longleftarrow\left(k\right) Rd⟵(k)

No bits in the status register are modified.

Example:

lds r3, maximum    ;; Load the byte in label maximum into r3

LDD: Load Indirect + Displacement from Data Space to Register

Instruction that loads one byte from the memory address in the data space to a register. The address is computed adding the register and the displacement expressed as a natural number

LaTeX: d dsuch that LaTeX: 0\le d\le63 0≤d≤63.

Syntax:

  • LDDRd,Y+q,
  • LDDRd,Z+q,

where

LaTeX: 0\le d\le31,0\le q\le63 0≤d≤31,0≤q≤63

Operations:

  • LaTeX: Rd\longleftarrow\left(Y+q\right) Rd⟵(Y+q)
  • LaTeX: Rd\longleftarrow\left(Z+q\right) Rd⟵(Z+q)

No bits in the status register are modified.

Example:

ldd r25, y+12  ;; Load r25 with the value pointed by y + 12
ldd r31, z+63  ;; Load r31 with the value pointed by z + 63

ST: Store byte

Instruction that stores the byte in the second operand in the address pointed by the registerX,Y, orZ. The value of the memory address can bepost-incremented, orpre-decremented. The register must have the appropriate address loaded before the instruction is executed.

Syntax:

  • STX,Rr;STY,Rr;STZ,Rr
  • STX+,Rr;STY+,Rr;STZ+,Rr
  • ST-X,Rr;ST-Y,Rr;ST-Z,Rr

where

LaTeX: 0\le r\le31 0≤r≤31

Operations:

  • LaTeX: \left(X\right)\longleftarrow Rr;\:\left(Y\right)\longleftarrow Rr;\:\left(Z\right)\longleftarrow Rr (X)⟵Rr;(Y)⟵Rr;(Z)⟵Rr
  • LaTeX: \left(X\right)\longleftarrow Rr;\:X\longleftarrow X+1;\:\left(Y\right)\longleftarrow Rr;\:Y\longleftarrow Y+1;\:\left(Z\right)\longleftarrow Rr;\:Z\longleftarrow Z+1 (X)⟵Rr;X⟵X+1;(Y)⟵Rr;Y⟵Y+1;(Z)⟵Rr;Z⟵Z+1
  • LaTeX: X\longleftarrow X-1,\:\left(X\right)\longleftarrow Rr;\:Y\longleftarrow Y-1,\:\left(Y\right)\longleftarrow Rr;\:Z\longleftarrow Z-1,\left(Z\right)\longleftarrow Rr X⟵X−1,(X)⟵Rr;Y⟵Y−1,(Y)⟵Rr;Z⟵Z−1,(Z)⟵Rr

No bits in the status register are modified.

Example:

st x, r25
st y+, r25
st -z, r25

STD: Store from a Register into Indirect + Displacement Data Space

Instruction that stores one byte from the given register in memory address in the data space. The memory address is computed adding the register and the displacement expressed as a natural number

LaTeX: d dsuch that LaTeX: 0\le d\le63 0≤d≤63.

Syntax:

  • STDY+q,Rr,
  • STDZ+q,Rr,

where

LaTeX: 0\le31,\:0\le q\le63 0≤31,0≤q≤63

Operations:

  • LaTeX: \left(Y+q\right)\longleftarrow Rr (Y+q)⟵Rr
  • LaTeX: \left(Z+q\right)\longleftarrow Rr (Z+q)⟵Rr

No bits in the status register are modified.

Example:

std y+12, r25  ;; Store r25 in the address pointed by y + 12
std z+63, r31  ;; Store r31 in the address pointed by z + 63

STS: Store a register directly in memory

Instruction that stores one byte from a register into the data memory in an address directly included in the instruction. The address is typically a symbol that has been previously defined.

Syntax:

STSk,Rr

where

LaTeX: 0\le r\le31,0\le k\le65535 0≤r≤31,0≤k≤65535

Operations:

LaTeX: \left(k\right)\longleftarrow Rr (k)⟵Rr

Example:

STS maximum, R2  ;; Store R2 in label maximum in memory.

PUSH: Store the register value in the stack

Instruction that stores the value in the given register in the stack and updates the value of the stack pointer (SP).

Syntax:PUSHRrwhere

LaTeX: 0\le r\le31 0≤r≤31

Operations:

LaTeX: SP\longleftarrow SP-1,\:\left(SP\right)\longleftarrow Rr SP⟵SP−1,(SP)⟵Rr

No bits in the status register are modified.

Example:

PUSH R14  ;; Store R14 at the top of the stack

POP: Load the register with the value at the top of the stack

Instruction that loads the value at the top of the stack, pointed by the stack pointer (SP) in the given register. The stack pointer is also updated to keep pointing to the new top of the stack.

Syntax:POPRdwhere

LaTeX: 0\le r\le31 0≤r≤31

Operations:

LaTeX: Rd\longleftarrow\left(SP\right),\:SP\longleftarrow SP+1 Rd⟵(SP),SP⟵SP+1

No bits in the status register are modified.

Example:

POP R14  ;; Restore the value of R14 from the stack

7.4.2. Arithmetic Instructions

ADD: Add two registers

Instruction that adds two registers and places the result in the destination one.

Syntax:ADDRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: Rd\longleftarrow Rd+Rr Rd⟵Rd+Rr

The bits H, S, V, N, Z and C of the status register are modified.

Example:

ADD R1, R2    ;; Add R1 and R2 and leave the result in R1

SUB: Subtract two registers

Instruction that subtracts the second register from the first and leaves the result in the first.

Syntax:SUBRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: Rd\longleftarrow Rd-Rr Rd⟵Rd−Rr

The bits H, S, V, N, Z and C of the status register are modified.

Example:

sub r3, r4    ;; Store in R3, the result of R3 - R4

SUBI: Subtract an 8-bit constant from a register

Instruction that subtracts the second operand, an 8-bit constant from the register and leaves the result in the register.

Syntax:SUBIRd,Kwhere

LaTeX: 16\le d\le31,0\le k\le255 16≤d≤31,0≤k≤255

Operations:

LaTeX: Rd\longleftarrow Rd-k Rd⟵Rd−k

The bits H, S, V, N, Z and C of the status register are modified.

Example:

subi r23, 14    ;; Store in R23, the result of R23 - 14

INC: Increment

Instruction that adds one to the content of the given register and places the result in the same register.

Syntax:INCRdwhere

LaTeX: 0\le d\le31 0≤d≤31

Operations:

LaTeX: Rd\longleftarrow Rd+1 Rd⟵Rd+1

The bits S, V, N, Z and C from the status registers are updated.

Example:

INC R26

DEC: Decrement

Instruction that subtract one from the content of the given register and leaves the result in the same resister.

Syntax:DECRdwhere

LaTeX: 0\le d\le31 0≤d≤31

Operations:

LaTeX: Rd\longleftarrow Rd-1 Rd⟵Rd−1

The bits S, V, N, Z and C from the status registers are updated.

Example:

DEC R5

NEG: Negate the value in a register

Instruction that changes the sign of the value in a register

Syntax:NEGRdwhere

LaTeX: 0\le d\le31 0≤d≤31

Operations:

LaTeX: Rd\longleftarrow0-Rd Rd⟵0−Rd

The bits H, S, V, N, Z, and C are updated.

Example:

NEG R22  ;; Change the sign of the value in register R22.

MUL: Unsigned Multiplication

Instruction that performs the multiplication of two unsigned 8-bit values. The 16-bit unsigned product isalwaysstored in registersR1(the high byte), andR0(the low byte).

Syntax:MULRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: R1 R1: LaTeX: R0\longleftarrow Rd\times Rr R0⟵Rd×Rr

The bits Z and C of the status register are updated.

Example:

MUL R5, R7    ;; The 16 bit product is stored in R1:R0

7.4.3. Logic Instructions

Logic instructions perform what is known asbit-wiselogic operations. In other words, the operation is performed in all 8 bits of the operands by paring the bits in the same position for both operands (when appropriate) and performing the basic operation.

AND: Logical Conjunction

Instruction that performs the logical AND between the content of the two given registers.

Syntax:

ANDRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: Rd\longleftarrow Rd\cdot Rr Rd⟵Rd⋅Rr

The bits S, N, Z and C of the status register are updated, and the bit V is set to zero.

Example:

AND R16, R7    ;; Compute the conjunction and leave result in R16

OR: Logical Disjunction

Instruction that performs the logical OR between the content of the two given registers.

Syntax:

ORRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: Rd\longleftarrow Rd\vee Rr Rd⟵Rd∨Rr

The bits S, N, Z and C of the status register are updated, and the bit V is set to zero.

Example:

OR R16, R7    ;; Compute the disjunction and leave result in R16

EOR: Exclusive OR

Instruction that performs the logical exclusive OR between the content of the two given registers.

Syntax:

EORRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: Rd\longleftarrow Rd\oplus Rr Rd⟵Rd⊕Rr

The bits S, N, Z and C of the status register are updated, and the bit V is set to zero.

Example:

EOR R16, R7    ;; Compute the exclusive or and leave result in R16

7.4.4. Shift and Rotate Instructions

Shift and rotate instructions are used to access the bits inside a byte independently. By shifting and rotation their positions, they are prepared to be then processed by other instructions.

LSR/LSL: Logical Shift Right/Left

Instruction that shifts all bits in the given register by one place to the right/left. The instructionLSRclears bit 7 of the register, and bit 0 is loaded with the value of the C (carry) flag of the status register. This operation is equivalent to divide the unsigned value in the register by two. The instructionLSLclears bit 0 of the register, and bit 7 is loaded into the C flag of the status register. This operation is the same as multiplying signed and unsigned values by two.

Syntax:

  • LSRRd
  • LSLRd

where

LaTeX: 0\le d\le31 0≤d≤31

Operations:

The bits H, S, V, N, Z and C of the status register are updated.

Example:

LSL R12    ;; Shift R12 to the left (multiply by 2)
LSR R15    ;; Shift R15 to the right (divide by 2)

ROR/ROL: Rotate Right/Left through Carry

Instruction that treats its operand as a circular register by concatenating the carry bit of the status register. The instructions shift the carry bit C into bit 7/bit 0. The instruction can be used to multiply multi-byte numbers by two.

Syntax:

  • RORRd
  • ROLRd

where

LaTeX: 0\le d\le31 0≤d≤31

Operations:

The bits H, S, V, N, Z and C of the status register are updated.

Example:

ROL R12    ;; Rotate R12 to the left
ROR R15    ;; Rotate R15 to the right

ASR: Arithmetic Shift Right

Shifts all bits in Rd one place to the right. Bit 7 is held constant, and bit 0 is loaded into the C flag in the status register. This instruction is equivalent to divide a number by two while maintaining its sign.

Syntax:

ASRRdwhere

LaTeX: 0\le d\le31 0≤d≤31

Operations:

The bits S, V, N, Z and C of the status register are updated.

Example:

ASR R31      ;; Shift to the right (multiply by 2 and propagate sign)

7.4.5. Compare Instructions

The following two instructions are used when a comparison is needed between two numbers. This type of operations are very common in assembly programs. For example, when evaluating the condition of an iterative structure (a loop), the comparison is used to decide if a new iteration is required. Two comparison instructions are presented: one that compares two registers, and another that compares one register with a constant.

CP: Compare

Instruction that performs a comparison between two registers. The processor performs a subtraction between the two operandsbut the result is not storedin a general purpose register, but only modifies the flags of the status register.

Syntax:CPRd,Rrwhere

LaTeX: 0\le d\le31,0\le r\le31 0≤d≤31,0≤r≤31

Operations:

LaTeX: Rd-Rr Rd−Rr

The bits H, S, V, N, Z and C of the status register are modified.

Example:

CP R1, R5    ;; R5 - R1 is calculated and reflected in the status register

CPI: Compare with Immediate

Instruction that performs a comparison between a register and a 8-bit constant.The result is not storedin a general purpose register, but only modifies the flags of the status register.

Syntax:CPRd,Kwhere

LaTeX: 16\le d\le31,0\le k\le255 16≤d≤31,0≤k≤255

Operations:

LaTeX: Rd-K Rd−K

The bits H, S, V, N, Z and C of the status register are modified.

Example:

CPI R16, 5    ;; Subtract R16 - 5 and updates the status register

7.4.6. Jump and Branch Instructions

The set of instructions to jump and branch is one of the most important in the instruction set architecture. They are used to change the sequence of instruction execution and are the building blocks to implement high level constructions such as conditionals and loops. The difference between the jump and branch instructions is that the first ones always jump to a new location in the code, whereas the second change the sequenceonly if a specific condition is met. The branch instructions are also known asconditional branchorconditional jumpinstructions. Processors typically include a large set of these instructions to include as many conditions as possible.

JMP: Jump

Instruction that jumps to an addresskin the program memory.

Syntax:JMPkwhere

LaTeX: 0\le k<2^{22} 0≤k<222

Operations:

LaTeX: PC\longleftarrow k PC⟵k

No bits in the status register are modified.

Example:

        JMP dest     ;; Jump to location with label dest
        ....
        ....
dest:   ADD R1, R2   ;; Destination of the jum

BREQ/BRNE: Branch if Equal/Not Equal

Instructions that jump to the specified location in the code if the previous comparison or operation resulted in a zero result. The instructions check the value of the Z flag in the status register. The destination of the jump is supposed to be at a short distance of this instruction (no more than 63 instructions before or after).

Syntax:

  • BREQk
  • BRNEk

where

LaTeX: -64\le\:k\:\le63 −64≤k≤63

Operations:

  • LaTeX: if\:\left(Z=1\right)\:then\:PC\longleftarrow PC+k+1,\:else\:PC\longleftarrow PC+1 if(Z=1)thenPC⟵PC+k+1,elsePC⟵PC+1
  • LaTeX: if\:\left(Z=0\right)\:then\:PC\longleftarrow PC+k+1,\:else\:PC\longleftarrow PC+1 if(Z=0)thenPC⟵PC+k+1,elsePC⟵PC+1

No bits in the status register are modified.

Example:

        CPI R27, 5    ;; Compare R27 with number 5
        BRNE done     ;; Branch if R27 is different from 5
        ...
        ...
done:   ADD ...       ;; Destination of the branch

BRSH/BRLO: Branch if same or higher/lower (Unsigned)

Instructions that branch to a specific location if the previous operation has set the carry flag (C) to zero or one respectively. If preceded by a comparison operation, these instructions branch if the first operand of the comparison satisfies the condition specified in the branch with respect to the second operandboth interpreted as unsigned integers. For example, in the sequence:

CPI R23, 15
BRSH dest     ;; Branch if R23 same or higher than 15

the program will branch if R23 (as unsigned integer) issame or higherthan 15.

Syntax:

  • BRSHk
  • BRLOk

where

LaTeX: -64\le k\le63 −64≤k≤63

Operations:

  • LaTeX: if\left(C=0\right)\:then\:PC\longleftarrow PC+k+1,\:else\:PC\longleftarrow PC+1 if(C=0)thenPC⟵PC+k+1,elsePC⟵PC+1
  • LaTeX: if\left(C=1\right)\:then\:PC\longleftarrow PC+k+1,\:else\:PC\longleftarrow PC+1 if(C=1)thenPC⟵PC+k+1,elsePC⟵PC+1

No bits in the status register are modified.

Example:

        CPI R23, 15
        BRLO dest     ;; Branch if R23 is lower than 15 as unsigned
        ...
        ...
        ...
dest:   ADD ...

BRGE/BRLT: Branch if greater or equal/lower (Signed)

Instructions that branch to a specific location if the previous operation has set the sign flag (S) to zero or one respectively. If preceded by a comparison operation, these instructions branch if the first operand of the comparison satisfies the condition specified in the branch with respect to the second operandboth interpreted as signed integers. For example, in the sequence:

CPI R23, -15
BRGE dest     ;; Branch if R23 same is greater than -15

the program will branch if R23 (as signed integer) issame or higherthan -15.

Syntax:

  • BRGEk
  • BRLTk

where

LaTeX: -64\le k\le63 −64≤k≤63

Operations:

  • LaTeX: if\left(S=0\right)\:then\:PC\longleftarrow PC+k+1,\:else\:PC\longleftarrow PC+1 if(S=0)thenPC⟵PC+k+1,elsePC⟵PC+1
  • LaTeX: if\left(S=1\right)\:then\:PC\longleftarrow PC+k+1,\:else\:PC\longleftarrow PC+1 if(S=1)thenPC⟵PC+k+1,elsePC⟵PC+1

No bits in the status register are modified.

Example:

        CPI R23, -15
        BRLT dest     ;; Branch if R23 is less than -15 as signed int.
        ...
        ...
        ...
dest:   ADD ...

7.4.7. Compare and Branch Sequences

As mentioned before, the sequence of two instructions in which the first one compares two operands and the second is a conditional branch instruction is executed very frequently by the processor. These two-instruction sequences are the building block to implement the change in execution flow in high level constructions such as if-then-else (conditionals) or loops.

It is very important to understand how conditions are calculated, reduced to a comparison, and the result used to decide if a branch is occurring or not. When writing assembly code, there is a simple rule to remember how these instructions are interpreted. If we assume that there are two operandsAandBcompared using one of thecompare and test instructionsLinks to an external site.followed by a conditional branch such that:

CPI A, B
BR[CONDITION] destination

the code will branch todestinationif the conditionACONDITIONBis satisfied. For example, if the branch instruction isBRGE, then the processor will branch ifAis greater or equal thanB.

For example, the high level programming fragment:

if (x < 12) {
    [Code]
}

can be implemented in assembly code using the compare and branch instructions as follows (assuming that the valuexis an integer and has been previously loaded in registerR24):

        CPI R24, 12
        BRGE done    ;; Skip the block if R24 is greater or equal to 12
        ...
        ...  ;; Instructions implemented the [Code]  block
        ...
done:   ...  ;; Instructions following the if-then construction

7.4.8. Subroutine Call and Return

The instructions to call and return from a subroutine are also fundamental to implement function calls. Both instructions make use of the stack to store the address from which the call is being made, and where the subroutine must return upon termination. The implicit assumption is that function calls are perfectly nested, that is, a subroutine may call inside another subroutine that upon termination returns to the point where the first was executing.

The reason to use the stack to store the return address is derived from the existence of recursive functions. A program may have a single routine, method, or function, but during execution, the function may call itself, thus requiring to keep an arbitrary number of return addresses (as many as function invocations currently executing). A fixed location to store the return address would not allow functions to call themselves. The stack solves this problem by copying the return address as many times as needed in a specific order.

CALL: Call a subroutine

Instruction that calls a subroutine located in any other position of the program memory. The return address (the address of the instruction following the call) is stored in the stack (STACK). The stack pointer (SP) is decremented to point to the return address.

Syntax:CALLkwhere

LaTeX: 0\le k\le2^{22} 0≤k≤222

Operations:

LaTeX: PC\longleftarrow k,\:SP\longleftarrow SP-2,\:STACK\longleftarrow PC+2 PC⟵k,SP⟵SP−2,STACK⟵PC+2

No bits in the status register are modified.

Example:

        CALL funct     ;; Call the function
        ...
        ...
funct:  ...            ;; First instruction of the function

RET: Return from subroutine

Instruction that returns to the instruction following the instructionCALLlast invoked. The instruction has no operands, as they areimplicit. The address is assumed to be stored at the top of the stack. The instruction modifies the stack (STACK) to remove this value and adjust the stack pointer (SP).

Syntax:

ret

Operations:

LaTeX: PC\longleftarrow STACK,\:SP\longleftarrow SP+2 PC⟵STACK,SP⟵SP+2

No bits in the status register are modified.

Example:

        CALL funct  ;; Call the function
        ...         ;; Instruction to return
        ...
funct:  ...         ;; First instruction of the function
        ...
        RET         ;; Return to the point after funct was invoked

7.4.9. Input and Output Instructions

The input and output instructions are similar to the load and store with the difference that data is transferred from and to the input/output ports. These ports are denoted by their address.

IN: Load (reads) data from I/O space into register

Instruction that reads the value of an input port in location given as second parameter, and stores the value in the register given as first parameter.

Syntax:INRd,Awhere

LaTeX: 0\le d\le31 0≤d≤31and Ais the address of an input port.

Operations:

LaTeX: Rd\longleftarrow I/O\left[A\right] Rd⟵I/O[A]

No bits in the status register are modified.

Example:

in r12, 0x3E     ;; Load the data in port 0x3E in r12

OUT: Stores (writes) data from a register in I/O space

Instruction that stores or writes the data in the register given as second parameter in the output location given as first parameter.

Syntax:OUTA,Rdwhere

LaTeX: 0\le d\le31 0≤d≤31and Ais the address of an input port.

Operations:

LaTeX: I/O\left[A\right]\longleftarrow Rd I/O[A]⟵Rd

No bits in the status register are modified.

Example:

out 0x3D, r13     ;; Write the data in r12 to port 0x3E

7.5. Summary of Instructions

The subset of instructions considered in this document is summarized in the following tables. You maydownloadaPDFwiththesetablesforyourconvenience.

Preview the document

Data Transfer Instructions
MnemonicOperandsDescriptionOperation
MOVRd, RrMove Rr to RdRd ← Rr
LDIRd, KLoad K in RdRd ← K
Rd must be from R16 to R31, 0 ⩽ K ⩽ 255
LDRd, XLoad (X) in RdRd ← (X)
Rd, X+Load (X) in Rd, X is incrementedRd ← (X), X ← X + 1
Rd, -XX is decremented, Load (X) in RdX ← X - 1, Rd ← (X)
All three registers X, Y and Z can be used
LDDRd, Y+qRd is loaded with (Y + q)Rd ← (Y + q)
Register Z can also be used. Register X cannot be used. q has to be between 0 and 63
LDSRd, kLoad Rd with data in position k in memory.Rd ← (k)
k cannot go beyond 65535
STX, RrStore Rr in (X)(X) ← Rr
X+, RdStore Rr in (X), X is incremented(X) ← Rr, X ← X + 1
-X, RdX is decremented, Store Rr in (X)X ← X - 1, (X) ← Rr
All three registers X, Y and Z can be used
STDY+q, Rr(Y + q) stores Rr(Y + q) ← Rr
Register Z can also be used. Register X cannot be used. q has to be between 0 and 63
STSk, RrStore Rr in position k in memory.(k) ← Rd
k must satisfy 0 ⩽ k ⩽ 65535
PUSHRrPush Rr to stackSP ← SP - 1, (SP) ← Rr
POPRdPop Rd from stackRd ← (SP), SP ← SP + 1
Arithmetic Instructions
MnemonicOperandsDescriptionOperation
ADDRd, RrAdd Rr to RdRd ← Rd + Rr
SUBRd, RrSubtract Rr from RdRd ← Rd - Rr
SUBIRd, KSubtract K from RdRd ← Rd - K
K must be between 0 and 255
INCRdIncrement RdRd ← Rd + 1
DECRdDecrement RdRd ← Rd - 1
NEGRdChange sign of RdRd ← 0 - Rd
MULRd, RrUnsigned multiply

R1:R0 ← Rd * Rr

Logic Instructions
MnemonicOperandsDescriptionOperation
ANDRd, RrConjunction of Rd and RrRd ← Rd * Rr
ORRd, RrDisjunction of Rd and RrRd ← Rd + Rr
EORRd, RrExclusive OR of Rd and Rr

Rd ← Rd ⊕ Rr

Rotate and Shift Instructions
MnemonicOperandsDescriptionOperation
LSL/LSRRdShift left/rightShift into C flag
ROL/RORRdRotate left/rightRotate with C flag
ASRRdArithmetic shift rightSign extended shift
Compare Instructions
MnemonicOperandsDescriptionOperation
CPRd, RrSubtract and update flagsRd - Rr
CPIRd, KSubtract K and update flagsRd - K
K must be between 0 and 255
Jump and Branch Instructions
MnemonicOperandsDescriptionOperation
JMPkGo to instruction in position kPC ← k
BREQ/BRNEkBranch if equal/ not equal to instruction in position kIf (Z = 1/0) then PC ← PC + k+1 otherwise PC ← PC + 1
k must satisfy -64 ⩽ k ⩽ 63
BRSH/BRLOkBranch if same or higher/lower (unsigned) to instruction in position kIf (C = 0/1) then PC ← PC + k+1 otherwise PC ← PC + 1
k must satisfy -64 ⩽ k ⩽ 63
BRGE/BRLTkBranch if greater or equal/lower (signed) to instruction in position kIf (S = 0/1) then PC ← PC + k+1 otherwise PC ← PC + 1
k must satisfy -64 ⩽ k ⩽ 63
Call and Return Instructions
MnemonicOperandsDescriptionOperation
CALLkCall subroutine in position kPC ← k, SP ← SP - 2, (SP) ← PC + 2
RETReturn from subroutine

PC ← (SP), SP ← SP + 2

Input and Output instructions
MnemonicOperandsDescriptionOperation
INRd, ALoad I/O data from address A to register RdRd ← I/O[A]
OUTA, RdStore value in register Rd in I/O address AI/O[A] ← Rd

The following video shows a summary of some of the instructions provided by the AVR architecture..

7.1. Types of Instruction Sets

The set of instructions that a microprocessor can execute and their structure is generically called itsinstruction set architectureor ISA. The ISA description contains all the details about the execution of each instruction and the effects in the components of the microprocessor. A microprocessor may have a small number of instruction types but a large number of total possible instructions because of the variations allowed in the operands. For example, an instruction to add two numbers may have variations for signed and unsigned numbers, one of the operands is stored in memory, or the result can be stored in one of the general purpose registers.

Most of the information in this chapter is contained in the document8-bit AVR Instruction Setor itsequivalent HTML version.

The decision about what instructions will be possible to execute in a microprocessors is one of the most important and has a profound effect in the performance when executing programs. Furthermore, the set of instructions and the processor architecture are tightly coupled. For example, the operands for most of the instructions are stored in the register file, thus, it must allow for these operands to be accessed efficiently. The AVR architecture, for example, allows various combinations when accessing the operands in the stageRegister Operand Fetch.

The other criteria that influences the type of instructions supported by a microprocessor is the complexity of the implementation. Ultimately, the instructions need to be sequenced by a digital circuit. The higher the complexity of the tasks carried out by an instruction, the higher the complexity of the design. This complexity usually translates into a large number of gates that require a larger physical space (larger chip) and more power consumption.

Consider the following example. Should a processor include as part of its machine language an instruction that given a real number and the coefficients of a second order polynomial obtains its corresponding value? Suppose we call this instructionSDPE(second degree polynomial evaluation). One possible format of this instruction could be:

SDPE a, b, c, n, dest

The instruction would evaluate the polynomial using the three coefficients and the additional value, and store the result in locationdest. The evaluation would calculate the value with the formula:

LaTeX: dest\:\&=\&a\ast n\wedge2\:+b\ast n+c dest&=&a∗n∧2+b∗n+c

Although such instruction could be useful, a processor without such instruction could still calculate the same result executing a sequence of instructions to perform the required additions and multiplications. Thus the trade-off to explore when designing a microprocessor is between the complexity of the architecture, and the performance obtained by including certain operations in the ISA. A microprocessor with a reduced set of instructions may require more operations to perform sophisticated calculations, but they are executed faster due to its simplicity. Conversely, a microprocessor with a large number of instructions may require much shorter instruction sequences, but they may take longer to execute due to their complexity.

Over the years microprocessor manufacturers have explored this trade-off and created two categories of systems depending on the complexity of the machine languages.

7.1.1. Complex Instruction Set Computers (CISC)

The processors that provide a rich and complex set of instructions are calledComplex Instruction Set ComputersorCISC. These instructions typically use several operands and may require multiple memory accesses. There are various examples of CISC microprocessors, but perhaps the most popular architectures of this type are Intel’s x86, IA-32, and IA-64 (also known asItanium,x86_64orAMD64.) Thex86architecture was present in the personal computers in 1970s. The architecture was then evolved to the IA-32 (32 bit bus), and then the 64 bit version. These processors are present in numerous desktop computers, servers, and laptops. Other examples of CISC architectures are those in systems such as IBM’s System/360, DEC PDP-11, DEC VAX, or Motorola 68k.

The machine instructions in a CISC computer allow handling multiple operands, some or all of them in memory, and accessed with address manipulations that may require certain arithmetic operations. For example, the following instruction is part of the IA-32 architecture:

ADD $4, 14(%eax, %ebx, 8)

and adds the value 4 to the data in memory stored in the address obtained by adding the value 14, the content of register%eax, and the content of register%ebxmultiplied by 8. As you can see, the execution of this instruction requires more than one arithmetic operation. The addition denoted by the instruction code (ADD) can be performed once the operands are obtained, and additional additions, and even a multiplication is needed to obtain the second operand.

7.1.2. Reduced Instruction Set Computers (RISC)

RISC architectures appeared as an alternative to existing CISC microprocessors. The philosophy is the opposite, microprocessors have a very reduce set of instructions performing each avery simple operation. The idea is to translate this simplicity in the functionality into a faster execution, and a simpler structure in the microprocessor. A reduced execution time does not necessarily translate into a processor that is faster than a CISC architecture. A simple set of instructions means that complex operations will require sequences of instructions, some of them very large.

Some examples of current RISC architectures are:

  • MIPS (Microprocessor without Interlocked Pipeline Stages): used in some routers, Nintendo consoles, original PlayStation, PlayStation 2 and PSP.
  • ARM: present in numerous computers and personal devices such as digital cameras, mobile phones, iPod, etc.
  • SPARC (Scalable Processor ARChitecture): powered the systems sold by Sun Microsystems (now owned by Oracle).
  • PowerPC (Performance OPtimization with Enhanced RISC - Performance Computing): It was created by a consortium of Apple, IBM and Motorola in the early 1990s to be used in personal computers. Not widely used these days.
  • AVR architecture. This architecture is now included in multiple microcontrollers used in embedded systems. Some of the systems in the Arduino family use microprocessors with this architecture.

The comparison between RISC and CISC architectures cannot be done solely in terms of the number of instructions executed per unit of time. With that measure, RISC processors are clear winners. A more exact comparison is to execute the same high level task in two processors and measure the amount of time it takes to finish the task, instead of the number of instructions executed. Very likely, the CISC architecture will execute a smaller number of instructions, but each of them will take longer than the RISC architecture.

7.1.3. Fixed vs Variable Size Instruction Encoding

Another important feature of a microprocessor that can be used to divide them into two categories is the format in which the instructions are encoded. In processors that use afixed lengthformat, every instruction has exactly the same size. This feature has numerous consequences in the design stage. Typically, a small number of operands are allowed in every instruction. A large number of operands would make the format longer, and perhaps not fully used by all the instructions. The number of different instructions is also reduced as a larger number of instructions translates into a larger number of possible cases to encode, and eventually into a larger format. The main advantage of these instructions is in the decoding stage. That is, when the instruction has been loaded in the instruction register, the processor needs to identify which one it is and the type of operands that are required. A fixed format greatly simplifies this stage as the bits encoding the different elements of the instruction are located in the same positions.

The processors with avariable lengthformat, on the other hand, allow instructions to be encoded with different number of bits. Thus, an instruction may have an arbitrary number of operands as they will be encoded with additional bits. The main disadvantage of this approach is during the decoding stage, that is, when the processor needs to identify the type of instruction to execute and obtain all its operands. In these processors, the decoding stage may require loading additional bytes from memory while the instruction is being decoded.

The following video summarizes the differences between CISC and RISC architecture..