GAS组装程序分段错误(写入自动变量) [英] GAS Assembly program segmentation fault (writing to auto variable)

查看:101
本文介绍了GAS组装程序分段错误(写入自动变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在C语言中完成此操作:

I intend to do this in C:

#include<stdio.h>
int main() {
  int arr[5];
  arr[0] = 5;
  arr[1] = 0;
  arr[2] = 1;
  arr[3] = 3;
  arr[4] = 4;
  int max = 0;
  for(int i = 0;i < 5;i++)
    if(max < arr[i])
      max = arr[i];
  printf("%d\n", max);
  return 0;
}

这是我的代码链接: array_max.s .这是我的AT& T格式的汇编代码:

This is my code link: array_max.s. This is my assembly code in AT&T format :

.data

.text
.globl _start
_start:
  movl $5, -20(%ebp)
  movl $0, -16(%ebp)
  movl $1, -12(%ebp)
  movl $3, -8(%ebp)
  movl $4, -4(%ebp)
  movl $0, %ecx
  movl $5, %eax
  loop:
    cmp $0, %eax
    je terminate
    cmp %ecx, -20(%ebp,%eax,4)
    jg assign
    jmp loop


terminate:
  movl $4, %eax
  movl $1, %ebx
  movl $1, %edx
  int $0x80
  movl $1, %eax
  int $0x80
  ret

assign:
  movl -20(%ebp,%eax,4), %ecx
  ret

我在第一个指令movl $5, -20(%ebp)上出现段错误.我是新手,请帮忙.

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

推荐答案

我在第一个指令movl $5, -20(%ebp)上出现段错误.

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

您没有在堆栈上为arr分配任何空间(即:int arr[5]):

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

为了通过恢复先前的堆栈帧来释放分配给堆栈的内存,请在ret之前使用leave指令.

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

这篇关于GAS组装程序分段错误(写入自动变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆