2020-05-10 09:54:30 -05:00
|
|
|
// Compiler:
|
|
|
|
//
|
|
|
|
// Run-time:
|
|
|
|
// status: 0
|
|
|
|
|
2021-12-30 09:28:53 -06:00
|
|
|
#![feature(asm_const, asm_sym)]
|
|
|
|
|
2021-12-30 08:36:30 -06:00
|
|
|
use std::arch::{asm, global_asm};
|
|
|
|
|
2020-05-10 09:54:30 -05:00
|
|
|
global_asm!("
|
|
|
|
.global add_asm
|
|
|
|
add_asm:
|
|
|
|
mov rax, rdi
|
|
|
|
add rax, rsi
|
|
|
|
ret"
|
|
|
|
);
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn add_asm(a: i64, b: i64) -> i64;
|
|
|
|
}
|
|
|
|
|
2021-12-15 21:06:16 -06:00
|
|
|
pub unsafe fn mem_cpy(dst: *mut u8, src: *const u8, len: usize) {
|
|
|
|
asm!(
|
|
|
|
"rep movsb",
|
|
|
|
inout("rdi") dst => _,
|
|
|
|
inout("rsi") src => _,
|
|
|
|
inout("rcx") len => _,
|
|
|
|
options(preserves_flags, nostack)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-10 09:54:30 -05:00
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
asm!("nop");
|
|
|
|
}
|
|
|
|
|
|
|
|
let x: u64;
|
|
|
|
unsafe {
|
|
|
|
asm!("mov $5, {}",
|
|
|
|
out(reg) x,
|
|
|
|
options(att_syntax)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 5);
|
|
|
|
|
|
|
|
let x: u64;
|
|
|
|
let input: u64 = 42;
|
|
|
|
unsafe {
|
|
|
|
asm!("mov {input}, {output}",
|
|
|
|
"add $1, {output}",
|
|
|
|
input = in(reg) input,
|
|
|
|
output = out(reg) x,
|
|
|
|
options(att_syntax)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 43);
|
|
|
|
|
|
|
|
let x: u64;
|
|
|
|
unsafe {
|
|
|
|
asm!("mov {}, 6",
|
|
|
|
out(reg) x,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 6);
|
|
|
|
|
|
|
|
let x: u64;
|
|
|
|
let input: u64 = 42;
|
|
|
|
unsafe {
|
|
|
|
asm!("mov {output}, {input}",
|
|
|
|
"add {output}, 1",
|
|
|
|
input = in(reg) input,
|
|
|
|
output = out(reg) x,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 43);
|
|
|
|
|
2021-12-15 21:06:16 -06:00
|
|
|
// check inout(reg_class) x
|
2021-09-05 10:26:01 -05:00
|
|
|
let mut x: u64 = 42;
|
|
|
|
unsafe {
|
|
|
|
asm!("add {0}, {0}",
|
2021-12-15 21:06:16 -06:00
|
|
|
inout(reg) x
|
2021-09-05 10:26:01 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 84);
|
|
|
|
|
|
|
|
// check inout("reg") x
|
|
|
|
let mut x: u64 = 42;
|
|
|
|
unsafe {
|
|
|
|
asm!("add r11, r11",
|
2021-12-15 21:06:16 -06:00
|
|
|
inout("r11") x
|
2021-09-05 10:26:01 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 84);
|
|
|
|
|
|
|
|
// check a mix of
|
|
|
|
// in("reg")
|
|
|
|
// inout(class) x => y
|
|
|
|
// inout (class) x
|
|
|
|
let x: u64 = 702;
|
|
|
|
let y: u64 = 100;
|
|
|
|
let res: u64;
|
|
|
|
let mut rem: u64 = 0;
|
|
|
|
unsafe {
|
|
|
|
asm!("div r11",
|
|
|
|
in("r11") y,
|
|
|
|
inout("eax") x => res,
|
|
|
|
inout("edx") rem,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(res, 7);
|
|
|
|
assert_eq!(rem, 2);
|
|
|
|
|
2021-12-15 21:06:16 -06:00
|
|
|
// check const
|
2021-09-05 10:26:01 -05:00
|
|
|
let mut x: u64 = 42;
|
|
|
|
unsafe {
|
|
|
|
asm!("add {}, {}",
|
|
|
|
inout(reg) x,
|
2021-12-15 21:06:16 -06:00
|
|
|
const 1
|
2021-09-05 10:26:01 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 43);
|
|
|
|
|
|
|
|
// check const (ATT syntax)
|
|
|
|
let mut x: u64 = 42;
|
|
|
|
unsafe {
|
|
|
|
asm!("add {}, {}",
|
|
|
|
const 1,
|
|
|
|
inout(reg) x,
|
|
|
|
options(att_syntax)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 43);
|
|
|
|
|
|
|
|
// check sym fn
|
|
|
|
extern "C" fn foo() -> u64 { 42 }
|
|
|
|
let x: u64;
|
|
|
|
unsafe {
|
|
|
|
asm!("call {}", sym foo, lateout("rax") x);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 42);
|
|
|
|
|
|
|
|
// check sym fn (ATT syntax)
|
|
|
|
let x: u64;
|
|
|
|
unsafe {
|
|
|
|
asm!("call {}", sym foo, lateout("rax") x, options(att_syntax));
|
|
|
|
}
|
|
|
|
assert_eq!(x, 42);
|
|
|
|
|
|
|
|
// check sym static
|
|
|
|
static FOO: u64 = 42;
|
|
|
|
let x: u64;
|
|
|
|
unsafe {
|
|
|
|
asm!("mov {1}, qword ptr [rip + {0}]", sym FOO, lateout(reg) x);
|
|
|
|
}
|
|
|
|
assert_eq!(x, 42);
|
|
|
|
|
|
|
|
// check sym static (ATT syntax)
|
|
|
|
let x: u64;
|
|
|
|
unsafe {
|
|
|
|
asm!("movq {0}(%rip), {1}", sym FOO, lateout(reg) x, options(att_syntax));
|
|
|
|
}
|
|
|
|
assert_eq!(x, 42);
|
|
|
|
|
2020-05-10 09:54:30 -05:00
|
|
|
assert_eq!(unsafe { add_asm(40, 2) }, 42);
|
2021-12-15 21:06:16 -06:00
|
|
|
|
|
|
|
let array1 = [1u8, 2, 3];
|
|
|
|
let mut array2 = [0u8, 0, 0];
|
|
|
|
unsafe {
|
|
|
|
mem_cpy(array2.as_mut_ptr(), array1.as_ptr(), 3);
|
|
|
|
}
|
|
|
|
assert_eq!(array1, array2);
|
2020-05-10 09:54:30 -05:00
|
|
|
}
|