sbb指令和进位标志的使用
我正在研究组装测试中应用的一个问题,但在确定代码实际执行的操作时遇到问题。我将在下面写下我认为它所做的事情。
我完全不明白某人在做什么。我的猜测是它是用来欺骗的。我这么认为,因为进位标志永远不会从 0 开始改变。我错了?在循环之前进位标志为零,并且循环内没有任何内容改变它。我错过了什么吗?
C 中函数的签名是:
char FX (unsigned int N, unsigned int * P1, unsigned int * P2);
汇编代码(使用 AT&T 格式)和我的注释是:
FX: pushl %ebp ; stacks ebp
movl %esp, %ebp ; move esp to ebp
pushl %esi ; stacks esi
pushl %edi ; stacks edi
movl 8(%ebp),%ecx ; N
movl 12(%ebp),%esi ; *P1
movl 16(%ebp),%edi ; *P2
cld ; Clear Direction Flag DF = 0
clc ; Clear Carry Flag CF = 0
L1: lodsl ; Load String gets ESI - > EAX = *P1 e P1++(because DF =0)
sbbl (%edi),%eax ; eax = eax - (edi + CF) *P1 = *P1 - (*P2 - 0)
stosl ; Store String saves EAX into EDI *P2 = eax e P2++
loop L1 ; N-- and loops L1 while N > 0
movb $0,%AL ; Clear least significant 2 bytes from EAX without altering flags
adcb %AL,%AL ; AL = AL + AL + CF
popl %edi ; restore edi
popl %esi ; restore esi
popl %ebp ; restore ebp
ret ; return eax
我认为此代码仅将内容从 P1 中开始的向量复制到 P2 中的另一个向量,但无法理解为什么我需要 sbb、adc 指令,以及为什么需要担心进位标志,因为没有减法或加法。
感谢您的帮助!
I'm studying a question applied in an assembly test and i´m having problems determining what the code is actualy doing. I'll put below what i think it is doind.
I don't understand exatly what the sbb is doing. My guess is that it´s there to trick. I think this, because the Carry Flag is never changed from 0. I´m i wrong? The carry flag is zero before de loop and nothing inside the loop change it. I´m a missing something?
The signature of the function in C is:
char FX (unsigned int N, unsigned int * P1, unsigned int * P2);
And the assembly code (using AT&T format) with my comments is:
FX: pushl %ebp ; stacks ebp
movl %esp, %ebp ; move esp to ebp
pushl %esi ; stacks esi
pushl %edi ; stacks edi
movl 8(%ebp),%ecx ; N
movl 12(%ebp),%esi ; *P1
movl 16(%ebp),%edi ; *P2
cld ; Clear Direction Flag DF = 0
clc ; Clear Carry Flag CF = 0
L1: lodsl ; Load String gets ESI - > EAX = *P1 e P1++(because DF =0)
sbbl (%edi),%eax ; eax = eax - (edi + CF) *P1 = *P1 - (*P2 - 0)
stosl ; Store String saves EAX into EDI *P2 = eax e P2++
loop L1 ; N-- and loops L1 while N > 0
movb $0,%AL ; Clear least significant 2 bytes from EAX without altering flags
adcb %AL,%AL ; AL = AL + AL + CF
popl %edi ; restore edi
popl %esi ; restore esi
popl %ebp ; restore ebp
ret ; return eax
I think this code only copies content from a vector starting in P1 to another vector in P2, but and can't undertand why do i need the sbb, adc instructions, and why a need to worry with carry flag since there is no subtractions or additions.
Thanks for the help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像是一个将参数
P1
和P2
视为指向 BigNums 长度N
并计算它们的差异,将其存储回P2
。无论 bignum 减法是否导致借位,它最终都会返回返回值的低 8 位。在循环之前,
clc
指令将进位标志设置为 0。循环体由三个语句lodsl
、sbbl
和stosl
组成。每次循环时,lodsl
指令都会将esi
指向的内存中的一个字加载到eax
中。然后,sbbl
指令从eax
中减去edi
指向的存储器中的字以及进位位的内容。第一次循环时,进位位始终为 0,因此sbbl
的工作方式就像subl
一样。但是sbbl
也可以将进位位设置为减法的结果。下一次循环时,进位位的值将在 sbbl 中使用。该循环迭代
N
次,每次sbbl
使用前一次迭代设置的 C 标志。最终效果是减去两个 N 字 bignum。This looks like a function that treats the arguments
P1
andP2
as pointers to BigNums of lengthN
and computes their difference, storing it back intoP2
. It finally returns in the low 8 bits of the return value whether or not the bignum subtraction resulted in a borrow.Before the loop, the carry flag is set to 0 by the
clc
instruction. The body of the loop consists of three statementslodsl
,sbbl
, andstosl
. Each time through the loop, thelodsl
instruction loads a word from the memory pointed to byesi
intoeax
. Thesbbl
instruction then subtracts fromeax
the word from the memory pointed to byedi
and also the contents of the carry bit. The first time through the loop, the carry bit will always be 0, so thesbbl
works just like asubl
would. But thesbbl
may also set the carry bit as a result of the subraction. Next time around the loop, that value of the carry bit is used in thesbbl
.The loop is iterated
N
times, eachsbbl
using the C flag set by the previous iteration. The net effect is of subtracting two N-word bignums.