# Testing asm volatile generates "incorrect" code

**URL:** <https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892>\
**Category:** Help\
**Tags:** compiler\
**Created:** [September 5, 2024, 11:43am UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892 "2024-09-05T11:43:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [September 5, 2024, 11:43am UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/1 "2024-09-05T11:43:22Z")

</div>

consider the following code, which inspects interrupt status before/after actually disabling all CPU interrupts:

```zig
export var mask: u32 = 0;
export var flag: bool = false;

export fn intrTest() void {
    mask = get_PRIMASK(); // correctly retrieves 'primask' register
    flag = isEnabled(); // incorrectly returns true, regardless of 'primask'
    asm volatile ("cpsid i" ::: "memory"); // disable interrupts
    mask = get_PRIMASK(); // ditto
    flag = isEnabled(); // ditto
}

pub fn get_PRIMASK() u32 {
    const m: u32 = 0;
    asm volatile (
        \\mrs %[m], primask        
        :
        : [m] "r" (m),
        : "memory"
    );
    return m;
}

pub fn isEnabled() bool {
    return (get_PRIMASK() == 0);
}

```

[[i’m targeting a cortex M0+… here’s a [godbolt](https://godbolt.org/z/afjGKeEo6) link… ]]

the results of compilation under zig 0.13.0 are as follows:

```asm
intrTest:
        movs r0, #0
        mrs r0, primask
        ldr r1, .LCPI0_0
        str r0, [r1, #4]
        mrs r0, primask
        movs r2, #1
        strb r2, [r1]
        cpsid i
        mrs r0, primask
        str r0, [r1, #4]
        mrs r0, primask
        strb r2, [r1]
        bx lr
.LCPI0_0:
        .long .L_MergedGlobals

```

i’m suspicious of the `movs r2, #1` instruction, as it effectively “asserts” that the `primask` register equals 0 – `isEnabled` in my source code…

continuing further, the `strb r2, [r1]` instruction that occurs **after** interrupts are disabled with `cpsid i` simply stores the **same** value of 1…

while there is a `mrs r0, primask` instruction at the beginning of my `isEnabled` function, isn’t it strange that there’s no subsequent code that compares this value to 0???

while i originally though the adjacent calls to `get_PRIMASK` were giving the compiler problems here, a smaller example that **only** uses `isEnabled` exhibits the same problem… for whatever reason, the `results of get_PRIMASK` **inside** `isEnabled` are simply being swallowed…

i just recently discovered this problem in much larger codebase – where the generated code had the same anomaly… the results are easily inspected using a debugger, logic analyzer, etc…

needless to say, having `isEnabled` always return true is leading to all sorts race conditions in my embedded apps 🤪

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [September 5, 2024, 1:37pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/2 "2024-09-05T13:37:37Z")

</div>

Consider adding a memory fence after saving the `flag`.  
e.g.

```zig
    flag = isEnabled(); // incorrectly returns true, regardless of 'primask'
    asm volatile ("dmb");

```

EDIT:  
You can also use the zig builtin:

```zig
    flag = isEnabled(); // incorrectly returns true, regardless of 'primask'  
    @fence(.seq_cst);

```

The following is heavier and safer:

```zig
    @atomicStore(bool, &flag, isEnabled(), .seq_cst);

```

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [September 5, 2024, 2:22pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/3 "2024-09-05T14:22:01Z")

</div>

adding the fence has no effect… the same `movs r2, #1` instruction is present, instead of a test on `primask`…

[[my understanding is that the cortex-m0+ architecture, while supporting the `dmb` instruction, treats it as a `nop`… fences **are** required on some the more advanced architectures with more complex instruction pipelines]]

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [September 5, 2024, 2:52pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/4 "2024-09-05T14:52:44Z")

</div>

try:

```zig
pub fn get_PRIMASK() u32 {
    return asm volatile (
        \\mrs r0, primask
        : [ret] "={r0}" (-> u32)
    );
}

```

EDIT:  
or

```zig
pub fn get_PRIMASK() u32 {
    return asm volatile (
        \\mrs %[ret], primask
        : [ret] "=r" (-> u32)
    );
}

```

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [September 5, 2024, 3:18pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/5 "2024-09-05T15:18:10Z")

</div>

i believe that works!!!

to increase my understanding, explain what’s different in your version of `get_PRIMASK` from my original…

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [September 5, 2024, 3:33pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/6 "2024-09-05T15:33:37Z")

</div>

Before entering the optimizer the code for:

```zig
pub fn get_PRIMASK() u32 {
    const m: u32 = 0;
    asm volatile (
        \\mrs %[m], primask        
        :
        : [m] "r" (m),
        : "memory"
    );
    return m;
}

```

is:

```zig
get_PRIMASK:
        sub sp, #4
        movs r0, #0
        str r0, [sp]
        mrs r0, primask
        add sp, #4
        bx lr

```

* * *

for:

```zig
pub fn get_PRIMASK() u32 {
    return asm volatile (
        \\mrs %[ret], primask
        : [ret] "=r" (-> u32)
    );
}

```

is

```zig
get_PRIMASK:
        mrs r0, primask
        bx lr

```

* * *

Normally the optimizer must erase the stack manipulation in the first case and inline both functions as:

```zig
        mrs r0, primask

```

I believe that when the compiler optimizes the first/more complex case, it triggers a bug, because the resulting code makes no sense.

---

<div class="post-metadata">

**Author:** ![alp](https://ziggit.dev/user_avatar/ziggit.dev/alp/32/545_2.png) [@alp](https://ziggit.dev/u/alp)\
**Post date:** [September 5, 2024, 3:59pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/7 "2024-09-05T15:59:39Z")

</div>

Providing some clarification here because there is very little documentation around inline ASM, and I would like to save anyone from tearing their hair out in the future 🫠

First, the optimizer will _never_ optimize your inline assembly. As a matter of fact, it does not even analyze it. Hence the need to tell the compiler what registers/memory you are clobbering. If assembly were analyzed, the compiler would know exactly what is clobbered already.

Second, I’ll explain why the first example doesn’t work. What you’re asking the compiler to do is _enter_ your inline asm block with the **value (0)** into an arbitrary register (hint: this allows the compiler to optimize the _boundaries_ of your inline asm, but not the asm itself, because the compiler is allowed to choose the best register)! The inline asm block then reads primask into a register, but does not change the value of m. For that, you need a pointer. I think this is what you were going for (although dimdin’s suggestion is optimal):

```zig
asm volatile(
    \\ mrs r0, primask
    \\ str r0, %[m]
    :
    : [m] "p" (&m)
    : "r0", "memory"
)

```

The thread solution works because you are allocating an output register. This tells the compiler: at the end of my inline asm expression, the result of the assembly block should be in this register. So you read primask into the register, your asm block ends, and the value is correctly returned from the block.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![LucasSantos91](https://ziggit.dev/user_avatar/ziggit.dev/lucassantos91/32/440_2.png) [@LucasSantos91](https://ziggit.dev/u/LucasSantos91)\
**Post date:** [September 5, 2024, 6:51pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/8 "2024-09-05T18:51:37Z")

</div>

> [@biosbob](#):
>
> ```zig
> const m: u32 = 0;
> asm volatile (
> \\mrs %[m], primask        
> :
> : [m] "r" (m),
> : "memory"
> );
> return m;
> 
> ```

I don’t think this does what you think it does. `m` is const, so you can’t write to it, even in assembly. Also, you’re not passing the address of `m` to the assembly, you’re passing the value of `m`, which is 0.  
This function is equivalent to this:

```zig
/*Assembly*/ mrs (register containing 0), primask
/*Zig*/return 0;

```

Since `get_PRIMASK` always returns 0, `is_enabled` can be optimized to simply `return true;`. Hence why there is no comparison to 0.

I think what you meant was:

```zig
pub fn get_PRIMASK() u32 {
    var m: u32 = 0;
    asm volatile (
        \\mrs %[m], primask        
        :
        : [m] "r" (&m),
        : "memory"
    );
    return m;
}

```

But @dimdin code is better anyways.

---

<div class="post-metadata">

**Author:** ![andy\_mango](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@andy\_mango](https://ziggit.dev/u/andy_mango)\
**Post date:** [September 6, 2024, 10:45pm UTC](https://ziggit.dev/t/testing-asm-volatile-generates-incorrect-code/5892/9 "2024-09-06T22:45:35Z")

</div>

I thought I might chirp in here because I have a few head wounds in this area. In my particular case I needed access to the v7m load and store instructions that perform a memory access in an unprotected manner, i.e. `ldr?t` and `str?t`. I started by cribbing the clang header file from CMSIS version 6. For `ldrbt` that looks like (I advise welding goggles when reading CMSIS code):

```zig
__STATIC_FORCEINLINE uint8_t__ LDRBT(volatile uint8_t *ptr)
{
  uint32_t result;

  __ASM volatile ("ldrbt %0, %1" : "=r" (result) : "Q" (*ptr) );
  return ((uint8_t)result); /* Add explicit type cast here */
}

```

After many trials, I found the following inserted the correct code:

```zig
pub inline fn ldrbt(
    ptr: *const mmio.ByteRegister,
) mmio.ByteRegister {
    return asm volatile ("ldrbt %[ret], [%[ptr]]"
        : [ret] "=r" (-> mmio.ByteRegister),
        : [ptr] "r" (ptr),
    );
}

```

The lesson I learned was to drive into my head that the three sections after the assembly language template are for outputs, inputs, and clobbers, in that order. In this case, the output is the byte retrieved via the input pointer. Just have to keep repeating to myself what I’m trying to accomplish. Many fewer bruises now.
