如何在汇编语言X86中检测溢出条件 [英] How to detect overflow conditions in Assembly Language X86
问题描述
我有一个作业,其中我们必须编写两个函数.还必须使用处理器的条件代码检测溢出条件,并返回0
表示已遇到错误.我能够编写函数.
I have an assignment in which we have to write two functions. Also must detect overflow conditions using the processor's condition codes and return 0
to indicate that an error has been encountered. I was able to write the functions.
.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)!)
.当数字变大时,阶乘会发生溢出.
This function basically does nCr = n! / (r! (n-r)!)
. The overflow occurs in factorial when the numbers get larger.
我只是不明白如何设置溢出条件.
I just do not understand how I would set the overflow conditions.
推荐答案
在x86架构上,当执行诸如addl 8(%ebp), %eax
之类的算术指令时,条件代码将设置在CPU状态字中.有些指令的行为取决于条件代码.
On the x86 architecture, when an arithmetic instruction executes such as addl 8(%ebp), %eax
the condition codes are set in the CPU status word. There are instructions whose behavior depends on condition codes.
您可以让代码在给定条件下采用替代路径(执行分支). x86在Jxx
助记符下提供了一系列条件分支指令:JA, JAE, JB, JBE, JC, JCXZ, ..., JZ
.例如,JZ
表示如果为零则跳转:如果指令产生的结果为零,则转移一个分支,并设置零标志. JO
在溢出时跳转.
You can have the code take an alternate path (execute a branch) on a given condition. The x86 has a family of conditional branching instructions under the Jxx
mnemonics: JA, JAE, JB, JBE, JC, JCXZ, ..., JZ
. For instance JZ
means jump if zero: take a branch if the instruction produced a zero result, setting the zero flag. JO
is jump on overflow.
条件也可以转换为字节数据并存储到寄存器或存储器中.这对于编译如下C表达式很有用:
A condition can also be converted to a byte datum and stored into a register or memory. This is useful for compiling C expressions like:
x = (y != 3); /* if (y != 3) x = 1; else x = 0 */
这是由SETx
组指令完成的,该组指令也很多,如条件分支:SETA, SETAE, SETB, ..., SETZ
.例如,如果零条件为真,则SETZ将给定字节设置为1.例如
It is done by the SETx
group of instructions which are also numerous, like the conditional branches: SETA, SETAE, SETB, ..., SETZ
. For instance SETZ will set a given byte to 1 if the zero condition is true. E.g.
seto %bl /* set bottom byte of B register to 1 if overflow flag set */
这篇关于如何在汇编语言X86中检测溢出条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!