Skip to content

Instantly share code, notes, and snippets.

@commiebstrd
Last active January 5, 2018 04:50
Show Gist options
  • Save commiebstrd/c2ec00f7ef65bd2bdfa7635e9fae89a6 to your computer and use it in GitHub Desktop.
Save commiebstrd/c2ec00f7ef65bd2bdfa7635e9fae89a6 to your computer and use it in GitHub Desktop.
execute multiple inline assembly instructions with a single asm!{} invocation!
#![feature(asm)]
fn calc() -> usize {
let mut a: usize = 0;
unsafe {
asm!(/* Instructions: for(i=10; 0<i; i--){ a+=i } */
"mov rcx, 10\n\t" /* rcx = 10 - i - counter */
"xor rax, rax\n\t" /* rax = 0 - a - storage */
"loop:\n\t" /* label */
"add rax, rcx\n\t" /* rax+=rcx */
"dec rcx\n\t" /* rcx-- */
"jnz loop" /* rcx!=0 -> loop */
: "={rax}"(a) /* output rax->a */
: /* no input */
: "rax", "rcx" /* clobbers rax & rcx */
: "intel" /* att is bad mmmk */
);
}
a
}
fn main() {
println!{"result is: {}", calc()};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment