sbb指令和进位标志的使用

发布于 2025-01-09 07:28:14 字数 1429 浏览 0 评论 0原文

我正在研究组装测试中应用的一个问题,但在确定代码实际执行的操作时遇到问题。我将在下面写下我认为它所做的事情。

我完全不明白某人在做什么。我的猜测是它是用来欺骗的。我这么认为,因为进位标志永远不会从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2025-01-16 07:28:14

这看起来像是一个将参数 P1P2 视为指向 BigNums 长度 N 并计算它们的差异,将其存储回P2。无论 bignum 减法是否导致借位,它最终都会返回返回值的低 8 位。

在循环之前,clc 指令将进位标志设置为 0。循环体由三个语句 lodslsbblstosl 组成。每次循环时,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 and P2 as pointers to BigNums of length N and computes their difference, storing it back into P2. 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 statements lodsl, sbbl, and stosl. Each time through the loop, the lodsl instruction loads a word from the memory pointed to by esi into eax. The sbbl instruction then subtracts from eax the word from the memory pointed to by edi and also the contents of the carry bit. The first time through the loop, the carry bit will always be 0, so the sbbl works just like a subl would. But the sbbl 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 the sbbl.

The loop is iterated N times, each sbbl using the C flag set by the previous iteration. The net effect is of subtracting two N-word bignums.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文