【发布时间】:2015-11-21 19:22:27
【问题描述】:
我有这个汇编代码(Linux 32Bit 用 gcc -m32 编译),我真的不明白为什么我的程序不工作。
.data
.bla:
.ascii "%d\n\0"
.globl main
.text
main: pushl $4
call factorial
movl $1, %eax
ret
factorial:
push %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx
cmpl $1, %ecx
jg .rek
movl $1, %eax
movl %ebp, %esp
pop %ebp
ret
.rek:
decl %ecx
pushl %ecx
call factorial
addl $4, %esp
movl 8(%ebp), %ecx
imull %ecx, %eax
pushl %eax
pushl $.bla
call printf
addl $8, %esp
movl %ebp, %esp
pop %ebp
ret
不幸的是,每次确实发生分段错误 + 大于 4 的参数都不起作用。
当我用“3”运行程序时,这应该是我的堆栈:
3
ret add
ebp
2
ret add
ebp
1
ret add
ebp
当我到达递归的底部时,我将保存在 eax 中的返回值与 8(%ebp) 相乘,这应该是下一个值。
非常感谢您提供的任何帮助。
【问题讨论】:
-
考虑到尝试调用名为
maine的不存在例程,我看不出上面的代码是如何构建的。 -
谢谢,发在这里之前忘记改了。但问题仍然存在。
-
@Noahnder 看到我的回答。