Skip to content
tgtakaokaPublic

About

C++ library of assembler / disassembler that can run on embedded system

Topics

Resources

Stars

44 stars

Watchers

4 watching

Forks

Repository files navigation

https://github.com/tgtakaoka/libasm/blob/main/LICENSE.md https://github.com/tgtakaoka/libasm/releases badge

libasm: cross assemble/disassemble library

The libasm allows assembling and disassembling supported retro CPUs.

Assembler library interface

Assembling MC68HC11 instruction in string to binary can be done in a minute. You can also add symbol table lookup via SymbolTable interface.

#include <asm_mc6800.h>
libasm::mc6800::AsmMc6800 asm6800;
asm6800.setCpu("68HC11");

const char *text = "SUBD $90";
libasm::Insn insn{0x1000};

asm6800.encode(text, insn);
assert(insn.getError() == OK);
assert(insn.address()  == 0x1000);
assert(insn.length()   == 2);    // direct addressing
assert(insn.bytes()[0] == 0x93); // SUBD
assert(insn.bytes()[1] == 0x90); // $90

Disassembler library interface

To disassemble MC68000 binaries, you need to wrap memory by ArrayMemory or implements your own DisMemory interface to feed binaries. You can also add symbol table lookup via SymbolTable interface.

#include <dis_mc68000.h>
libasm::mc68000::DisMc68000 dis68k;
dis68k.setOption("relative", "true");

const uint16_t words[] = {0047372, 0x1232};
const libasm::ArrayMemory memory{0x1000, words, sizeof(words)};
libasm::DisMemory dismem = memory.iterator();
libasm::Insn insn{dismem.address()};
char opr[80];

dis68k.decode(dismem, insn, opr, sizeof(opr));
assert(insn.getError() == OK);
assert(insn.address()  == 0x1000);
assert(std::string(insn.name()) == "jmp");
assert(std::string(opr) == "(*+$1234,pc)");

PlatformIO examples

There are interactive assembler and disassembler examples you can build and run on the host with PlatformIO at examples.

Assembler command line interface

On POSIX environment, assembler command line interface is provided. It can generate Intel HEX or Motorola S-Record output.

libasm assembler (version 2.0.6)
usage: asm [-o <output>] [-l <list>] <input>
  -C <CPU>          : target CPU
     MC6800 MB8861 MC6801 HD6301 MC68HC11 MC6805 MC146805 MC68HC05
     MC68HC08 MC68HCS08 MC6809 HD6309 MC68HC12 MC68HCS12 MC68HCS12X
     MC68HC16 MOS6502 R65C02 G65SC02 W65C02S W65C816S i8039 i8048 i80C39
     i80C48 MSM80C39 MSM80C48 i8051 i8080 i8085 V30EMU Z80 Z180 Z280 Z380
     Z8 Z86C Z88 TLCS90 TLCS900 TLCS900L TLCS900H TLCS900H2 INS8060
     INS8070 CP1600 CDP1802 CDP1804 CDP1804A SCN2650 F3850 IM6100 HD6120
     TMS7000 TMS370 TMS32010 TMS32015 TMS32020 TMS320C25 TMS320C26
     TMS320C20X TMS320C5X TMS320C30 TMS320C31 TMS320C32 AM29030 AM29040
     AM29050 AM29200 AM29240 AM29245 80960 i8086 i80186 i80286 i80386
     i80486 V30 V33 V35 i8096 i80196KB i80196 i80196N i80296 MC68000
     MC68010 MC68020 MC68030 MC68040 H16 H8/300 H8/300H H8S/2000 H8S/2600
     H8/510 H8/520 H8/530 80C166 80C167 XC16X SH-1 SH-2 SH-DSP SH-2E SH-2A
     TMS9900 TMS9980 TMS9995 TMS99105 TMS99110 Z8001 Z8002 NS32032 MN1610
     MN1613 MN1613A J11 T11
  -o <output>       : output file
  -l <list>         : list file
  -S[<bytes>]       : output Motorola S-Record format
  -H[<bytes>]       : output Intel HEX format
                    : optional <bytes> specifies data record length (max 32)
  -h                : use lower case letter for hexadecimal
  -n                : output line number to list file
  -v                : print progress verbosely
  --<name>=<vale>   : extra options (<type> [, <CPU>])
  --<name>          : --<name>=true
  --no-<name>       : --<name>=false
  --list-radix      : set listing radix (8, 16) (int)
  --smart-branch    : optimize branch instruction (bool)
  --advanced-mode   : H8/300H 24-bit advanced mode (bool: H8/300)
  --extmode         : Extended mode (bool: Z380)
  --fpu             : floating point co-processor (text: AM29200, 80960, 8086, 68000, SH-1, 32032)
  --fwait           : 8087 WAIT before instructions: none (default), auto or asm86 (text: 8086)
  --gnu-as          : GNU assembler compatible (bool: 8086)
  --implicit-word   : unknown instruction defines word (bool: 6100, J11)
  --intmult         : enable integer multiply instructions (bool: AM29200)
  --ip-operand      : IP-relative operand: offset (default) or target (text: 80960)
  --lwordmode       : Long word mode (bool: Z380)
  --maximum-mode    : enable maximum mode (bool: TLCS900, H8/510)
  --mmc             : enable logical page:address with memory map controller (bool: 68HC12)
  --mode-bits       : value of the mode bit of an unused operand field (int: 80960)
  --optimize-index  : optimize zero index (bool: Z8)
  --optimize-segment: optimize segment override (bool: 8086)
  --pc-bits         : program counter width in bit, default 13 (int: 6805)
  --pmmu            : memory management unit (text: 68000, 32032)
  --reg-alias       : emit register alias for the current setrp value (bool: Z8)
  --short-direct    : optimize direct addressing (bool: Z8001)
  --signed-prefix   : FE prefix for signed multiply and divide (bool: 8096)
  --silicon         : silicon revision (default C30 1, C31 5, C32 2) (int: 320C30)
  --use-register    : enable register name Rn (bool: 1802)
  --use16           : 16-bit program model (bool: 8086)
  --use32           : 32-bit program model (bool: 8086)
  --zilog-syntax    : Use Zilog syntax (bool: 8080)

Disassembler command line interface

On POSIX environment, disassembler command line interface is provided. It can read Intel HEX or Motorola S-Record input.

libasm disassembler (version 2.0.6)
usage: dis -C <CPU> [-o <output>] [-l <list>] <input>
  -C <CPU>         : target CPU
     MC6800 MB8861 MC6801 HD6301 MC68HC11 MC6805 MC146805 MC68HC05
     MC68HC08 MC68HCS08 MC6809 HD6309 MC68HC12 MC68HCS12 MC68HCS12X
     MC68HC16 MOS6502 R65C02 G65SC02 W65C02S W65C816S i8039 i8048 i80C39
     i80C48 MSM80C39 MSM80C48 i8051 i8080 i8085 V30EMU Z80 Z180 Z280 Z380
     Z8 Z86C Z88 TLCS90 TLCS900 TLCS900L TLCS900H TLCS900H2 INS8060
     INS8070 CP1600 CDP1802 CDP1804 CDP1804A SCN2650 F3850 IM6100 HD6120
     TMS7000 TMS370 TMS32010 TMS32015 TMS32020 TMS320C25 TMS320C26
     TMS320C20X TMS320C5X TMS320C30 TMS320C31 TMS320C32 AM29030 AM29040
     AM29050 AM29200 AM29240 AM29245 80960 i8086 i80186 i80286 i80386
     i80486 V30 V33 V35 i8096 i80196KB i80196 i80196N i80296 MC68000
     MC68010 MC68020 MC68030 MC68040 H16 H8/300 H8/300H H8S/2000 H8S/2600
     H8/510 H8/520 H8/530 80C166 80C167 XC16X SH-1 SH-2 SH-DSP SH-2E SH-2A
     TMS9900 TMS9980 TMS9995 TMS99105 TMS99110 Z8001 Z8002 NS32032 MN1610
     MN1613 MN1613A J11 T11
  -o <output>      : output file
  -l <list>        : list file
  <input>          : file can be Motorola S-Record or Intel HEX format
  -A start[,end]   : disassemble start address and optional end address
  -r               : use program counter relative notation
  -h               : use lower case letter for hexadecimal
  -u               : use upper case letter for output
  -v               : print progress verbosely
  --<name>=<vale>  : extra options (<type> [, <CPU>])
  --upper-hex      : use upper case letter for hexadecimal (bool)
  --upper-case     : use upper case letter for output (bool)
  --list-radix     : set listing radix (8, 16) (int)
  --relative       : program counter relative branch target (bool)
  --c-style        : C language style number constant (bool)
  --intel-style    : Intel style hexadecimal/octal/binary (bool)
  --motorola-style : Motorola style hexadecimal/octal/binary (bool)
  --origin-char    : letter for origin symbol (char)
  --gnu-as         : GNU assembler compatible (bool)
  --advanced-mode  : H8/300H 24-bit advanced mode (bool: H8/300)
  --cc-prefix      : use cc_ prefix for condition code (bool: 80C166)
  --extern-symbol  : emit absolute branch target as A_XXXXXXXX symbol (bool: AM29200)
  --extern-symbol  : emit direct address as seg_SS_off_XXXX symbol (bool: Z8001)
  --external-paren : disp2(disp(ext)) as external addressing (bool: 32032)
  --extmode        : Extended mode (bool: Z380)
  --far-insn       : use callf/jmpf for far call/jump (bool: 8086)
  --fdmode         : FP11 FD mode: single (default) or double (text: J11)
  --flmode         : FP11 FL mode: short (default) or long (text: J11)
  --fp-alias       : use FP for the frame pointer (bool: H8/510)
  --fpu            : floating point co-processor (text: AM29200, 80960, 8086, 68000, SH-1, 32032)
  --fwait          : 8087 WAIT before instructions: none (default), auto or asm86 (text: 8086)
  --index-sharp    : use # for [Rw+#data16] (bool: 80C166)
  --indirect-long  : [] for indirect long operand (bool: 6502)
  --intmult        : enable integer multiply instructions (bool: AM29200)
  --ip-operand     : IP-relative operand: offset (default) or target (text: 80960)
  --lock-insn      : lock prefix as instruction (bool: 8086)
  --longa          : enable 16-bit accumulator (bool: 6502)
  --longi          : enable 16-bit index registers (bool: 6502)
  --lwordmode      : Long word mode (bool: Z380)
  --maximum-mode   : enable maximum mode (bool: TLCS900, H8/510)
  --mmc            : enable logical page:address with memory map controller (bool: 68HC12)
  --mode-bits      : value of the mode bit of an unused operand field (int: 80960)
  --pc-alias       : use PC for the program counter (bool: CP1600)
  --pc-bits        : program counter width in bit (default 13) (int: 6805)
  --pmmu           : memory management unit (text: 68000, 32032)
  --ptr-suffix     : add ptr suffix for memory reference (bool: 8086)
  --repeat-insn    : repeat prefix as instruction (bool: 8086)
  --segment-insn   : segment override as instruction (bool: 8086)
  --segmented-addr : use <<segment>> notation (bool: Z8001)
  --sfr-name       : use name for core SFR (bool: 80C166)
  --short-direct   : use |addr| for short direct notation (bool: Z8001)
  --silicon        : silicon revision (default C30 1, C31 5, C32 2) (int: 320C30)
  --sp-alias       : use SP for the stack pointer (bool: CP1600, 68000, H16, H8/300, H8/510, Z8001)
  --use-absolute   : zero register indexing as absolute addressing (bool: 8096)
  --use-aux-name   : use aux register name ARn (bool: 32010)
  --use-port-name  : use port name PAn (bool: 32010)
  --use-register   : use register name Rn (bool: 1802)
  --use-sharp      : use # (default =) for immediate (bool: 8070)
  --use16          : 16-bit program model (bool: 8086)
  --use32          : 32-bit program model (bool: 8086)
  --work-register  : prefer work register name than alias address (bool: Z8)
  --zilog-syntax   : Use Zilog syntax (bool: 8080)

Supported host environment

  • PlatformIO (native)

  • Linux, macOS (C++14)

More information about this library can be found at GitHub

About

C++ library of assembler / disassembler that can run on embedded system

Topics

Resources

Stars

44 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages