Skip to content

Commit 2ed2686

Browse files
authored
Merge pull request #1542 from alex34567/master
Rework cpuid asm example.
2 parents 1694968 + 4b46d47 commit 2ed2686

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/unsafe/asm.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,24 @@ fn main() {
232232
// three entries of four bytes each
233233
let mut name_buf = [0_u8; 12];
234234
// String is stored as ascii in ebx, edx, ecx in order
235-
// Because ebx is reserved, we get a scratch register and move from
236-
// ebx into it in the asm. The asm needs to preserve the value of
237-
// that register though, so it is pushed and popped around the main asm
235+
// Because ebx is reserved, the asm needs to preserve the value of it.
236+
// So we push and pop it around the main asm.
238237
// (in 64 bit mode for 64 bit processors, 32 bit processors would use ebx)
239238

240239
unsafe {
241240
asm!(
242241
"push rbx",
243242
"cpuid",
244-
"mov [{0}], ebx",
245-
"mov [{0} + 4], edx",
246-
"mov [{0} + 8], ecx",
243+
"mov [rdi], ebx",
244+
"mov [rdi + 4], edx",
245+
"mov [rdi + 8], ecx",
247246
"pop rbx",
248247
// We use a pointer to an array for storing the values to simplify
249248
// the Rust code at the cost of a couple more asm instructions
250249
// This is more explicit with how the asm works however, as opposed
251250
// to explicit register outputs such as `out("ecx") val`
252251
// The *pointer itself* is only an input even though it's written behind
253-
in(reg) name_buf.as_mut_ptr(),
252+
in("rdi") name_buf.as_mut_ptr(),
254253
// select cpuid 0, also specify eax as clobbered
255254
inout("eax") 0 => _,
256255
// cpuid clobbers these registers too
@@ -269,9 +268,11 @@ This instruction writes to `eax` with the maximum supported `cpuid` argument and
269268

270269
Even though `eax` is never read we still need to tell the compiler that the register has been modified so that the compiler can save any values that were in these registers before the asm. This is done by declaring it as an output but with `_` instead of a variable name, which indicates that the output value is to be discarded.
271270

272-
This code also works around the limitation that `ebx` is a reserved register by LLVM. That means that LLVM assumes that it has full control over the register and it must be restored to its original state before exiting the asm block, so it cannot be used as an output. To work around this we save the register via `push`, read from `ebx` inside the asm block into a temporary register allocated with `out(reg)` and then restoring `ebx` to its original state via `pop`. The `push` and `pop` use the full 64-bit `rbx` version of the register to ensure that the entire register is saved. On 32 bit targets the code would instead use `ebx` in the `push`/`pop`.
271+
This code also works around the limitation that `ebx` is a reserved register by LLVM. That means that LLVM assumes that it has full control over the register and it must be restored to its original state before exiting the asm block, so it cannot be used as an input or output **except** if the compiler uses it to fulfill a general register class (e.g. `in(reg)`). This makes `reg` operands dangerous when using reserved registers as we could unknowingly corrupt out input or output because they share the same register.
273272

274-
This can also be used with a general register class (e.g. `reg`) to obtain a scratch register for use inside the asm code:
273+
To work around this we use `rdi` to store the pointer to the output array, save `ebx` via `push`, read from `ebx` inside the asm block into the array and then restoring `ebx` to its original state via `pop`. The `push` and `pop` use the full 64-bit `rbx` version of the register to ensure that the entire register is saved. On 32 bit targets the code would instead use `ebx` in the `push`/`pop`.
274+
275+
This can also be used with a general register class to obtain a scratch register for use inside the asm code:
275276

276277
```rust
277278
use std::arch::asm;

0 commit comments

Comments
 (0)