Last active
January 5, 2018 04:50
-
-
Save commiebstrd/c2ec00f7ef65bd2bdfa7635e9fae89a6 to your computer and use it in GitHub Desktop.
execute multiple inline assembly instructions with a single asm!{} invocation!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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