PostHeaderIcon Break IC PIC16F610 Binary: What a Thousand Words of Space Does to Code

A thousand words is not much. On the PIC16F610 — fourteen pins, 1024 by 14 bits of program flash, 64 bytes of RAM, two comparators, an internal oscillator selectable at four or eight megahertz — the program array is small enough that the compiler cannot be indifferent to size. Every choice it makes is a compromise, and each compromise leaves a shape in the binary that tells you something about the source that produced it.

This is a different reading problem from the one on a large part. On a part with eight thousand words, the image is long and the work is navigation. Here the image fits in a page of listings and the work is interpretation: why the same six instructions appear eleven times, why there is a table where a conditional would be more natural, why a subroutine that is obviously useful is called exactly once.

The constraints that shape the code

Constraint Value here Effect on the generated code
program memory 1024 words inline expansion is expensive, sharing is mandatory
RAM 64 bytes variables are overlaid by lifetime, not by name
stack 8 levels nesting depth is a budget, not a given
call model no parameter passing arguments travel in fixed registers
instruction set 35 instructions no multiply, no divide, no indexed addressing

Each of those has a visible consequence. The absence of a multiply instruction means a multiplication by a constant appears as a shift-and-add sequence, or as a lookup table if the range is small. The absence of indexed addressing means every array access is a computed jump through a table of return instructions. Sixty-four bytes of RAM means the compiler reused the same address for variables that never coexist, so the disassembly shows one location serving three purposes at different times.

Fingerprint one: the same block, many times

The clearest sign of a size-constrained build is repetition. A sequence that appears at five call sites was cheaper to duplicate than to call, because a call costs two words at the site plus the subroutine body plus the stack level. On this part, a three-instruction idiom is duplicated without hesitation.

The reading consequence is that an unfamiliar block appearing repeatedly is not necessarily a library function that was inlined. It may be the compiler’s own idiom for a common operation — a sixteen-bit compare, a signed shift, a byte swap. Recognising the idiom once and then matching it everywhere collapses a page of listings into a handful of concepts.

Fingerprint two: tables where branches belong

A conditional assignment over a small range becomes a table on this part. The pattern is a computed jump into a run of return-literal instructions, one per case, and the table is often placed immediately after the dispatch code so that the offset is a simple addition to the program counter.

Source construct Generated form How to recognise it
switch over 8 values add offset to PC, then N returns runs of retlw with consecutive values
array of constants same, with data in the literals values form a monotonic or lookup sequence
state machine table of goto targets two tables, one for state, one for action
string or message packed literals read in pairs high bytes that look like printable ASCII

The third row is the important one. On this part a state machine is often a table of targets indexed by state, which is more compact than a chain of comparisons and much easier to read once identified. The giveaway is a cluster of goto instructions whose destinations are all within a few hundred words of each other.

Fingerprint three: variables that share an address

Sixty-four bytes of RAM cannot hold every variable the source declared, so the compiler overlays them. Two variables that never coexist occupy the same address, and the disassembly shows one location being written with a threshold early in the program and a counter later.

This is not corruption and it is not a bug in the recovery; it is the normal output of a lifetime analysis. The way to read it is to follow the address through the program rather than the name: at each write, ask what the value is used for until the next write. The answer names the variable for that interval.

A caution follows. Automated tools that assign one name per address will produce a listing where a single symbol is used for three unrelated quantities. That listing is worse than no listing, because it looks authoritative.

Fingerprint four: the deliberate delay

Small parts have no timer to spare, so delays are loops. A nested loop with a literal count is a delay calibrated to the instruction rate; the count and the nesting depth give the duration.

These loops are the timing skeleton of the product. A delay of a few milliseconds before reading an input is a debounce. A delay of twenty milliseconds after driving an output is a relay settling time. A delay that is not a round number of milliseconds is usually a protocol bit time, and matching it against known bus rates identifies the protocol even when the pin is bit-banged rather than driven by a peripheral.

What the binary can and cannot tell you

Question Answerable from the image How
which compiler family usually yes idiom shapes, table layout, call convention
original function boundaries mostly call sites, stack depth, RAM liveness
variable names no only roles, inferred from use
exact source statements no equivalent structure, not the original text
hardware configuration yes port and comparator setup
product behaviour yes, in outline main loop, state tables, delays

The honest framing of a delivery is that it recovers structure and behaviour, not the original source. Claims beyond that are what turn a good technical result into a dispute.

Delivering something usable

The convention on this family is worth restating because the file is small enough that mistakes hide: 1024 words occupy 2048 bytes if each word carries two bytes, and a file half that length is using a packed convention. A file that loads without complaint and runs for six instructions has almost certainly been mis-converted.

Beyond the image, the useful additions are a memory map showing which RAM addresses are used and for what, an annotated listing with the recognised idioms marked, and a note on the delays and what they imply about the hardware.

Where a small part has an awkward three-page array instead of a round one, PIC16F527 heximal deals with the boundary problem that follows. Where the same small array belongs to a part whose serial peripheral has to be identified from the initialisation code, PIC16F689 software continues the reading. Where the space constraint is absent because the array is four times larger, PIC16F747 code shows what changes when the compiler stops worrying. And where a small part uses its last program word for calibration rather than code, PIC12F508 code recovery covers that boundary case.

The short version

On a thousand-word part, the code is shaped by scarcity. Repetition, tables, overlaid variables and hand-tuned delays are all consequences of the size, and each is a readable signal. Treat the fingerprints as information rather than noise and the listing becomes a design description.

Comments are closed.