Inline Assembly

Frost provides a powerful inline assembly interface that allows direct hardware access while maintaining safety and clarity.

Basic Syntax

The inline assembly block uses a structured format:

__asm__ {
    code -> "assembly instructions"
    constraints -> "constraint string"
    args -> (variables)
    parameters -> parameter types
    return_type -> return type
    side_effects -> boolean
    align_stack -> boolean
    dialect -> assembly dialect (ATT or Intel)
}

Assembly Block Components

Code Section

Contains the actual assembly instructions using placeholders:

Constraints

Specifies how variables map to registers or memory:

Common Constraints

  • r: Register operand

  • m: Memory operand

  • i: Immediate integer

  • =: Write-only operand

  • +: Read-write operand

Arguments

Variables passed to assembly:

Type Specifications

Safety and Optimization

Side Effects Declaration

Stack Alignment

Assembly Dialects

Best Practices

  1. Minimal Usage

Prefer high-level Frost when possible. Use assembly only for:

  • Hardware access

  • Performance-critical sections

  • Platform-specific instructions

  1. Documentation

Document inline assembly blocks for clarity. Add a header with function name and purpose:

  1. Error Handling

Always verify assembly execution and handle errors:

Remember that inline assembly should be used judiciously, only when necessary for performance optimization or hardware access. Frost's type system ensures that inline assembly blocks are safe and properly integrated with the rest of your code.

Last updated