【发布时间】:2012-03-21 01:32:17
【问题描述】:
我有一个任务,我们必须编写两个函数。还必须使用处理器的条件代码检测溢出条件并返回0 以指示遇到错误。我能够编写函数。
.file "formula.c"
.text
.globl _nCr
.def _nCr; .scl 2; .type 32; .endef
_nCr:
pushl %ebp
movl %esp, %ebp
subl $56, %esp
movl 8(%ebp), %eax
movl %eax, (%esp)
testl %eax, %eax
call _factorial
movl %eax, -12(%ebp)
movl 12(%ebp), %eax
addl $1, %eax
movl %eax, (%esp)
call _factorial
movl %eax, -16(%ebp)
movl 12(%ebp), %eax
notl %eax
addl 8(%ebp), %eax
movl %eax, (%esp)
call _factorial
movl %eax, -20(%ebp)
movl -16(%ebp), %eax
movl %eax, %edx
imull -20(%ebp), %edx
movl %edx, -28(%ebp)
movl -12(%ebp), %eax
movl %eax, %edx
sarl $31, %edx
idivl -28(%ebp)
leave
ret
.globl _factorial
.def _factorial; .scl 2; .type 32; .endef
_factorial:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $1, -8(%ebp)
movl $1, -4(%ebp)
jmp L3
L4:
movl -8(%ebp), %eax
imull -4(%ebp), %eax
movl %eax, -8(%ebp)
addl $1, -4(%ebp)
L3:
movl -4(%ebp), %eax
cmpl 8(%ebp), %eax
jle L4
movl -8(%ebp), %eax
leave
ret
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.align 4
这个函数基本上是做nCr = n! / (r! (n-r)!)的。当数字变大时,会在阶乘中发生溢出。
我只是不明白如何设置溢出条件。
【问题讨论】:
-
溢出条件由算术指令自动设置。你只需要知道如何阅读它们。
-
感谢您的回复。我应该放入自己的状态标志。我只是不知道如何放入自己的条件标志。
-
根据问题和您的评论看来这是作业,所以我添加了作业标签。如果这不是家庭作业,请随意删除标签。
-
@user1282285 您可以通过执行所需的操作来设置条件标志。还是我错过了什么?
-
如果您自己编写和理解程序集,而不是使用 GCC 生成的程序集,您可能会更轻松。
标签: c assembly x86 integer-overflow