Skip to content

Commit 1013e20

Browse files
chorman0773ehuss
authored andcommitted
Finish adding inline tests to inline-assembly.md
1 parent fc7622c commit 1013e20

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/inline-assembly.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,39 @@ r[asm.rules.x86-x87]
11341134
- On x86, the x87 floating-point register stack must remain unchanged unless all of the `st([0-7])` registers have been marked as clobbered with `out("st(0)") _, out("st(1)") _, ...`.
11351135
- If all x87 registers are clobbered then the x87 register stack is guaranteed to be empty upon entering an `asm` block. Assembly code must ensure that the x87 register stack is also empty when exiting the asm block.
11361136

1137+
```rust
1138+
# #[cfg(target_arch = "x86_64")]
1139+
pub fn fadd(x: f64, y: f64) -> f64{
1140+
let mut out = 0f64;
1141+
let mut top = 0u16;
1142+
// we can do complex stuff with x87 if we clobber the entire x87 stack
1143+
unsafe{ core::arch::asm!(
1144+
"fld qword ptr [{x}]",
1145+
"fld qword ptr [{y}])",
1146+
"faddp",
1147+
"fstp qword ptr [{out}]",
1148+
"xor eax, eax",
1149+
"fstsw ax",
1150+
"shl eax, 11",
1151+
x = in(reg) &x,
1152+
y = in(reg) &y,
1153+
out = in(reg) &mut out,
1154+
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
1155+
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
1156+
out("eax") top
1157+
);}
1158+
1159+
assert_eq!(top & 0x7, 0);
1160+
out
1161+
}
1162+
1163+
pub fn main(){
1164+
# #[cfg(target_arch = "x86_64")]{
1165+
assert_eq!(fadd(1.0, 1.0), 2.0);
1166+
# }
1167+
}
1168+
```
1169+
11371170
r[asm.rules.arm64ec]
11381171
- On arm64ec, [call checkers with appropriate thunks](https://learn.microsoft.com/en-us/windows/arm/arm64ec-abi#authoring-arm64ec-in-assembly) are mandatory when calling functions.
11391172

@@ -1249,7 +1282,18 @@ The following directives are guaranteed to be supported by the assembler:
12491282
- `.uleb128`
12501283
- `.word`
12511284

1285+
```rust
1286+
# #[cfg(target_arch = "x86_64")] {
1287+
let bytes: *const u8;
1288+
let len: usize;
1289+
// `push` and `pop` are UB when used with nostack
1290+
unsafe { core::arch::asm!("jmp 3f", "2: .ascii \"Hello World!\"", "3: lea {bytes}, [2b+rip]", "mov {len}, 12", bytes = out(reg) bytes, len = out(reg) len); }
12521291

1292+
let s = unsafe{core::str::from_utf8_unchecked(core::slice::from_raw_parts(bytes, len))};
1293+
1294+
assert_eq!(s, "Hello World!");
1295+
# }
1296+
```
12531297

12541298
r[asm.target-specific-directives]
12551299
#### Target Specific Directive Support

0 commit comments

Comments
 (0)