【发布时间】:2015-05-09 18:18:52
【问题描述】:
Code1:实现调用fact(阶乘)函数的main函数
section .data
msg db "Enter the Number whose factorial is to be calculated",10,0
msg1 db "The factorial is: ",0
ioput db "%d"
section .bss
a resd 1
section .text
global main
extern printf,scanf,fact
main:
pusha
push msg
call printf
add esp,4
popa
pusha
push a
push ioput
call scanf
add esp,8
popa
mov ebx,dword[a]
pusha
push ebx
call fact
add esp,4
pusha
push msg1
call printf
add esp,4
popa
pusha
push eax
push ioput
call printf
add esp,8
popa
ret
实现事实(阶乘函数)的代码2:
section .text
global fact
extern printf,scanf
fact:
enter 0,0
cmp ebx,1
jnz next
ret
next:
push ebx
dec ebx
call fact
pop ebx
mul eax,ebx
leave
ret
系统统计:32 位机器,Ubuntu 14.04,使用 Nasm
问题说明:程序收到信号SIGILL,指令非法。为什么会出现此错误?
【问题讨论】:
-
pusha和popa甚至不平衡。检查包含call fact的块。以这种方式返回会去一个有趣的地方。 -
另外,学习使用调试器。