0

In the x86 intel syntax, global variable values can be accessed with [rip+a], but when I try to assign a new value to a global variable address, like mov [rip+a], 4, a Segmentation Fault occurs. I was trying to write the above code in the same way as other register operations, but that does not seem to work. The compiler is cc, not gcc.

The code that causes the Segmentation Fault is as follows.

.intel_syntax noprefix
.globl main

a:
  .int 8
main:
  push rbp
  mov rbp, rsp
  sub rsp, 0
  push [rip+a]
  pop rax
  mov dword ptr [rip+a], 4 // adding this line causes Segmentation Fault
  mov rsp, rbp
  pop rbp
  ret

I couldn't find a way to output assembly in intel syntax at https://godbolt.org/ and I couldn't find any information about the above in my search, so I asked a question.


Thanks to Peter Cordes and fuz's answers, it worked with the following assembler.

.intel_syntax noprefix
.data
a:
  .int 8
  .text
.globl main
main:
  push rbp
  mov rbp, rsp
  sub rsp, 0
  mov dword ptr [rip+a], 4
  mov rax, [rip+a]
  mov rsp, rbp
  pop rbp
  ret
  • If you want to copy/paste from Godbolt, uncheck the "filter directives" option so it leaves in key directives like `.data` and `.text`. To remove some clutter, you can use `-g0` to leave out debug info; see [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Jul 30 '22 at 00:12
  • The `.text` section is not writable. Put your variable into the `.data` section. Also, I recommend aligning it to 8 bytes. – fuz Jul 30 '22 at 00:32
  • Thank you! I was able to write by using the `.data` section. Also, I did not remove the filter on godbolt to check. Thanks for the reference. – go horikoshi Jul 30 '22 at 03:37

0 Answers0