GAS Assembly program segmentation fault (writing to auto variable)

前端 未结 1 2020
無奈伤痛
無奈伤痛 2021-01-20 06:14

I intend to do this in C:

#include
int main() {
  int arr[5];
  arr[0] = 5;
  arr[1] = 0;
  arr[2] = 1;
  arr[3] = 3;
  arr[4] = 4;
  int max         


        
相关标签:
1条回答
  • 2021-01-20 06:33

    I am having a segmentation fault on the very first instruction movl $5, -20(%ebp).

    You are not allocating any space on the stack for arr (i.e.: int arr[5]):

    pushl %ebp
    movl %esp, %ebp
    subl $20,%esp //<--- memory allocation
    

    In order to deallocate the memory allocated on the stack by restoring the previous stack frame, use the leave instruction just before ret.

    0 讨论(0)
提交回复
热议问题