@@ -58,7 +58,7 @@ but you must add the right number of `:` if you skip them:
58
58
asm!("xor %eax, %eax"
59
59
:
60
60
:
61
- : "eax"
61
+ : "{ eax} "
62
62
);
63
63
# } }
64
64
```
@@ -69,7 +69,7 @@ Whitespace also doesn't matter:
69
69
# #![feature(asm)]
70
70
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
71
71
# fn main() { unsafe {
72
- asm!("xor %eax, %eax" ::: "eax");
72
+ asm!("xor %eax, %eax" ::: "{ eax} ");
73
73
# } }
74
74
```
75
75
@@ -83,7 +83,7 @@ expressions must be mutable lvalues:
83
83
# #![feature(asm)]
84
84
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
85
85
fn add(a: i32, b: i32) -> i32 {
86
- let mut c = 0 ;
86
+ let c: i32 ;
87
87
unsafe {
88
88
asm!("add $2, $0"
89
89
: "=r"(c)
@@ -100,6 +100,21 @@ fn main() {
100
100
}
101
101
```
102
102
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
+
103
118
## Clobbers
104
119
105
120
Some instructions modify registers which might otherwise have held
@@ -112,7 +127,7 @@ stay valid.
112
127
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
113
128
# fn main() { unsafe {
114
129
// 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} ");
116
131
# } }
117
132
```
118
133
@@ -139,3 +154,14 @@ Current valid options are:
139
154
the compiler to insert its usual stack alignment code
140
155
3 . * intel* - use intel syntax instead of the default AT&T.
141
156
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