Skip to content

Inline assembly #189

@nomota

Description

@nomota

There needs an explanation on Inline Assembly page

How to put C3 vars to/from inline assembly

// Gcc/Clang
int add(int a, int b) {
    int result;
    __asm__ (
        "addl %%ebx, %%eax;"  // The actual assembly instruction
        : "=a" (result)       // Output: 'a' refers to the EAX register
        : "a" (a), "b" (b)    // Inputs: 'a' into EAX, 'b' into EBX
    );
    return result;
}

int add_optimized(int a, int b) {
    int result;
    __asm__ (
        "addl %2, %0;" 
        : "=r" (result)  // %0: compiler chooses a register for output
        : "0" (a),       // %1: use the same register as %0 for input 'a'
          "r" (b)        // %2: compiler chooses a register for input 'b'
    );
    return result;
}
// Note: Only works on 32-bit MSVC
int add_msvc(int a, int b) {
    __asm {
        mov eax, a
        add eax, b
        // Result is left in eax, which is the standard return register
    }
}
int add_arm(int a, int b) {
    int result;
    __asm__ (
        "add %w0, %w1, %w2"  // Instruction: result = a + b
        : "=r" (result)      // %0: Output operand
        : "r" (a),           // %1: Input operand
          "r" (b)            // %2: Input operand
    );
    return result;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions