Search code examples
assemblymutableinstruction-set

Mutable data section?


I'm developing a virtual CPU for fun and I am currently working on the assembly part.

Now I wonder if values in the .data section should be mutable or not.

section .data
  MyGlobalVar dw 10h

In the places where I reference MyGlobalVar, should I resolve the value at assembly time or runtime? Which would be the most appropriate way of doing it?

mov ra, 10h          ; Assembly time resolved value
mov ra, [0x00000000] ; Address of MyGlobalVar dereferenced when executed

Solution

  • If you implicitly dereferenced at assemble time to produce an immediate constant, you'd always get the value from the source code. That's what equ is for; programmers can use that if that's what they want instead of a load from .rodata. (In that case, the bytes in that section would only be accessed by code that indexes a lookup table or something like that.)

    And obviously that doesn't work at all for mutable data, it has to be a load from the memory location other code might have modified.

    It's assembly language; don't change the program instructions to something with an architecturally different effect. You're writing an assembler, not a compiler. Even a section like .rodata that normally gets mapped into read-only pages can get modified by a debugger, potentially.