Look the next example:
#include <stdio.h>
int x = 0;
int main()
{
int y = 0;
for (int i = 0; i<10; i++){
y += 3;
x += 2;
}
return 0;
}
The correspondent assembly is:
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, -8(%rbp)
movl $0, -4(%rbp)
jmp .L2
.L3:
movl x(%rip), %eax
addl $2, %eax
movl %eax, x(%rip)
addl $3, -8(%rbp)
addl $1, -4(%rbp)
.L2:
cmpl $9, -4(%rbp)
jle .L3
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
My question is, why can variable y be changed directly from memory and the global variable x can't?
I am not sure if is because x86 is harvard