Skip to content

Commit fa40c92

Browse files
authored
Merge pull request PacktPublishing#13 from PacktPublishing/fix#11
Make example ch01/a-assembly-dereference work on aarch64
2 parents d3bea0c + e3f7831 commit fa40c92

File tree

1 file changed

+24
-2
lines changed
  • ch01/a-assembly-dereference/src

1 file changed

+24
-2
lines changed
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1+
//! # FIXES:
2+
//! The number is identical to the number in the GitHub issue tracker
3+
//!
4+
//! ## FIX ISSUE #11:
5+
//! See:https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/11
6+
//! The book didn't make it clear that this example will only work on `x86-64` architecture,
7+
//! so users on newer M-series macs (which uses the ARM64 instruciton set), will get a
8+
//! compilation error. This is solved by conditionally compiling a version that works
9+
//! with the ARM64 instruction set.
110
211
use std::arch::asm;
312

413
fn main() {
514
let t = 100;
615
let t_ptr: *const usize = &t; // if you comment out this...
7-
// ...and uncomment the line below. The program will fail.
16+
// ...and uncomment the line below. The program will fail.
817
// let t_ptr = 99999999999999 as *const usize;
918
let x = dereference(t_ptr);
1019

1120
println!("{}", x);
1221
}
1322

23+
#[cfg(target_arch = "x86-64")]
1424
fn dereference(ptr: *const usize) -> usize {
1525
let mut res: usize;
16-
unsafe {
26+
unsafe {
1727
asm!("mov {0}, [{1}]", out(reg) res, in(reg) ptr)
1828
};
1929
res
2030
}
31+
32+
// FIX #11
33+
#[cfg(target_arch = "aarch64")]
34+
fn dereference(ptr: *const usize) -> usize {
35+
let mut res: usize;
36+
unsafe {
37+
asm!("ldr {0}, [{1}]", out(reg) res, in(reg) ptr)
38+
};
39+
res
40+
}
41+
42+

0 commit comments

Comments
 (0)