From c08dd88b999dc0852b1b9cce748c42b072ad30fb Mon Sep 17 00:00:00 2001
From: "henthe-5@student.ltu.se" <henthe-5@student.ltu.se>
Date: Tue, 19 Feb 2019 13:01:24 +0100
Subject: [PATCH] bare0_3 fix and bare0_5

---
 examples/bare0.rs | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/examples/bare0.rs b/examples/bare0.rs
index 2f36a34..d7f0c54 100644
--- a/examples/bare0.rs
+++ b/examples/bare0.rs
@@ -28,18 +28,37 @@ static mut Y: u32 = 0;
 #[entry]
 fn main() -> ! {
     // local mutabale variable (changed in safe code)
-    let mut x = unsafe { X };
+    let mut x = read_x();
     
     loop {
-        x.wrapping_add(1); // <- place breakpoint here (3)
-        unsafe {
-            X.wrapping_add(1);
-            Y = X;
-            assert!(x == X && X == Y +1 );
-        }
+        x = x.wrapping_add(1); // <- place breakpoint here (3)
+        write_x(read_x().wrapping_add(1));
+        write_y(read_x());
+        assert!(x == read_x() && read_x() == read_y() );
     }
 }
 
+fn read_x() -> u32 {
+    return unsafe { X };
+}
+
+fn read_y() -> u32 {
+    return unsafe { Y };
+}
+
+fn write_x(x: u32) {
+    unsafe {
+        X = x;
+    }
+}
+
+fn write_y(y: u32) {
+    unsafe {
+        Y = y
+    }
+    
+}
+
 // 0. Compile/build the example in debug (dev) mode.
 //
 //    > cargo build --example bare0
@@ -75,10 +94,10 @@ fn main() -> ! {
 //
 //    Change (both) += opertions to use wrapping_add
 //    load and run the progam, what happens
-//    ** x value stays at u32 max 4294967295**
+//    ** x value wraps around u32 max 4294967295 and get value 0**
 //
 //    Now continue exectution, what happens
-//    ** X,Y and x  = 4294967295 **
+//    ** X,Y and x is incremented and wraps around max **
 //
 //    Commit your answers (bare0_3)
 //
-- 
GitLab