08.01.2013 Views

Introduction to Microcontrollers

Introduction to Microcontrollers

Introduction to Microcontrollers

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.1. PROCESSOR CORE 17<br />

Example: Some opcodes of the ATmega16<br />

The ATmega16 is an 8-bit harvard RISC controller with a fixed opcode size of 16 or in some<br />

cases 32 bits. The controller has 32 general purpose registers. Here are some of its instructions<br />

with their corresponding opcodes.<br />

instruction result operand conditions opcode<br />

ADD Rd, Rr Rd + Rd ← Rr 0 ≤ d ≤ 31, 0000 11rd dddd rrrr<br />

0 ≤ r ≤ 31<br />

AND Rd, Rr Rd ← Rd & Rr 0 ≤ d ≤ 31, 0010 00rd dddd rrrr<br />

0 ≤ r ≤ 31<br />

NOP 0000 0000 0000 0000<br />

LDI Rd, K Rd ← K 16 ≤ d ≤ 31, 1110 KKKK dddd KKKK<br />

0 ≤ K ≤ 255<br />

LDS Rd, k Rd ← [k] 0 ≤ d ≤ 31, 1001 000d dddd 0000<br />

0 ≤ k ≤ 65535 kkkk kkkk kkkk kkkk<br />

Note that the LDI instruction, which loads a register with a constant, only operates on the upper<br />

16 out of the whole 32 registers. This is necessary because there is no room in the 16 bit <strong>to</strong><br />

s<strong>to</strong>re the 5th bit required <strong>to</strong> address the lower 16 registers as well, and extending the operation<br />

<strong>to</strong> 32 bits just <strong>to</strong> accommodate one more bit would be an exorbitant waste of resources.<br />

The last instruction, LDS, which loads data from the data memory, actually requires 32 bits<br />

<strong>to</strong> accommodate the memory address, so the controller has <strong>to</strong> perform two program memory<br />

accesses <strong>to</strong> load the whole instruction.<br />

what you need. For instance, the 10 lines of ATmega16 RISC code require 20 byte of code (each<br />

instruction is encoded in 16 bits), whereas the 68030 instruction fits in<strong>to</strong> 4 bytes. So here, the 68030<br />

clearly wins. If, however, you only need instructions already provided by an architecture with short<br />

opcodes, it will most likely beat a machine with longer opcodes. We say “most likely” here, because<br />

CISC machines with long opcodes tend <strong>to</strong> make up for this deficit with variable size instructions. The<br />

idea here is that although a complex operation with many operands may require 32 bits <strong>to</strong> encode,<br />

a simple NOP (no operation) without any arguments could fit in<strong>to</strong> 8 bits. As long as the first byte<br />

of an instructions makes it clear whether further bytes should be decoded or not, there is no reason<br />

not <strong>to</strong> allow simple instructions <strong>to</strong> take up only one byte. Of course, this technique makes instruction<br />

fetching and decoding more complicated, but it still beats the overhead of a large fixed-size opcode.<br />

RISC machines, on the other hand, tend <strong>to</strong> feature short but fixed-size opcodes <strong>to</strong> simplify instruction<br />

decoding.<br />

Obviously, a lot of space in the opcode is taken up by the operands. So one way of reducing the<br />

instruction size is <strong>to</strong> cut back on the number of operands that are explicitly encoded in the opcode.<br />

In consequence, we can distinguish four different architectures, depending on how many explicit<br />

operands a binary operation like ADD requires:<br />

Stack Architecture: This architecture, also called 0-address format architecture, does not have any<br />

explicit operands. Instead, the operands are organized as a stack: An instruction like ADD takes<br />

the <strong>to</strong>p-most two values from the stack, adds them, and puts the result on the stack.<br />

Accumula<strong>to</strong>r Architecture: This architecture, also called 1-address format architecture, has an ac-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!