Attack Microchip Source Code: 35 Instructions and No Stack Frames
Judgements about how hard a device is to reverse are usually made from the instruction set. By that measure, Microchip’s mid-range 8-bit cores look trivial: around three dozen instructions, a single working register, an accumulator-style architecture, and addressing that fits on one page of documentation. Someone who has reversed an ARM Cortex-M image expects this to be a weekend.
It is not, and the reason has nothing to do with the instruction count. It is that the architecture provides no stack frame. There is a hardware call stack that stores return addresses and nothing else. Local variables live at fixed addresses in a data bank, allocated by the linker at build time, not pushed at run time. So a function’s entry point is not marked by a prologue that adjusts a frame pointer, and its exit is not marked by a matching epilogue. The markers that a decompiler relies on elsewhere simply do not exist.
Why the missing prologue breaks the usual pipeline
On an architecture with frames, a function typically begins with a recognisable sequence: subtract from the stack pointer, save callee-saved registers, set up a frame pointer. That sequence is a signature, and function identification is largely signature matching. Remove the signature and identification becomes a graph problem instead.
| Architecture feature | Consequence for reconstruction |
|---|---|
| No frame pointer | No prologue signature; entry points must be inferred |
| Locals at fixed addresses | No way to bound a function’s data by its frame |
| Hardware return stack only | Return addresses are not in memory, so no unwind |
| Single accumulator | Data flow is highly sequential; expressions are long chains |
| Banked data memory | Every data access carries bank selection state |
The third row is the one that hurts. On architectures that push return addresses onto the data stack, a return address on the stack is evidence of a call site, and scanning for those addresses reconstructs the call graph mechanically. Here the return address is in a small hardware stack that is not memory-mapped, so there is nothing to scan. The call graph has to be built from the call instructions themselves, which means finding those instructions first.
That difference also changes what a deliverable can promise. On an architecture with frames, a tool can enumerate functions exhaustively and the analyst reviews a list. Here the list is the output of an inference, and its completeness is a judgement. Breaking an MCU sets out why that distinction matters when the deliverable is a maintainable project rather than a file.
Building the call graph from call instructions
The workable approach is iterative and starts from anchors that are known. The reset vector is known. The interrupt vector is known. Every call instruction found in a linear sweep nominates a target. Each nominated target is then validated by checking that its first instructions are plausible as an entry: not in the middle of a multi-word instruction, not immediately following an unconditional branch that would make it unreachable, and consistent with the alignment the architecture uses.
Validation is where the iteration happens. A call whose target lands in the middle of a table is not a call; it is a computed access that the linear sweep misdecoded because it walked into data. Removing that nomination changes the boundary of the region containing it, which changes what is code and what is data, which changes subsequent nominations.
The retlw table trap
Mid-range PIC has a idiom that defeats every naive disassembler: the computed jump through a table of retlw instructions. The table is entered by adding an offset to the program counter, and each entry is a return-with-literal instruction. Executed in sequence from the top it would return immediately; entered at an offset it returns the selected constant.
| What it looks like linearly | What it actually is |
|---|---|
| A run of return instructions with literals | A constant lookup table |
| A branch into the middle of that run | The table’s dispatch instruction |
| Code after the run that is never reached linearly | The real continuation |
A disassembler that treats the run as code will produce a function that returns a constant and has a hundred unreachable instructions after it. Recognising the pattern requires knowing that a branch target inside a return run is legal and meaningful here.
Bank state is program state
Data memory is banked, and the bank select bits are part of a status register that is set by ordinary instructions. This means the meaning of a data access depends on the execution path that led to it. Two identical instructions at two addresses can touch different physical locations. Reconstructing variables therefore requires tracking bank state along every path, which is a data-flow problem rather than a decoding problem.
The practical consequence is that automatic decompilation of these parts is unreliable in a way that is visible: the same variable appears as several different variables, or two different variables merge. A reconstruction that does not track bank state will produce code that compiles and does not work.
What the small instruction set does buy
Once boundaries are established, the decoding is genuinely easy, and the reconstruction is unusually faithful. There are few instruction forms, so the decompiler has little choice to make, and the generated C is close to what a human would write for the same operations. Bit manipulation is explicit, hardware access is explicit, and there is no scheduling to undo because the architecture does not reorder.
| Property | Effect on reconstruction quality |
|---|---|
| Few instruction forms | High fidelity, low ambiguity |
| Explicit bit operations | Register-level intent survives |
| No pipeline reordering | Source order largely preserved |
| No register renaming | Variables stay in place |
This is the trade the architecture makes: it is easy to read and hard to bound. Reading is the cheap part, and the cost sits in the boundary work.
Where this connects to the rest
The image itself has to come from somewhere, and on these parts the code protect bit usually decides that the route is physical; attacking Microchip MCU firmware covers that interaction in detail. The generic problem of turning a disassembly into something maintainable is treated in cracking microcontroller source code, and the four losses described there apply here with a twist: inlining is rare on this architecture, so the reconstruction is closer to source than on a 32-bit part.
Services at this boundary
Work covers image acquisition, boundary recovery using the iterative call-graph method, bank-state-aware variable reconstruction, table and jump-table identification, and delivery of compilable C at an agreed level. It does not cover recovering original identifiers or comments, and it does not cover promising a byte-identical rebuild.
The honest expectation to set is that the instruction set is the easy half. The schedule is dominated by boundary work and bank tracking, and those are the two things to ask about when comparing quotations.