Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
e7020e_2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gustav Hansson
e7020e_2020
Commits
1337defd
Commit
1337defd
authored
5 years ago
by
Gustav Hansson
Browse files
Options
Downloads
Patches
Plain Diff
bare0 0-5
parent
b2d69e6d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/bare0.rs
+49
-13
49 additions, 13 deletions
examples/bare0.rs
with
49 additions
and
13 deletions
examples/bare0.rs
+
49
−
13
View file @
1337defd
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment