diff options
| author | bors <bors@rust-lang.org> | 2015-04-30 10:13:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-04-30 10:13:05 +0000 |
| commit | 42bfeec53c266fb0b08ad90d324206bd3d64df16 (patch) | |
| tree | 0572b5a53d703783a3b6209e18d1b44be258547e /src/doc/trpl | |
| parent | b59403606953d01391c1faa38d96062e3dfafa3d (diff) | |
| parent | 766c1bc99283a553fe893469fafa1f7e82ac6157 (diff) | |
| download | rust-42bfeec53c266fb0b08ad90d324206bd3d64df16.tar.gz rust-42bfeec53c266fb0b08ad90d324206bd3d64df16.zip | |
Auto merge of #24842 - GBGamer:patch-3, r=steveklabnik
They now use the currently working syntax. Also, I added two examples.
Diffstat (limited to 'src/doc/trpl')
| -rw-r--r-- | src/doc/trpl/inline-assembly.md | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/doc/trpl/inline-assembly.md b/src/doc/trpl/inline-assembly.md index 1a4592f980f..58c2a982dd3 100644 --- a/src/doc/trpl/inline-assembly.md +++ b/src/doc/trpl/inline-assembly.md @@ -58,7 +58,7 @@ but you must add the right number of `:` if you skip them: asm!("xor %eax, %eax" : : - : "eax" + : "{eax}" ); # } } ``` @@ -69,7 +69,7 @@ Whitespace also doesn't matter: # #![feature(asm)] # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] # fn main() { unsafe { -asm!("xor %eax, %eax" ::: "eax"); +asm!("xor %eax, %eax" ::: "{eax}"); # } } ``` @@ -77,13 +77,13 @@ asm!("xor %eax, %eax" ::: "eax"); Input and output operands follow the same format: `: "constraints1"(expr1), "constraints2"(expr2), ..."`. Output operand -expressions must be mutable lvalues: +expressions must be mutable lvalues, or not yet assigned: ``` # #![feature(asm)] # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] fn add(a: i32, b: i32) -> i32 { - let mut c = 0; + let c: i32; unsafe { asm!("add $2, $0" : "=r"(c) @@ -100,6 +100,22 @@ fn main() { } ``` +If you would like to use real operands in this position, however, +you are required to put curly braces `{}` around the register that +you want, and you are required to put the specific size of the +operand. This is useful for very low level programming, where +which register you use is important: + +``` +# #![feature(asm)] +# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +# unsafe fn read_byte_in(port: u16) -> u8 { +let result: u8; +asm!("in %dx, %al" : "={al}"(result) : "{dx}"(port)); +result +# } +``` + ## Clobbers Some instructions modify registers which might otherwise have held @@ -112,7 +128,7 @@ stay valid. # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] # fn main() { unsafe { // Put the value 0x200 in eax -asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "eax"); +asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "{eax}"); # } } ``` @@ -139,3 +155,14 @@ Current valid options are: the compiler to insert its usual stack alignment code 3. *intel* - use intel syntax instead of the default AT&T. +``` +# #![feature(asm)] +# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +# fn main() { +let result: i32; +unsafe { + asm!("mov eax, 2" : "={eax}"(result) : : : "intel") +} +println!("eax is currently {}", result); +# } +``` |
