PostHeaderIcon MSP430G2121: An Instruction Set That Happens to Be Easy to Read

Reverse engineering effort depends less on the amount of code than on the architecture it was compiled for. The same thousand instructions take an afternoon on one core and a week on another, and the difference is entirely in how much work the reader has to do to establish what each instruction means and where it ends.

This core sits at the friendly end of that spectrum. The reasons are specific, and each one removes a class of difficulty that appears on other architectures.

Fixed length, almost

Almost every instruction is one 16-bit word. The exceptions are instructions with an immediate operand, which occupy two words, and that exception is encoded in the opcode rather than discovered by trying to decode.

The consequence is that linear disassembly works. Starting at an address and stepping forward word by word produces correct instruction boundaries with no heuristics, because there is no ambiguous encoding to resolve. On architectures with variable-length instruction sets, a single misinterpreted byte can shift the alignment and produce plausible-looking garbage for the rest of the region.

Property Effect on the reverse engineer
Fixed instruction length Linear sweep gives correct boundaries
Immediate operands marked in the opcode No guesswork about where the next instruction starts
No branch delay slots Instruction order equals execution order
Orthogonal addressing Each operand can be read independently

Seven addressing modes, applied uniformly

Every operand can be register, indexed, symbolic, absolute, indirect, auto-increment or immediate. The same seven apply to source and destination, in every instruction, with no special cases.

That orthogonality is the reason a single mental model covers the whole instruction set. Once the seven modes are known, the meaning of any instruction follows from its operands without consulting a table — which is exactly the property that makes reading large regions practical.

Two of the modes deserve a note because they are distinctive. The symbolic mode computes an address relative to the program counter, which is how most compiler-generated data references appear. The absolute mode is the same mechanism with the register forced, which is how peripheral registers appear.

That second distinction is a gift: a peripheral access is visually identifiable in the listing, because it uses absolute addressing while data accesses usually use symbolic or indexed addressing.

The constant generator

A quirk of the design worth understanding early: the four registers at the top of the register file are not general purpose in the usual sense. Two of them are special, and the other two form a mechanism that produces six constants without an operand word.

The result is that constants zero, one, two, four, eight and minus one appear as compact operand encodings rather than as data in the instruction stream. That reduces the amount of literal data sitting between instructions, which reduces the chance of a disassembler mistaking data for code.

The cost is that these instructions look strange on first reading — an operation apparently on a register that is not a register. Once recognised, they are faster to read than a literal load.

The limited jump range

One property works against the reader. The conditional jump instructions carry a ten-bit signed displacement, which reaches a few hundred words in either direction.

When a compiler needs to branch further, it inserts a two-step construct: a conditional jump to a nearby unconditional branch, or an inverted conditional jump around an absolute branch. Those constructs appear in optimised code as pairs, and they used to be a common source of misreading, because the first instruction appears to branch to a strange destination.

Recognising the pattern — a conditional jump whose target is a single unconditional branch — closes the confusion. It also explains why a disassembly sometimes shows a short jump to a jump.

Twenty-seven instructions

The instruction set is small enough to hold in mind completely, which is not true of most architectures. That has a practical consequence: after a few hours with one image, reading becomes recognition rather than decoding.

The complementary cost is that operations other architectures make cheap become sequences. Multiplication and division are not instructions; they are routines. A 32-bit add is a sequence of adds with carry. Those sequences are recognisable, and once recognised they read as the operations they implement.

What this means for the work

Three practical consequences follow, and they are the reason analysis on this family can be quoted with confidence.

Function boundaries are recoverable without a tool. Call and return are explicit, so a linear sweep plus a list of call targets produces a function inventory.

Compiler behaviour is legible. The startup code and the library routines are short enough to read completely, which means the toolchain can be identified from the image rather than guessed at.

Behaviour maps to a small instruction vocabulary. When the device does something specific — a delay, a measurement, a bit toggle — the corresponding instructions are findable by their constants, and the constants are the specification.

Related reading

FAQ

Is a disassembler still needed? For volume, yes. For understanding, a listing and patience are enough.

Why do some instructions have two words? Because an immediate operand has to be carried in the instruction stream.

What is the constant generator for? It encodes six common constants without an extra word.

Does the short jump range cause misreads? It causes the branch-to-branch pattern, which is recognisable once known.

Conclusion

This architecture is easy to read for structural reasons: fixed lengths, orthogonal addressing, no delay slots, a small vocabulary and constants that do not litter the code. Those properties make the analysis steps elsewhere in this series practical rather than aspirational.

Comments are closed.