Skip to content

Commit e435e57

Browse files
author
Commeownist
committed
Add rudimentary asm tests
1 parent 29c3d2d commit e435e57

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

tests/run/asm.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,92 @@ fn main() {
6262
}
6363
assert_eq!(x, 43);
6464

65+
// check inout(reg_class) x
66+
let mut x: u64 = 42;
67+
unsafe {
68+
asm!("add {0}, {0}",
69+
inout(reg) x
70+
);
71+
}
72+
assert_eq!(x, 84);
73+
74+
// check inout("reg") x
75+
let mut x: u64 = 42;
76+
unsafe {
77+
asm!("add r11, r11",
78+
inout("r11") x
79+
);
80+
}
81+
assert_eq!(x, 84);
82+
83+
// check a mix of
84+
// in("reg")
85+
// inout(class) x => y
86+
// inout (class) x
87+
let x: u64 = 702;
88+
let y: u64 = 100;
89+
let res: u64;
90+
let mut rem: u64 = 0;
91+
unsafe {
92+
asm!("div r11",
93+
in("r11") y,
94+
inout("eax") x => res,
95+
inout("edx") rem,
96+
);
97+
}
98+
assert_eq!(res, 7);
99+
assert_eq!(rem, 2);
100+
101+
// check const
102+
let mut x: u64 = 42;
103+
unsafe {
104+
asm!("add {}, {}",
105+
inout(reg) x,
106+
const 1
107+
);
108+
}
109+
assert_eq!(x, 43);
110+
111+
// check const (ATT syntax)
112+
let mut x: u64 = 42;
113+
unsafe {
114+
asm!("add {}, {}",
115+
const 1,
116+
inout(reg) x,
117+
options(att_syntax)
118+
);
119+
}
120+
assert_eq!(x, 43);
121+
122+
// check sym fn
123+
extern "C" fn foo() -> u64 { 42 }
124+
let x: u64;
125+
unsafe {
126+
asm!("call {}", sym foo, lateout("rax") x);
127+
}
128+
assert_eq!(x, 42);
129+
130+
// check sym fn (ATT syntax)
131+
let x: u64;
132+
unsafe {
133+
asm!("call {}", sym foo, lateout("rax") x, options(att_syntax));
134+
}
135+
assert_eq!(x, 42);
136+
137+
// check sym static
138+
static FOO: u64 = 42;
139+
let x: u64;
140+
unsafe {
141+
asm!("mov {1}, qword ptr [rip + {0}]", sym FOO, lateout(reg) x);
142+
}
143+
assert_eq!(x, 42);
144+
145+
// check sym static (ATT syntax)
146+
let x: u64;
147+
unsafe {
148+
asm!("movq {0}(%rip), {1}", sym FOO, lateout(reg) x, options(att_syntax));
149+
}
150+
assert_eq!(x, 42);
151+
65152
assert_eq!(unsafe { add_asm(40, 2) }, 42);
66153
}

0 commit comments

Comments
 (0)