{"record":{"id":"8bb710ce2a399520","repo":"NationalSecurityAgency/ghidra","slug":"could-not-assemble-s","errorCode":null,"errorMessage":"Could not assemble: %s","messagePattern":"Could not assemble: (.+?)","errorType":"exception","errorClass":"AssemblyException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AbstractPatchAssemblyCommand.java","lineNumber":74,"sourceCode":"\n\tpublic AddressSetView assemble(T program, TaskMonitor monitor)\n\t\t\tthrows CancelledException, IOException, MemoryAccessException, AssemblyException {\n\t\tmonitor.setMessage(\"Constructing Assembler\");\n\t\tmonitor.checkCancelled();\n\t\tAssemblyBuffer buf = new AssemblyBuffer(asm, entry, initialContext);\n\n\t\tmonitor.initialize(lines.size(), \"Assembling\");\n\t\tfor (String line : lines) {\n\t\t\tif (line.isBlank()) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// LATER: Data directives? Label placement?\n\t\t\ttry {\n\t\t\t\tbuf.assemble(line);\n\t\t\t\tmonitor.increment();\n\t\t\t}\n\t\t\tcatch (AssemblySyntaxException | AssemblySemanticException e) {\n\t\t\t\tthrow new AssemblyException(\"Could not assemble: %s\".formatted(line), e);\n\t\t\t}\n\t\t}\n\n\t\tmonitor.initialize(0, \"Placing bytes\");\n\t\tmonitor.checkCancelled();\n\t\tAddressSet set = new AddressSet(entry, buf.getNext().previous());\n\t\t// Get the command before we modify the memory/listing, so it can inspect things\n\t\tCommand<T> dis = newDisassembleCommand(set, program);\n\n\t\tprogram.getListing()\n\t\t\t\t.clearCodeUnits(set.getMinAddress(), set.getMaxAddress(), true, monitor);\n\t\tprogram.getMemory().setBytes(entry, buf.getBytes());\n\n\t\tmonitor.setMessage(\"Disassembling\");\n\t\tmonitor.checkCancelled();\n\n\t\t// Might not succeed...\n\t\tdis.applyTo(program);","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AbstractPatchAssemblyCommand.java#L56-L92","documentation":"Thrown by AbstractPatchAssemblyCommand.assemble() when AssemblyBuffer.assemble(line) fails for one instruction line. The original AssemblySyntaxException (malformed/unknown mnemonic or operand) or AssemblySemanticException (valid syntax but impossible encoding, e.g. bad register or out-of-range immediate) is wrapped in an AssemblyException whose message is 'Could not assemble: <line>'. Note: only raw instruction text is supported here; the source comment marks data directives and label placement as unimplemented.","triggerScenarios":"Calling the patch assembler (single-line or multi-line) where at least one line is: an unknown/unsupported mnemonic for the program's processor/Sleigh language, has the wrong number of operands, references a register that does not exist for the language, uses an immediate outside the encodable range, uses the wrong assembly syntax for the architecture, or contains a label/data directive the assembler does not understand.","commonSituations":"Typo in an instruction; pasting x86 syntax into an ARM (or vice versa) program; using AT&T vs Intel form when the language expects the other; an instruction the Sleigh assembler does not encode for that language variant; encoding-dependent operand (e.g. conditional suffix, addressing mode) not supported; whitespace/blank handling confusion (note: truly blank lines are skipped).","solutions":["Read the offending line from the message after 'Could not assemble: ' and correct the typo / operand count.","Confirm the instruction is valid for the program's processor language and that the assembler was built for that language (Assemblers.getAssemblerForLanguage(program.getLanguage())).","Switch the operand/register to a form the language encodes (e.g. different immediate width, valid register name, supported addressing mode); consult the Sleigh / processor reference.","Split multi-instruction input into one line per instruction and assemble line-by-line to isolate which line fails, since the exception reports only the first failing line.","If no line assembles at all, the language may lack an assembler spec; verify the language ships assembly templates before attempting patch assembly."],"exampleFix":"// before - typo + wrong-syntax instruction for the target language\nString bad = \"mvo rax, 0x1\";  // 'mvo' is not a valid mnemonic\n\n// after - correct mnemonic and operands for x86:LE:64:default\nString good = \"mov rax, 0x1\";","handlingStrategy":"try-catch","validationCode":"// Pre-assemble each line into a throwaway AssemblyBuffer before\n// committing patches to the real program, to localize the failure.\nAssemblyBuffer probe = new AssemblyBuffer(asm, entry, initialContext);\njava.util.List<String> problems = new java.util.ArrayList<>();\nfor (String line : lines) {\n    if (line.isBlank()) continue;\n    try {\n        probe.assemble(line);\n    } catch (AssemblySyntaxException | AssemblySemanticException e) {\n        problems.add(line + \"  <- \" + e.getMessage());\n    }\n}","typeGuard":null,"tryCatchPattern":"// applyTo() already catches AssemblyException and stores its message in\n// status (command returns false). For direct assemble() use, catch\n// AssemblyException and read the offending line from the message.\ntry {\n    cmd.assemble(program, monitor);\n} catch (AssemblyException e) {\n    String msg = e.getMessage();           // \"Could not assemble: <line>\"\n    String line = msg.substring(msg.indexOf(':') + 1).trim();\n    Throwable cause = e.getCause();        // AssemblySyntax|SemanticException\n    // surface line + cause to the user; do NOT silently retry unchanged input\n}","preventionTips":["Build the Assembler for the exact program language: Assemblers.getAssemblerForLanguage(program.getLanguage()).","Feed one instruction per line; the patch assembler does not support labels or data directives yet.","When pasting assembly, confirm the dialect (Intel/AT&T, suffix conventions) matches what the Sleigh assembler accepts.","Assemble line-by-line in a probe buffer first so the user sees every failing line, not just the first."],"tags":["ghidra","assembler","assembly","patch","sleigh","syntax","semantic"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}