extract-asm
npx machina-cli add skill nmoinvaz/speedy-gonzales/extract-asm --openclawExtract Assembly for a Function
Extract the assembly of a specific function from an object file or assembly file.
Prerequisites
The input must be a compiled object file (.o or .obj) or assembly file (.s or .asm). If you have source code, compile it first (e.g. via CMake build or standalone clang -S).
Usage
Invoke with: the input file and function name.
Arguments:
$1= input file (.o,.obj,.s, or.asm)$2= function name
Examples:
build/CMakeFiles/zlib-ng.dir/trees.c.o compress_block/tmp/output.s my_functionbuild/CMakeFiles/zlib-ng.dir/Release/trees.obj compress_block
Steps
-
Obtain assembly from the input:
If
.oobject file (Mach-O / ELF):- Disassemble with:
objdump -d --no-show-raw-insn <file>.o
If
.objobject file (PE/COFF — Windows):dumpbinrequires the MSVC environment. Find the VS install path with vswhere, then run via cmd.exe:VSPATH=$(vswhere -latest -property installationPath) cmd.exe /c "call \"${VSPATH}\VC\Auxiliary\Build\vcvarsall.bat\" amd64 >nul 2>&1 && dumpbin /disasm <file>.obj"
If
.sor.asmassembly file:- Use directly.
- Disassemble with:
-
Extract the named function:
Mach-O: Find
_<function_name>:, collect until next^_[a-zA-Z]label (excluding_Ltmp,_LCFI,_LBBprefixes).ELF: Find
<function_name>:in objdump output, collect until the next function header.PE/COFF (dumpbin): Find
<function_name>:label, collect until the next label or summary line.Then for all formats:
- Filter out assembler directives (
.prefix), comments (#,;,//,@), and blank lines - Keep only instruction lines
- Filter out assembler directives (
-
Output the extracted assembly to stdout. Print a summary line with the instruction count.
Architecture Notes
x86-64
leaqinstructions with(%rip)are PC-relative address computations, not memory loads.- Memory operands use
offset(%reg)syntax (e.g.168(%rdi)loads from rdi+168).
AArch64
- Memory operands use
[reg, #offset]syntax (e.g.[x0, #168]loads from x0+168). - Load instructions:
ldr,ldur,ldp,ldrb,ldrh,ldrsw. Store:str,stur,stp,strb,strh. - On Apple Silicon this compiles natively — no cross-compilation needed.
x86-64 (Windows/MSVC)
- Uses Intel syntax by default (destination first):
mov rax, [rcx+168]. - Calling convention: first four args in
rcx,rdx,r8,r9(notrdi,rsilike SysV). - Function names may be decorated (e.g.
?name@@...for C++). C functions are undecorated.
General
- Object files from CMake builds are typically at:
- Unix:
build/CMakeFiles/<target>.dir/<source>.o - Windows (Ninja):
build/CMakeFiles/<target>.dir/<source>.obj - Windows (VS generator):
build/CMakeFiles/<target>.dir/<config>/<source>.obj
- Unix:
Source
git clone https://github.com/nmoinvaz/speedy-gonzales/blob/main/skills/extract-asm/SKILL.mdView on GitHub Overview
Extract the assembly of a named function from an object file (.o/.obj) or an assembly source (.s/.asm). This is useful for debugging, performance analysis, and verification of compiler optimizations by isolating the exact machine code of a function. The tool handles Mach-O, ELF, and PE/COFF formats and outputs clean instruction lines to stdout.
How This Skill Works
It takes a file and a function name as input. For .o/.obj files, it disassembles with objdump or dumpbin; for .s/.asm it uses the file directly. It then finds the function label, filters out directives and comments, and prints only instruction lines, followed by a summary with the instruction count.
When to Use It
- You have a compiled library or binary and need to inspect the exact assembly of a specific function
- You want to compare how a function compiles across builds or compiler flags
- You're debugging a crash and need to see the precise instructions around a function
- You're performing low level performance analysis or micro-architectural tuning
- You have an assembly file and want to isolate a function for focused review
Quick Start
- Step 1: Ensure you have a compiled object or an assembly file and know the function name
- Step 2: Run the extractor with the file and function name, e.g. extract-asm <file> <function-name>
- Step 3: Read the printed assembly for the function and the final instruction count
Best Practices
- Ensure the input is actually a compiled object file or an assembly source (.o, .obj, .s, .asm)
- If you have source code, compile it first to produce a valid object or assembly file
- Use the exact function name; be aware of case sensitivity and possible name decoration
- Filter out noninstruction lines and verify the final count matches expectations
- On Windows, consider decorated names and use dumpbin when targeting PE/COFF
Example Use Cases
- Extract compress_block from build/CMakeFiles/zlib-ng.dir/trees.c.o to study its assembly
- Extract a customFunction from /tmp/output.s to review the disassembly directly
- Extract compress_block from build/CMakeFiles/zlib-ng.dir/Release/trees.obj for cross build comparison
- Isolate a function in an ELF object to verify the prologue and calling convention in Linux
- Inspect a Windows PE/COFF function using dumpbin to analyze MSVC generated code