Skip to content

Commit 3d0ad46

Browse files
committed
Fix the inline assembly examples
They now use the currently working syntax.
1 parent 314b1f1 commit 3d0ad46

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/doc/trpl/inline-assembly.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ but you must add the right number of `:` if you skip them:
5858
asm!("xor %eax, %eax"
5959
:
6060
:
61-
: "eax"
61+
: "{eax}"
6262
);
6363
# } }
6464
```
@@ -69,7 +69,7 @@ Whitespace also doesn't matter:
6969
# #![feature(asm)]
7070
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
7171
# fn main() { unsafe {
72-
asm!("xor %eax, %eax" ::: "eax");
72+
asm!("xor %eax, %eax" ::: "{eax}");
7373
# } }
7474
```
7575

@@ -83,7 +83,7 @@ expressions must be mutable lvalues:
8383
# #![feature(asm)]
8484
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
8585
fn add(a: i32, b: i32) -> i32 {
86-
let mut c = 0;
86+
let c: i32;
8787
unsafe {
8888
asm!("add $2, $0"
8989
: "=r"(c)
@@ -100,6 +100,21 @@ fn main() {
100100
}
101101
```
102102

103+
If you would like to use real operands in this position, however,
104+
you are required to put curly braces `{}` around the register that
105+
you want, and you are required to put the specific size of the
106+
operand. This is useful for very low level programming, where
107+
which register you use is important:
108+
109+
```
110+
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
111+
# unsafe fn read_byte_in(port: u16) -> u8 {
112+
let result: u8;
113+
asm!("in %dx, %al" : "={al}"(result) : "{dx}"(port));
114+
result
115+
# }
116+
```
117+
103118
## Clobbers
104119

105120
Some instructions modify registers which might otherwise have held
@@ -112,7 +127,7 @@ stay valid.
112127
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
113128
# fn main() { unsafe {
114129
// Put the value 0x200 in eax
115-
asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "eax");
130+
asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "{eax}");
116131
# } }
117132
```
118133

@@ -139,3 +154,14 @@ Current valid options are:
139154
the compiler to insert its usual stack alignment code
140155
3. *intel* - use intel syntax instead of the default AT&T.
141156

157+
```
158+
# #![feature(asm)]
159+
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
160+
# fn main() {
161+
let result: i32;
162+
unsafe {
163+
asm!("mov eax, 2" : "={eax}"(result) : : : "intel")
164+
}
165+
println!("eax is currently {}", result);
166+
# } }
167+
```

0 commit comments

Comments
 (0)