Skip to content

xoofx/AsmArm64

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

182 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AsmArm64 ci NuGet

AsmArm64 is a powerful ARM64 Assembler and Disassembler .NET library.

✨ Features

  • Full support of 2448+ ARM64 v8.x/v9.x instructions
    • Automatically generated from ARM XML specification files from latest update 2024-12.
    • Note that SVE/SVE2/SME are not yet supported.
  • Unique strongly typed assembler API
  • Easily disassemble instructions and operands, including the knowledge of which operands and status flags are being read/write.
  • High performance / zero allocation library for disassembling / assembling instructions.
  • 15,000+ unit tests battle testing this library
  • Compatible with net8.0+ and NativeAOT.

πŸ’» Example

Strongly Typed Assembler API

using var asm = new Arm64Assembler(0x1_0000);

// // Main entry point
// _start:
asm.Label("_start", out var labelStart);
//     mov     x0, #5         // Call sum_loop(5)
//     bl      sum_loop       // Call the function, result in x0
//     ret                    // Return from _start (normally would return to a caller)
var labelSumLoop = asm.CreateLabel("sum_loop");
asm.MOVZ(X0, 5)
   .BL(labelSumLoop)
   .RET();
// 
// 
// // Function: sum_loop(x0)
// // - Takes x0 as the loop limit
// // - Returns the sum in x0
// sum_loop:
asm.Label(labelSumLoop);
//     mov     x1, #0         // Accumulator (sum)
//     mov     x2, #0         // Counter
asm.MOVZ(X1, 0)
   .MOVZ(X2, 0);
// 
// loop_start:
asm.Label("loop_start", out var labelLoopStart);
//     add     x1, x1, x2     // Add counter (x2) to sum (x1)
//     add     x2, x2, #1     // Increment counter
//     cmp     x2, x0         // Compare counter with limit
//     blt     loop_start     // If counter < limit, continue loop
//     mov     x0, x1         // Store result in x0 (return value)
//     ret                    // Return to caller
asm.ADD(X1, X1, X2)
   .ADD(X2, X2, 1)
   .CMP(X2, X0)
   .B(LT, labelLoopStart)
   .MOV(X0, X1)
   .RET()
   .End();
// Bytes available in asm.Buffer or asm.ToArray()

Generated instruction methods return the assembler, so calls can be chained. Statement-style calls still work when the returned assembler is ignored.

Labels can be used in simple address expressions for label operands, e.g. asm.B(loop + 4) or asm.ADR(X0, dataLabel + 16). Arm64Address.Page/label.Page() and PageOffset() help with ADRP + page-offset address materialization. Subtracting labels (labelB - labelA) evaluates to a signed expression once both labels are bound.

When generated overloads can do so without creating ambiguous optional-operand signatures, they also record caller file/line information in asm.DebugMap. BeginFunction, EndFunction, BeginCodeSection, EndCodeSection, BeginDataSection, and EndDataSection add named markers to the same map for tooling and annotated disassembly. Finalization failures such as unbound labels or out-of-range labels throw Arm64AssemblerException with structured Arm64AssemblerDiagnostic entries; TryEnd(out var diagnostics) is available for non-throwing tooling workflows.

For larger layouts, Arm64Block/ArrangeBlocks can place fixed and aligned floating blocks, Literal/FlushLiteralPool emits explicit literal pools for LDR literal, and ExternalLabel records unresolved symbol relocations in asm.Relocations.

Disassembler API

ReadOnlySpan<byte> instructionBuffer = asm.Buffer;
var disassembler = new Arm64Disassembler();
var textWriter = new StringWriter();
disassembler.Disassemble(instructionBuffer, textWriter);
Console.WriteLine(textWriter);

// For quick roundtrips, use:
string text = asm.Disassemble();
// or:
string text2 = Arm64Disassembler.DisassembleToString(instructionBuffer, asm.BaseAddress);

will print:

LL_01:
    mov x0, #5
    bl LL_02

    ret

LL_02:
    mov x1, #0
    mov x2, #0

LL_03:
    add x1, x1, x2
    add x2, x2, #1
    cmp x2, x0
    b.lt LL_03

    mov x0, x1
    ret

Arm64DisassemblerOptions.TryFormatLabel can be used to resolve absolute addresses to label or symbol names, for example from PDB data. Arm64DisassemblerOptions.AutoLabelKinds controls which PC-relative operands get generated local labels (branches, calls, ADR/ADRP, or literal loads). Formatting options include style presets, comment prefix, address prefix, generated local-label format, and hex casing. Arm64DisassemblerOptions.InvalidDataMode controls trailing bytes that are not a complete 4-byte instruction: throw, emit a .byte directive, or ignore them.

πŸ“– User Guide

For more details on how to use AsmArm64, please visit the user guide.

πŸͺͺ License

This software is released under the BSD-2-Clause license.

πŸ€— Author

Alexandre Mutel aka xoofx.

About

The most powerful ARM 64 (v8, v9) Assembler / Disassembler for .NET

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors

Languages