Skip to content

Writing a Printer

Antonio Davide edited this page Feb 20, 2019 · 6 revisions

The Printer object is tied to a specific AssemblerPlugin and provides fine graded customization of a disassembler listing.
A custom Printer should stay in the same folder of their AssemblerPlugin.

Creation

A custom printer can be created by inheriting from Printer and by overriding one or more functions, in this case we want a customized register (as $1, $2, $3, ...) and immediate visualization (as #immediate).

// customasm_printer.h

#include "../../plugins/assembler/printer.h"

class CustomPrinter: public Printer
{
  public:
    CustomPrinter(DisassemblerAPI* disassembler);
    virtual std::string reg(const RegisterOperand& regop) const; // For custom registers
    virtual std::string imm(const Operand* op) const;            // For custom immediates
};
// customasm_printer.cpp

#include "customasm_printer.h"

CustomPrinter::CustomPrinter(DisassemblerAPI* disassembler): Printer(disassembler)
{

}

std::string CustomPrinter::reg(const RegisterOperand& regop) const
{
    return "$" + std::to_string(regop.r);
}

std::string CustomPrinter::imm(const Operand* op) const
{
    return "#" + Printer::imm(op);
}

AssemblerPlugin relationship

The Printer is not a plugin and it can't be registered, instead, we must override override the AssemblerPlugin method called createPrinter() and return an instance of our custom printer: REDasm will take the ownership of the object.

// customasm.h

#include "../../plugins/plugins.h"

class CustomAssembler: public AssemblerPlugin
{
  public:
    ...
    virtual Printer* createPrinter(DisassemblerAPI *disassembler);
    ...
};
// customasm.cpp

#include "customasm.h"
#include "customasm_printer.h"

Printer* CustomAssembler::createPrinter(DisassemblerAPI *disassembler)
{
    return new CustomPrinter(disassembler);
}

Backend

Like AssemblerPlugin, REDasm provides a Capstone customized version of the Printer called CapstonePrinter which, by default, overrides reg() function in order to display register names.

You can’t perform that action at this time.