title: x86_64 Assembly - Segfault when trying to edit a byte within an array in x64 assembly
body: <p>The tutorial I am following is for x86 and was written using 32-bit assembly, I'm trying to follow along while learning x64 assembly in the process. This has been going very well up until this lesson where I have the following simple program which simply tries to modify a single character in a string; it compiles fine but segfaults when ran.</p>
  
  <pre><code>section .text
  
  global _start ; Declare global entry oint for ld
  _start:
  
      jmp short message ; Jump to where or message is at so we can do a call to push the address onto the stack
  
      code:   
      xor rax, rax    ; Clean up the registers
      xor rbx, rbx
      xor rcx, rcx
      xor rdx, rdx
  
      ; Try to change the N to a space
      pop rsi ; Get address from stack
      mov al, 0x20 ; Load 0x20 into RAX
      mov [rsi], al; Why segfault?
      xor rax, rax; Clear again
  
      ; write(rdi, rsi, rdx) = write(file_descriptor, buffer, length)
      mov al, 0x01    ; write the command for 64bit Syscall Write (0x01) into the lower 8 bits of RAX
      mov rdi, rax    ; First Paramter, RDI = 0x01 which is STDOUT, we move rax to ensure the upper 56 bits of RDI are zero
      ;pop rsi        ; Second Parameter, RSI = Popped address of message from stack
      mov dl, 25  ; Third Parameter, RDX = Length of message
      syscall     ; Call Write
  
      ; exit(rdi) = exit(return value)    
      xor rax, rax    ; write returns # of bytes written in rax, need to clean it up again
      add rax, 0x3C   ; 64bit syscall exit is 0x3C
      xor rdi, rdi    ; Return value is in rdi (First parameter), zero it to return 0
      syscall     ; Call Exit
  
      message:
      call code   ; Pushes the address of the string onto the stack
      db 'AAAABBBNAAAAAAAABBBBBBBB',0x0A
  </code></pre>
  
  <p>This culprit is this line:</p>
  
  <pre><code>mov [rsi], al; Why segfault?
  </code></pre>
  
  <p>If I comment it out, then the program runs fine, outputting the message 'AAAABBBNAAAAAAAABBBBBBBB', why can't I modify the string?</p>
  
  <p>The authors code is the following:</p>
  
  <pre><code>global _start
  
  
  _start:
          jmp short ender
  
          starter:
  
          pop ebx                 ;get the address of the string
          xor eax, eax
  
          mov al, 0x20
          mov [ebx+7], al        ;put a NULL where the N is in the string
  
          mov al, 4       ;syscall write
          mov bl, 1       ;stdout is 1
          pop ecx         ;get the address of the string from the stack
          mov dl, 25       ;length of the string
          int 0x80
  
          xor eax, eax
          mov al, 1       ;exit the shellcode
          xor ebx,ebx
          int 0x80
  
          ender:
          call starter
          db 'AAAABBBNAAAAAAAABBBBBBBB'0x0A
  </code></pre>
  
  <p>And I've compiled that using:</p>
  
  <pre><code>nasm -f elf &lt;infile&gt; -o &lt;outfile&gt;
  ld -m elf_i386 &lt;infile&gt; -o &lt;outfile&gt;
  </code></pre>
  
  <p>But even that causes a segfault, images on the page show it working properly and changing the N into a space, however I seem to be stuck in segfault land :( Google isn't really being helpful in this case, and so I turn to you stackoverflow, any pointers (no pun intended!) would be appreciated </p>