Skip to Content
GuidesAnalysis and Reverse EngineeringExporting a disassembly as source

Exporting a Disassembly as Source

Once a program is annotated, you can turn it into source code: Klive Z80 assembly with your labels, comments and data regions, that reassembles to exactly the bytes it came from. This is for programs you are reverse engineering; to write your own compiled program to a tape, snapshot or NEX file, see Exporting your program.

Click the Export as source button (the file icon) in the header of the Disassembly view or of a bank document, or choose Debug › Export Disassembly as Source…. The Disassembly view and the menu export the RAM of the 64K view ($4000–$FFFF) under the current paging; a bank document exports its bank. The tools button’s Export as Source… does the same, and Export as Source on a named graphic exports the graphic. To export a whole ROM, use Export as source… in the ROM annotation editor.

Where the files go

The save dialog opens in the export folder:

  • in a Klive project, SourceExports in the project folder, so the exported file opens in the IDE;
  • without a project, KliveExports/source in your home folder, beside the screen recordings.

When you save somewhere else, Klive remembers that folder for the next export: in the project’s settings while a Klive project is open (relative to the project when the folder is inside it), otherwise in your user settings. It is the export.sourceFolder setting, so you can also set it with set: set -p export.sourceFolder asm for one project, set -u export.sourceFolder ~/asm for every export outside a project. A project uses only its own value, so a folder chosen outside a project never takes over a project’s exports.

From the command line

The command takes any range of the 64K view or a whole bank:

export-asm game.kz80.asm $8000 $9fff -open export-asm bank3.kz80.asm -bank 3

Proven, not hoped for

After writing the file, Klive always assembles it and compares the result with the bytes:

Verified: the export assembles to the same bytes.

If the file does not assemble, the first error is shown with its line; if it assembles to different bytes, the first differing addresses are listed. An export started from a button or the menu shows the verdict in a message box, as an error when the check fails; the command fails then too, so a script can tell.

The export takes care of the cases where a disassembly’s text would not assemble back to the same bytes:

  • Non-canonical encodings (the NEG and RETN mirrors, the alternative IM encodings, the long LD (nn),HL, a DD/FD prefix that has no effect, undefined ED opcodes) are written as .defb with the instruction in the comment.
  • An instruction that runs past its region is written as the bytes the region holds.
  • Copper and DMA programs, and the ROM’s inline RST $08 / RST $28 bytes, are .defb with their decoding as the comment.
  • A label the assembler would not accept (hl, ld, a name that differs from another only in case) is renamed, and the output says how.
  • A name used in an operand but defined outside the range (a ROM routine, a label in another bank) becomes NAME .equ $NNNN.
  • A skip region is exported as its bytes; -skip gap leaves it out and starts a new .org instead, and says the result is shorter.

A 128K bank is assembled into its bank with .model Spectrum128 and .bank; a Next export uses .model next for the Next’s own instructions.

The export is always written in Klive’s own assembly dialect.

Last updated on