memorysegmentation-faultnasmcpu-registersmov

SIGSEGV on simple move register to memory in NASM


I must be missing something very basic here. Searched SO but could not find the answer to this particular question. Here's my NASM code:

%include "io64.inc"

section .text
    myvar db "This is not working", 0
global CMAIN
CMAIN:
    mov rbp, rsp; for correct debugging
    ;write your code here
    xor rax, rax
    mov [myvar], rax
    ret

It crashes on the move [myvar], rax line with SIGSEGV. I am simply trying to store some zeroes at that address.

Thanks!

PS: Using SASM to build / run / debug with 64 bit option ticked (default settings otherwise), on Windows 10 64 bit.


Solution

  • section .text
       myvar db "This is not working", 0
    

    Section .text is an executable section without write permissions. This is done to prevent some kinds of vulnerabilities. You should either place your myvar into a writable section, e.g. .data (if the variable should live for the whole duration of program execution), have the variable on the stack (if it's not supposed to outlive the function where it's created), or change .text to be writable (not recommended for security reasons, but possible).