PostHeaderIcon Break IC PIC16F873 Software: From Image to Something an Engineer Can Maintain

The request says software. Software implies source, and source does not exist in the device. What exists is a sequence of instruction words, and the distance between that and something an engineer can open, understand and modify is the actual work — considerably more than the extraction that precedes it.

The PIC16F873 is a twenty-eight-pin flash part with 4096 by 14 bits of program memory, 128 bytes of data EEPROM and a ten-bit converter. Four thousand words is the size at which a listing stops being readable as a whole and starts needing structure: sections, names, an index.

Why a disassembly is not source

Property Original source Disassembly
names chosen by the author addresses and register numbers
comments intent absent
structure functions, modules linear addresses with jumps
constants symbolic literal values
build reproducible not reproducible without a toolchain decision

The fifth row is the one people underestimate. A disassembly can be reassembled, but only if the toolchain assumptions are known: the instruction set variant, the addressing of the configuration word, whether the assembler expects the file in one section or several. Without those, the rebuilt binary differs from the original even when the code is “the same”, and every difference has to be explained.

The four passes

The work is best done in four passes, each producing an artefact that the next one depends on.

Pass one: boundaries. Divide the image into functions by finding the entry points. Calls give most of them directly. Interrupt handler, reset code, and any table-reached routine have to be added manually. The output is a list of addresses with a provisional name each.

Pass two: memory. Name every RAM location by the role it plays. This is the pass that makes the listing readable, because the difference between movwf 0x25 and movwf motor_timer is the difference between cryptography and code.

Pass three: constants. Every literal used in a comparison, a delay or a scaling is a constant with meaning. Tabulating them, converting them into physical units where the arithmetic allows, gives the product’s parameter set.

Pass four: control flow. Write the structure down in prose: what the main loop does, what interrupts occur and why, what the states are. That document is the thing a new engineer reads first.

Naming RAM without inventing it

The temptation is to give every location a confident name. The discipline is to give it a name that states what the evidence supports.

A location written once at start-up and read in a loop is a configuration value. A location incremented in the timer interrupt and compared against a constant in the main loop is a timeout counter. A location that holds a value copied from the EEPROM and used in a multiply is a calibration factor. A location whose writes and reads never overlap in time with another’s is probably the same storage reused for two purposes.

Each of those names describes a role, and none of them pretends to be the author’s identifier. A listing with timeout_counter is useful; a listing with T1 because it was the first temporary is not; and a listing that insists the location is called battery_voltage when the evidence only supports adc_result is worse than either.

Making the build reproduce

Three decisions have to be made and written down before the disassembly can be reassembled into an identical binary.

The section layout: which ranges are code, which are tables, and where the configuration word goes. On this family the config word is at a fixed address, and an assembler that puts it elsewhere produces a binary that differs in one word and behaves differently in every way that matters.

The word representation: whether the assembler takes fourteen-bit words or packed bytes. This is the same convention problem that affects the file format, and it reappears at the build stage.

The literal conventions: whether values are written in decimal, hex or binary, and whether the instruction mnemonic expects an absolute address or a page-relative one. Cosmetic in source, decisive in output.

Writing those three decisions into a short build note, alongside the source, is what makes the result reproducible by someone else.

What “maintainable” actually buys

Change the customer wants Effort with image only Effort with structure
alter a threshold locate a literal by comparison against a known value edit one named constant
change a timing find and patch a delay count edit a constant with a unit
add an output hand-assemble into free space, patch a jump add a routine, call it
change a display character patch a table entry edit a table
port to a different part impractical recompile against a new header

The last row is the strongest argument for the work. A customer who recovers a design because the part is obsolete usually ends up porting it, and a port from a hex file is a rewrite, while a port from structured source with named registers is a week of work.

Protection, and what changes

If the array is protected, everything above happens after a die-level recovery, and the analysis is the same. If it is not protected, the extraction is trivial and the analysis is the entire engagement — which is why quoting these two cases the same way is a mistake in both directions.

Where the same family’s forty-pin member has a parameter block that the structure has to identify, PIC16F874 eeprom covers that search. Where the protection matrix determines what is available in the first place, PIC16F876 flash sets it out. And where a baseline part’s structure is a state machine rather than a call graph, PIC16C554 software shows the different shape.

The short version

Software is a structure, not a file. Four passes — boundaries, memory, constants, control flow — produce something a person can maintain, and three build decisions make it reproducible. The extraction is the easy part; say so in the quote.

Comments are closed.