Файл: Introduction to microcontrollers (G. Gridling, 2006).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1053
Скачиваний: 2
18 |
CHAPTER 2. MICROCONTROLLER COMPONENTS |
cumulator which is always used as one of the operands and as the destination register. The second operand is specified explicitly.
2-address Format Architecture: Here, both operands are specified, but one of them is also used as the destination to store the result. Which register is used for this purpose depends on the processor in question, e.g., the ATmega16 controller uses the first register as implicit destination, whereas the 68000 processor uses the second register.
3-address Format Architecture: In this architecture, both source operands and the destination are explicitly specified. This architecture is the most flexible, but of course it also has the longest instruction size.
Table 2.1 shows the differences between the architectures when computing (A+B)*C. We assume that in the cases of the 2- and 3-address format, the result is stored in the first register. We also assume that the 2- and 3-address format architectures are load/store architectures, where arithmetic instructions only operate on registers. The last line in the table indicates where the result is stored.
stack |
accumulator |
2-address format |
3-address format |
PUSH A |
LOAD A |
LOAD R1, A |
LOAD R1, A |
PUSH B |
ADD B |
LOAD R2, B |
LOAD R2, B |
ADD |
MUL C |
ADD R1, R2 |
ADD R1, R1, R2 |
PUSH C |
LOAD R2, C |
LOAD R2, C |
|
MUL |
MUL R1, R2 |
MUL R1, R1, R2 |
|
stack |
accumulator |
R1 |
R1 |
Table 2.1: Comparison between architectures.
Execution Speed
The execution speed of an instruction depends on several factors. It is mostly influenced by the complexity of the architecture, so you can generally expect a CISC machine to require more cycles to execute an instruction than a RISC machine. It also depends on the word size of the machine, since a machine that can fetch a 32 bit instruction in one go is faster than an 8-bit machine that takes 4 cycles to fetch such a long instruction. Finally, the oscillator frequency defines the absolute speed of the execution, since a CPU that can be operated at 20 MHz can afford to take twice as many cycles and will still be faster than a CPU with a maximum operating frequency of 8 MHz.
Available Instructions
Of course, the nature of available instructions is an important criterion for selecting a controller. Instructions are typically parted into several classes:
Arithmetic-Logic Instructions: This class contains all operations which compute something, e.g., ADD, SUB, MUL, . . . , and logic operations like AND, OR, XOR, . . . . It may also contain bit operations like BSET (set a bit), BCLR (clear a bit), and BTST (test whether a bit is set). Bit operations are an important feature of the microcontroller, since it allows to access single bits without changing the other bits in the byte. As we will see in Section 2.3, this is a very useful feature to have.
2.1. PROCESSOR CORE |
19 |
Shift operations, which move the contents of a register one bit to the left or to the right, are typically provided both as logical and as arithmetical operations. The difference lies in their treatment of the most significant bit when shifting to the right (which corresponds to a division by 2). Seen arithmetically, the msb is the sign bit and should be kept when shifting to the right. So if the msb is set, then an arithmetic right-shift will keep the msb set. Seen logically, however, the msb is like any other bit, so here a right-shift will clear the msb. Note that there is no need to keep the msb when shifting to the left (which corresponds to a multiplication by 2). Here, a simple logical shift will keep the msb set anyway as long as there is no overflow. If an overflow occurs, then by not keeping the msb we simply allow the result to wrap, and the status register will indicate that the result has overflowed. Hence, an arithmetic shift to the left is the same as a logical shift.
Example: Arithmetic shift
To illustrate what happens in an arithmetic shift to the left, consider a 4-bit machine. Negative numbers are represented in two’s complement, so e.g. -7 is represented as binary 1001. If we simply shift to the left, we obtain 0010 = 2, which is the same as -14 modulo 16. If we had kept the msb, the result would have been 1010 = -6, which is simply wrong.
Shifting to the right can be interpreted as a division by two. If we arithmetically right-shift -4 = 1100, we obtain 1110 = -2 since the msb remains set. In a logical shift to the right, the result would have been 0110 = 6.
Data Transfer: These operations transfer data between two registers, between registers and memory, or between memory locations. They contain the normal memory access instructions like LD (load) and ST (store), but also the stack access operations PUSH and POP.
Program Flow: Here you will find all instructions which influence the program flow. These include jump instructions which set the program counter to a new address, conditional branches like BNE (branch if the result of the prior instruction was not zero), subroutine calls, and calls that return from subroutines like RET or RETI (return from interrupt service routine).
Control Instructions: This class contains all instructions which influence the operation of the controller. The simplest such instruction is NOP, which tells the CPU to do nothing. All other special instructions, like power-management, reset, debug mode control, . . . also fall into this class.
Addressing Modes
When using an arithmetic instruction, the application programmer must be able to specify the instruction’s explicit operands. Operands may be constants, the contents of registers, or the contents of memory locations. Hence, the processor has to provide means to specify the type of the operand. While every processor allows you to specify the above-mentioned types, access to memory locations can be done in many different ways depending on what is required. So the number and types of addressing modes provided is another important characteristic of any processor. There are numerous addressing modes2, but we will restrict ourselves to the most common ones.
2Unfortunately, there is no consensus about the names of the addressing modes. We follow [HP90, p. 98] in our nomenclature, but you may also find other names for these addressing modes in the literature.
20 |
CHAPTER 2. MICROCONTROLLER COMPONENTS |
immediate/literal: Here, the operand is a constant. From the application programmer’s point of view, processors may either provide a distinct instruction for constants (like the LDI —load immediate— instruction of the ATmega16), or require the programmer to flag constants in the assembler code with some prefix like #.
register: Here, the operand is the register that contains the value or that should be used to store the result.
direct/absolute: The operand is a memory location.
register indirect: Here, a register is specified, but it only contains the memory address of the actual source or destination. The actual access is to this memory location.
autoincrement: This is a variant of indirect addressing where the contents of the specified register is incremented either before (pre-increment) or after (post-increment) the access to the memory location. The post-increment variant is very useful for iterating through an array, since you can store the base address of the array as an index into the array and then simply access each element in one instruction, while the index gets incremented automatically.
autodecrement: This is the counter-part to the autoincrement mode, the register value gets decremented. Again nice to have when iterating through arrays.
displacement/based: In this mode, the programmer specifies a constant and a register. The contents of the register is added to the constant to get the final memory location. This can again be used for arrays if the constant is interpreted as the base address and the register as the index within the array.
indexed: Here, two registers are specified, and their contents are added to form the memory address. The mode is similar to the displacement mode and can again be used for arrays by storing the base address in one register and the index in the other. Some controllers use a special register as the index register. In this case, it does not have to be specified explicitly.
memory indirect: The programmer again specifies a register, but the corresponding memory location is interpreted as a pointer, i.e., it contains the final memory location. This mode is useful e.g. for jump tables.
Table 2.2 shows the addressing modes in action. In the table, M[x] is an access to the memory address x, d is the data size, and #n indicates a constant. The notation is taken from [HP90] and varies from controller to controller.
As we have already mentioned, CISC processors feature more addressing modes than RISC processors, so RISC processors must construct more complex addressing modes with several instructions. Hence, if you often need a complex addressing mode, a CISC machine providing this mode may be the wiser choice.
Before we close this section, we would like to introduce you to a few terms you will often encounter:
•An instruction set is called orthogonal if you can use every instruction with every addressing mode.
•If it is only possible to address memory with special memory access instructions (LOAD, STORE), and all other instructions like arithmetic instructions only operate on registers, the architecture is called a load/store architecture.
•If all registers have the same function (apart from a couple of system registers like the PC or the SP), then these registers are called general-purpose registers.
2.1. PROCESSOR CORE |
21 |
|||||
addressing mode |
example |
result |
||||
immediate |
ADD R1, #5 |
R1 |
← R1 + 5 |
|||
register |
ADD R1, R2 |
R1 |
← R1 + R2 |
|||
direct |
ADD R1, 100 |
R1 |
← R1 + M[100] |
|||
register indirect |
ADD R1, (R2) |
R1 |
← R1 + M[R2] |
|||
post-increment |
ADD R1, (R2)+ |
R1 |
← R1 + M[R2] |
|||
ADD R1, −(R2) |
R2 |
← R2 + d |
||||
pre-decrement |
R2 |
← R2 − d |
||||
R1 |
← R1 + M[R2] |
|||||
displacement |
ADD R1, 100(R2) |
R1 |
← R1 |
+ M[100 + R2] |
||
indexed |
ADD R1, (R2+R3) |
R1 |
← R1 |
+ M[R2+R3] |
||
memory indirect |
ADD R1, @(R2) |
R1 |
← R1 |
+ M[M[R2]] |
||
Table 2.2: Comparison of addressing modes.
2.1.3Exercises
Exercise 2.1.1 What are the advantages of the Harvard architecture in relation to the von Neumann architecture? If you equip a von Neumann machine with a dual-ported RAM (that is a RAM which allows two concurrent accesses), does this make it a Harvard machine, or is there still something missing?
Exercise 2.1.2 Why was RISC developed? Why can it be faster to do something with several instructions instead of just one?
Exercise 2.1.3 What are the advantages of general-purpose registers as opposed to dedicated registers? What are their disadvantages?
Exercise 2.1.4 In Section 2.1.2, we compared different address formats. In our example, the accumulator architecture requires the least instructions to execute the task. Does this mean that accumulator architectures are particularly code-efficient?
Exercise 2.1.5 What are the advantages and drawbacks of a load/store architecture?
Exercise 2.1.6 Assume that you want to access an array consisting of 10 words (a word has 16 bit) starting at memory address 100. Write an assembler program that iterates through the array (pseudocode). Compare the addressing modes register indirect, displacement, auto-increment, and indexed.
Exercise 2.1.7 Why do negative numbers in an arithmetic shift left (ASL) stay negative as long as there is no overflow, even though the sign bit is not treated any special? Can you prove that the sign bit remains set in an ASL as long as there is no overflow? Is it always true that even with an overflow the result will remain correct (modulo the range)?