From 3d0ad46eea29876ac56c7843f893a041e658bf4e Mon Sep 17 00:00:00 2001 From: Nicholas <npmazzuca@gmail.com> Date: Sun, 26 Apr 2015 10:26:56 -0700 Subject: [PATCH 1/3] Fix the inline assembly examples They now use the currently working syntax. --- src/doc/trpl/inline-assembly.md | 34 +++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/doc/trpl/inline-assembly.md b/src/doc/trpl/inline-assembly.md index 1a4592f980fa7..ab360de62f521 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}"); # } } ``` @@ -83,7 +83,7 @@ expressions must be mutable lvalues: # #![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,21 @@ 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: + +``` +# #[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 +127,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 +154,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); +# } } +``` From a70b2ed826d5e51429e035c6924c63c9511afeac Mon Sep 17 00:00:00 2001 From: Nicholas <npmazzuca@gmail.com> Date: Sun, 26 Apr 2015 10:34:28 -0700 Subject: [PATCH 2/3] Outputs no longer have to be mutable --- src/doc/trpl/inline-assembly.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/trpl/inline-assembly.md b/src/doc/trpl/inline-assembly.md index ab360de62f521..091eadcc9d820 100644 --- a/src/doc/trpl/inline-assembly.md +++ b/src/doc/trpl/inline-assembly.md @@ -77,7 +77,7 @@ 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)] From 766c1bc99283a553fe893469fafa1f7e82ac6157 Mon Sep 17 00:00:00 2001 From: Nicholas <npmazzuca@gmail.com> Date: Sun, 26 Apr 2015 19:44:44 -0700 Subject: [PATCH 3/3] Fix the errors --- src/doc/trpl/inline-assembly.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/doc/trpl/inline-assembly.md b/src/doc/trpl/inline-assembly.md index 091eadcc9d820..58c2a982dd309 100644 --- a/src/doc/trpl/inline-assembly.md +++ b/src/doc/trpl/inline-assembly.md @@ -107,6 +107,7 @@ 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; @@ -163,5 +164,5 @@ unsafe { asm!("mov eax, 2" : "={eax}"(result) : : : "intel") } println!("eax is currently {}", result); -# } } +# } ```