Skip to content

Inline asm: add output constraint, add volatile #15

Closed
@bombela

Description

@bombela

It is not permitted for the asm to write to any input register or memory location (unless that input is tied to an output). This is because the compiler will assume that you haven't mutated the input. And can therefore reuse the register with its value.

Additionally, you must add the option volatile. Otherwise the compiler is free to remove the piece of inline asm since you do not care about the output value!

Here is an example that I believe to be correct:

fn busy_loop_4_cycles(count: u16) {
    let mut _count = count; // Quiet the linter with underscore.
    unsafe {
        llvm_asm!(r#"
            1: sbiw $0,1
            brne 1b
            "#
            :"=w" (_count) // output
            :"0" (_count) // input
            : // clobbers
            : "volatile" // options
        )
    }
}

links:

: "w" (0)

https://llvm.org/docs/LangRef.html#input-constraints

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