The volatile modifier adds instructions

The volatile modifier adds instructions to expand the variable to a 32-bit value (UXTB, UXTH, SXTB, SXTH).
It doesn't make sense. checking code online

#include "stdint.h"

struct _st
{
     uint8_t       a;
    volatile  uint8_t    b;
}st;

uint32_t test(uint8_t c)
{
    uint8_t out;
    if(st.a > c) out = st.a;
    else out = st.b; 
    return out;
};
код
test:
  ldr r2, .L3
  ldrb r3, [r2] //reading st.a
  cmp r3, r0
  bhi .L2
  ldrb r3, [r2, #1] //reading st.b
  uxtb r3, r3    // <<<<< 
.L2:
  mov r0, r3
  bx lr
.L3:
  .word .LANCHOR0
st:
Parents
  • Dear AVI_crak,

    On arm 32bit, we are working with 32bit registers, so it must do something with the "rest" of an uint8_t (unsigned int on 8bits) that you're playing with.

    Technically, if you see that you're using it always as an 8bit value, you can discard the forcing of extending it to a 32bit value, which is what it's done when you return st.a. However, by using the volatile qualifier, you tell the compiler to not optimize this variable. Thus, the expansion of the 8bit variable to fill a 32bit register remain.

    Best Regards,
    Willy

Reply
  • Dear AVI_crak,

    On arm 32bit, we are working with 32bit registers, so it must do something with the "rest" of an uint8_t (unsigned int on 8bits) that you're playing with.

    Technically, if you see that you're using it always as an 8bit value, you can discard the forcing of extending it to a 32bit value, which is what it's done when you return st.a. However, by using the volatile qualifier, you tell the compiler to not optimize this variable. Thus, the expansion of the 8bit variable to fill a 32bit register remain.

    Best Regards,
    Willy

Children