From 1337defdfa6533a606ee5c832370bfc340f747de Mon Sep 17 00:00:00 2001 From: 97gushan <97gustavh@gmail.com> Date: Wed, 5 Feb 2020 10:55:15 +0100 Subject: [PATCH] bare0 0-5 --- examples/bare0.rs | 62 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/examples/bare0.rs b/examples/bare0.rs index 29ff386..e0ddb4d 100644 --- a/examples/bare0.rs +++ b/examples/bare0.rs @@ -22,25 +22,49 @@ use panic_semihosting as _; use cortex_m_rt::entry; // a constant (cannot be changed at run-time) -const X_INIT: u32 = 10; -// const X_INIT: u32 = core::u32::MAX; +const X_INIT: u32 = core::u32::MAX; // global mutable variables (changed using unsafe code) static mut X: u32 = X_INIT; static mut Y: u32 = 0; +fn write_x(x: u32, i: u32) { + unsafe{ + X = x.wrapping_add(i); + } +} + +fn write_y(y: u32) { + unsafe{ + Y = y; + } +} + + +fn read_y() -> u32 { + unsafe{ + Y + } +} + +fn read_x() -> u32 { + unsafe{ + X + } +} + #[entry] fn main() -> ! { // local mutable variable (changed in safe code) - let mut x = unsafe { X }; + let mut x = read_x(); loop { - x += 1; // <- place breakpoint here (3) - unsafe { - X += 1; - Y = X; - assert!(x == X && X == Y); - } + x = x.wrapping_add(1); // <- place breakpoint here (3) + + write_x(read_x(), 1); + write_y(read_x()); + assert!(x == read_x() && read_x() == read_y()); + } } @@ -56,18 +80,24 @@ fn main() -> ! { // // Look under Variables/Local what do you find. // -// ** your answer here ** +// x: 9389056 // // In the Expressions (WATCH -vscode) view add X and Y // what do you find // // ** your answer here ** +// X: 9389055 +// Y: <optimized out> // // Step through one complete iteration of the loop // and see how the (Local) Variables are updated // can you foresee what will eventually happen? // // ** place your answer here ** +// +// x will update to 9389057 +// eventually x will overflow and if rust runs in debug mode then rust will panic and +// if it runs in release mode rust wont check for the overflow and just do a wrap the value // // Commit your answers (bare0_1) // @@ -76,6 +106,8 @@ fn main() -> ! { // (Hint, look under OUTPUT/Adopter Output to see the `openocd` output.) // // ** your answer here ** +// The program panics +// panicked at 'attempt to add with overflow', examples/bare0.rs:42:9 // // Commit your answers (bare0_2) // @@ -84,10 +116,12 @@ fn main() -> ! { // Change (both) += operations to use wrapping_add // load and run the program, what happens // ** your answer here ** -// +// x wrapps around and become 0 +// X does nothing // Now continue execution, what happens // ** your answer here ** -// +// x continues to add up +// X still does nothing // Commit your answers (bare0_3) // // (If the program did not succeed back to the breakpoint @@ -96,7 +130,9 @@ fn main() -> ! { // 4. Change the assertion to `assert!(x == X && X == Y + 1)`, what happens? // // ** place your answer here ** -// +// panic because the assertion fails +// panicked at 'assertion failed: x == X && X == Y + 1', examples/bare0.rs:46:13 +// // Commit your answers (bare0_4) // // 5. Remove the assertion and implement "safe" functions for -- GitLab