skoe · GitHub

First of all: I'm sorry if this is the wrong place to ask, but at least it's related to the book :)

While reading the Embedonomicon I wrote some bare metal stuff without using any crate. Just for learning how everything works together. I didn't find out why this source code:

    // Copy DATA
    let mut sdata = &mut _sdata as *mut u32;
    let edata = &mut _edata as *mut u32;
    let mut sidata = &_sidata as *const u32;
    while sdata < edata {
        ptr::write(sdata, ptr::read(sidata));
        sdata = sdata.offset(1);
        sidata = sidata.offset(1);
    }

was compiled to this assembly (copied from gdb):

│   0x8000026 <rustymc::run+18>     ldmia   r1!, {r2}                                                │
│   0x8000028 <rustymc::run+20>     stmia   r0!, {r2}                                                │
│  >0x800002a <rustymc::run+22>     b.n     0x8000026 <rustymc::run+18>                              │

An endless loop which will write to invalid memory sooner or later.

I boiled it down to a minimal program which is attached. I used rustc 1.47.0 and cargo run --release.

What's wrong with my code?

v3mini.tar.gz

Read the original on github.com ↗