Skip to content
Snippets Groups Projects
Commit 1337defd authored by Gustav Hansson's avatar Gustav Hansson
Browse files

bare0 0-5

parent b2d69e6d
Branches
No related tags found
No related merge requests found
...@@ -22,25 +22,49 @@ use panic_semihosting as _; ...@@ -22,25 +22,49 @@ use panic_semihosting as _;
use cortex_m_rt::entry; use cortex_m_rt::entry;
// a constant (cannot be changed at run-time) // 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) // global mutable variables (changed using unsafe code)
static mut X: u32 = X_INIT; static mut X: u32 = X_INIT;
static mut Y: u32 = 0; 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] #[entry]
fn main() -> ! { fn main() -> ! {
// local mutable variable (changed in safe code) // local mutable variable (changed in safe code)
let mut x = unsafe { X }; let mut x = read_x();
loop { loop {
x += 1; // <- place breakpoint here (3) x = x.wrapping_add(1); // <- place breakpoint here (3)
unsafe {
X += 1; write_x(read_x(), 1);
Y = X; write_y(read_x());
assert!(x == X && X == Y); assert!(x == read_x() && read_x() == read_y());
}
} }
} }
...@@ -56,18 +80,24 @@ fn main() -> ! { ...@@ -56,18 +80,24 @@ fn main() -> ! {
// //
// Look under Variables/Local what do you find. // Look under Variables/Local what do you find.
// //
// ** your answer here ** // x: 9389056
// //
// In the Expressions (WATCH -vscode) view add X and Y // In the Expressions (WATCH -vscode) view add X and Y
// what do you find // what do you find
// //
// ** your answer here ** // ** your answer here **
// X: 9389055
// Y: <optimized out>
// //
// Step through one complete iteration of the loop // Step through one complete iteration of the loop
// and see how the (Local) Variables are updated // and see how the (Local) Variables are updated
// can you foresee what will eventually happen? // can you foresee what will eventually happen?
// //
// ** place your answer here ** // ** 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) // Commit your answers (bare0_1)
// //
...@@ -76,6 +106,8 @@ fn main() -> ! { ...@@ -76,6 +106,8 @@ fn main() -> ! {
// (Hint, look under OUTPUT/Adopter Output to see the `openocd` output.) // (Hint, look under OUTPUT/Adopter Output to see the `openocd` output.)
// //
// ** your answer here ** // ** your answer here **
// The program panics
// panicked at 'attempt to add with overflow', examples/bare0.rs:42:9
// //
// Commit your answers (bare0_2) // Commit your answers (bare0_2)
// //
...@@ -84,10 +116,12 @@ fn main() -> ! { ...@@ -84,10 +116,12 @@ fn main() -> ! {
// Change (both) += operations to use wrapping_add // Change (both) += operations to use wrapping_add
// load and run the program, what happens // load and run the program, what happens
// ** your answer here ** // ** your answer here **
// // x wrapps around and become 0
// X does nothing
// Now continue execution, what happens // Now continue execution, what happens
// ** your answer here ** // ** your answer here **
// // x continues to add up
// X still does nothing
// Commit your answers (bare0_3) // Commit your answers (bare0_3)
// //
// (If the program did not succeed back to the breakpoint // (If the program did not succeed back to the breakpoint
...@@ -96,7 +130,9 @@ fn main() -> ! { ...@@ -96,7 +130,9 @@ fn main() -> ! {
// 4. Change the assertion to `assert!(x == X && X == Y + 1)`, what happens? // 4. Change the assertion to `assert!(x == X && X == Y + 1)`, what happens?
// //
// ** place your answer here ** // ** 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) // Commit your answers (bare0_4)
// //
// 5. Remove the assertion and implement "safe" functions for // 5. Remove the assertion and implement "safe" functions for
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment