### _arm_multiply def _arm_multiply(op: int, name: str) -> tuple[str, tuple[str, ...], str]: cond = CONDITIONS[op >> 28] s = "S" if op & (1 << 20) else "" rd, rn = (op >> 16) & 0xF, (op >> 12) & 0xF rs, rm = REGISTERS[(op >> 8) & 0xF], REGISTERS[op & 0xF] if name in ("MUL", "MLA"): # bits 23:22 are 00 for MUL and MLA (DS 4.7); the table also sends 01 here, as NBA # decodes it, and MUL's Rn field should be zero standard = not op & (1 << 22) and (name == "MLA" or rn == 0) operands = (REGISTERS[rd], rm, rs) + ((REGISTERS[rn],) if name == "MLA" else ()) return f"{name}{cond}{s}", operands, "" if standard else NONSTANDARD return f"{name}{cond}{s}", (REGISTERS[rn], REGISTERS[rd], rm, rs), "" # RdLo, RdHi ### _arm_swp def _arm_swp(op: int) -> tuple[str, tuple[str, ...], str]: b = "B" if op & (1 << 22) else "" operands = ( REGISTERS[(op >> 12) & 0xF], REGISTERS[op & 0xF], f"[{REGISTERS[(op >> 16) & 0xF]}]", ) # SWP is bits 27:20 = 00010B00 with bits 11:8 zero (DS 4.12); the table also sends the # other bit-21/20/23 patterns here, as NBA decodes them. standard = (op >> 8) & 0xF == 0 and op & 0x00B00000 == 0 comment = "" if standard else NONSTANDARD return f"SWP{CONDITIONS[op >> 28]}{b}", operands, comment ### _arm_halfword def _arm_halfword(op: int, name: str, address: int) -> tuple[str, tuple[str, ...], str]: """LDRH/STRH/LDRSB/LDRSH, and ARMv5's LDRD/STRD encodings (DS 4.10, U21).""" pre, up, write_back = bool(op & (1 << 24)), bool(op & (1 << 23)), bool(op & (1 << 21)) rn, rd = (op >> 16) & 0xF, (op >> 12) & 0xF comment = "" if op & (1 << 22): imm = ((op >> 4) & 0xF0) | (op & 0xF) offset = None if imm == 0 and up else _signed_imm(imm, up) if rn == 15 and pre: comment = f"=[{_address(address + 8 + (imm if up else -imm))}]" else: offset = ("" if up else "-") + REGISTERS[op & 0xF] if (op >> 8) & 0xF: comment = NONSTANDARD if not pre and write_back: comment = _join(comment, "post-indexed with W set: unpredictable (DS 4.10)") if name in ("LDRD", "STRD"): comment = _join(comment, "ARMv5 encoding: no transfer on the ARM7TDMI (U21)") mnemonic = f"{name[:3]}{CONDITIONS[op >> 28]}{_HALFWORD_SUFFIX[name]}" return mnemonic, (REGISTERS[rd], _indexed(rn, offset, pre, write_back and pre)), comment ### disassemble_arm def disassemble_arm(word: int, address: int) -> Instruction: """The ARM instruction ``word`` at ``address``.""" op = word & MASK32 address &= MASK32 handler = ARM_HANDLERS[((op >> 16) & 0xFF0) | ((op >> 4) & 0xF)] cond = CONDITIONS[op >> 28] target = None if handler in _DATA_HANDLERS: mnemonic, operands, comment = _arm_data(op, _DATA_HANDLERS[handler], address) elif handler == "_arm_mrs": mnemonic, operands, comment = _arm_mrs(op) elif handler == "_arm_msr": mnemonic, operands, comment = _arm_msr(op) elif handler == "_arm_bx": mnemonic, operands, comment = _arm_bx(op) elif handler in _MULTIPLY: mnemonic, operands, comment = _arm_multiply(op, _MULTIPLY[handler]) elif handler == "_arm_swp": mnemonic, operands, comment = _arm_swp(op) elif handler in _SINGLE: mnemonic, operands, comment = _arm_single(op, _SINGLE[handler], address) elif handler in _HALFWORD: mnemonic, operands, comment = _arm_halfword(op, _HALFWORD[handler], address) elif handler in ("_arm_ldm", "_arm_stm"): mnemonic, operands, comment = _arm_block(op, handler[5:].upper()) elif handler in ("_arm_b", "_arm_bl"): target = (address + 8 + (_sign_extend(op & 0xFFFFFF, 24) << 2)) & MASK32 mnemonic, operands, comment = f"{handler[5:].upper()}{cond}", (_address(target),), "" elif handler == "_arm_swi": mnemonic, operands, comment = f"SWI{cond}", (_hex(op & 0xFFFFFF),), "" elif handler in _COPROCESSOR: mnemonic, operands, comment = _arm_coprocessor(op, _COPROCESSOR[handler]) elif handler == "_arm_undefined": mnemonic, operands, comment = f"UND{cond}", (), "undefined instruction: trap to 0x04" else: # pragma: no cover - the tables name no other handler raise AssertionError(f"no disassembly for {handler}") if op >> 28 == 0xF: comment = _join(comment, "reserved condition NV: never executes (U19)") return Instruction( address, op.to_bytes(4, "little"), mnemonic, operands, comment, False, target ) ### _thumb_hi def _thumb_hi(op: int, name: str) -> tuple[str, tuple[str, ...], str]: """Format 5: ADD/CMP/MOV with a high register, and BX (DS 5.5).""" h1, h2 = bool(op & 0x80), bool(op & 0x40) rs = REGISTERS[(op >> 3) & 0xF] if name == "bx": comment = "" if op & 7 == 0 else NONSTANDARD if h1: comment = _join(comment, "H1 = 1 (ARMv5 BLX): a plain BX on the ARM7TDMI (U19)") return "BX", (rs,), comment rd = REGISTERS[(op & 7) | (h1 << 3)] comment = ( "" if h1 or h2 else "H1 = H2 = 0: 'undefined' in DS 5.5; runs on the low registers (U19)" ) return name.upper(), (rd, rs), comment ### _thumb_undefined def _thumb_undefined(op: int) -> tuple[str, tuple[str, ...], str]: trap = "undefined: trap to 0x04" if op & 0xFF00 == 0xDE00: return "UND", (_imm(op & 0xFF),), f"{trap}, provisional; NBA branches (C7)" if op & 0xF800 == 0xE800: return "UND", (_imm(op & 0x7FF),), f"{trap}, provisional; ARMv5 BLX suffix (C8)" if op & 0xFF00 == 0xBE00: return "UND", (_imm(op & 0xFF),), f"{trap}; ARMv5 BKPT" return "UND", (), trap