title: const variables in local and global scope in assembly
body: <p>I've compiled the following C code in <a href="https://godbolt.org/z/YjeMr5" rel="nofollow noreferrer">Compiler Exporer</a> to see how it handles the <code>const</code> keyword:</p>
  <pre><code>int a=1;
  const b=2;
  int func () {
      int c=3;
      const int d=4;
  }
  </code></pre>
  <hr />
  <pre><code>        .section        .data
  a:
          .long   1
          .section        .rodata
  b:
          .long   2
  func:
          pushq   %rbp
          movq    %rsp, %rbp
          movl    $3, -4(%rbp)
          movl    $4, -8(%rbp)
          nop     # also, why does it add a nop here?
          popq    %rbp
          ret
  </code></pre>
  <p>From what I can tell, for a variable defined outside a function (global to the file), it adds a label at the top. However, if it is a <code>const</code> variable, then the variable at the top is placed in the read-only section. My question then is for the following local variable:</p>
  <pre><code>const int d=4;
  </code></pre>
  <p>How is its 'constant-ness' managed, as it's just a value on the stack, and can't any value on the stack be freely modified? Or, in assembly, is there no such thing as a constant local-variable, and this is just a compiler-enforced concept?</p>