2

I am using x86 GNU assembly with GCC and am attempting to implement the Assembly equivalent of the following c/c++:

int x[10];
x[0] = 5;

However, when I attempt to run (with command ./a.out) my Assembly code below (after first compiling with the gcc filename.s), the error Segmentation fault: 11 is printed to the console:

.data
  x:.fill 10
  index:.int 0

.text
.globl _main
_main:
  pushq %rbp
  movq %rsp, %rbp
  subq $16, %rsp
  lea x(%rip), %rdi
  mov index(%rip), %rsi;
  movl $5, %eax;
  movl %eax, (%rdi, %rsi, 4);
  leave
  ret

In order to declare the array, I followed the instructions found here: Declaring Arrays In x86 Assembly.

Does anyone know why this behavior is happening? I am running this code on Mac OSX with the gcc compiler using GNU GAS syntax.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • If you want 10 integers (4 bytes) you'll need to do something like `x:.fill 10, 4` . Your version allocates 10 bytes (size of 1 is default if not specified) – Michael Petch Jul 10 '18 at 00:50
  • @MichaelPetch Thank you very much! If you could write your response in an answer, I will gladly accept it. – Ajax1234 Jul 10 '18 at 00:53

1 Answers1

1

As pointed out by @MichaelPetch, the byte size must be including with the .fill statement:

x:.fill 10, 4
Ajax1234
  • 69,937
  • 8
  • 61
  • 102