请给我一个非常简单的例子来创建一个函数并在 x86 汇编(AT&T 语法)中调用它。实际上,我正在尝试创建一个计算factorial
数字的函数。这就是我所做的一切:
#include<syscall.h>
#include<asm/unistd.h>
# Calculates Factorial, Argument is passed through stack
.text
.global _start
_start:
pushl $5 #factorial of this value will be calculated
call Fact
movl %eax, %ebx #eax contains the result, Result is the return val of the program
movl $1, %eax
int $0x80
ret
Fact:
popl %ebx #Return address
popl %edx
movl $1, %ecx #Will be used as a counter
movl $1, %eax #Result(Partial & complete) will be stored here
LOOP:
mul %ecx
inc %ecx
cmp %ecx, %edx
jle LOOP
pushl %ebx #Restore the return address
ret
我Segmentation Fault
一次又一次地收到错误。我正在使用GAS。Ubuntu