2022-01-31
1. Register review
The word “register” is an overloaded term (think of operator overloading in C++, Python, or other languages).
From the bottom-up perspective[1], any register is a group of N-DFFs that are treated as an N-bit entity.
1.1. The cast of characters
On the AVR architecture, there are “registers” of several slightly different flavors:
-
PC Program Counter. It’s own separate set of DFFs that is accessible only by a few instructions. Increments by +1 otherwise. Not typically called a “register” in a sentence, but truly is one. All CPUs have a Program Counter because it keeps track of the next instruction to execute.
-
R0 .. R31 General Purpose Working Registers. These are in the Register File, a term that simply means they are a special block of logic+FFs where several can be read and updated in a single clock cycle. Think of these like variables.
-
On AVR, the upper six can be paired up and operate as 16-bit entities. When used as such, they get new (human) names:
-
X (R27 : R26) (MSB : LSB)
-
Y (R29 : R28)
-
Z (R31 : R30)
-
Several instructions use X / Y / Z, so the 8-bit registers R26 : R31 are rarely used for general 8-bit variables.
-
-
-
SREG (or SR in other architectures). Special bits which:
-
Hold status and control bits that are critical to the behavior of the CPU (bit
I
, Global Interrupt Enable)> -
Get updated with the result of an ALU operation. (
C
,Z
,N
,V
,H
). -
Handy utility functionality (
T
andN ⨁ V = S
). -
On AVR, this register is in the I/O Memory address space.
-
-
I/O Registers. These are the locations whose bits configure various peripherals or extra features of the MCU. Timer, ADC, or GPIO configuration registers, etc.
-
SP (SPH:SPL) Stack Pointer. Holds the data memory (SRAM) address of the “top of (the) stack” (TOS).
-
Like the SREG, the SPH and SPL registers are in the I/O Memory address space.
-
Special instructions put the PC value onto the stack (
RCALL
) or take the value on the top of the stack and updates the PC (RET
orRETI
). -
Another pair of instructions puts a register’s value onto the stack (
PUSH
) or takes the value on the top of the stack and places into a register (POP
).
-
2. IF (branch) instructions
When you come to a fork in the road, take it.
You may be suprised to find out that there are only two branch instructions that can jump to an arbitrary place:
BRBC s, k
|
Jump to (PC + 1 + k) if bit s in the Status Register (SREG) is clear (zero), otherwise continue to the next instruction. |
BRBS s, k
|
Jump to (PC + 1 + k) if bit s in the Status Register (SREG) is set (one), otherwise continue to the next instruction. |
Since there are 8 bits in SREG and two clear/set possibilities for each:
How many possible BR__
instructions are there (not counting BRBC
and BRBS
) in the ATtiny85?
2.1. Q: Why instruction aliases?
Why does the Assembly Instruction Set list all of these extra branch instructions when they are simply BRB{C,S}
machine codes with specific values of s.
2.2. Q: Skip instructions vs. branch?
Study the following pairs of instructions in the AVR-InstructionSet-Manual-DS40002198_2021.pdf:
-
SBI{C,S}
-
SBR{C,S}
How would you program this behavior without these instructions?
Why are there two pairs instead of just one pair?
Describe a practical situation where you would use one of these instructions.
3. Bit instructions
Similar to the Status Register branching instructions BRB{C,S}
, the 16-member family of SREG bit setting and bit clearing instructions are covered by the pair
-
BSET s
andBCLR s
Having assembly mnemonics that directly describe the intended operation make it easier for the programmer to describe the intent of the code in a more human-friendly way.
For example, consider SEI
versus BSET 7
to set the Global Interrupt Enable bit.
These sorts of features have more advantages that just the obvious ones of less code and faster execution. These special instructions allow atomic operations that can’t be messed up.
To make this real, this report details using electro-magnetic field injection to cause bad behavior in a microcontroller. It demonstrates that a Toyota ECU (engine control unit, the engine’s main computer) can sometimes command wide-open throttle under certain conditions. Background: Wikipedia: Sudden unintended acceleration - Sudden acceleration in Toyota vehicles. Pay attention to the links in paragraph 5 of this section. The Toyota case had many problems, but the technical cause was probably traceable to glitches that O’Flynn demonstrates. Other earlier engineering failure analysis reports:
|