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