mozilla/rust — pull request #5359 by luqmana (2013-03-13 20:29:37+00:00)
Title: Inline assembly, part 2

Actually use operands properly now.

Which means you can do things like call printf:
```Rust
fn main() {
unsafe {
do str::as_c_str(~"The answer is %d.\n") |c| {
let a = 42;
asm!("mov $0, %rdi\n\t\
mov $1, %rsi\n\t\
xorl %eax, %eax\n\t\
call _printf"
:
: "r"(c), "r"(a)
: "rdi", "rsi", "eax"
: "volatile","alignstack"
);
}
}
}
```

```
% rustc foo.rs
% ./foo
The answer is 42.
```

Or just add 2 numbers:
```Rust
fn add(a: int, b: int) -> int {
let mut c = 0;
unsafe {
asm!("mov $1, %rax\n\t\
mov $2, %rbx\n\t\
add %rbx, %rax\n\t\
mov %rax, $0"
: "=r"(c)
: "r"(a), "r"(b)
: "rax", "rbx"
);
}
c
}

fn main() {
io::println(fmt!("%d", add(1, 2)));
}
```

```
% rustc foo.rs
% ./foo
3
```

This also classifies inline asm as RvalueStmtExpr instead of the somewhat arbitrary kind I made it initially. There are a foo XXX's regarding what to do in the liveness and move passes.