BCD加法汇编程序逻辑

时间:2017-02-07 09:42:47

标签: assembly x86

我试图理解下面的x86转储汇编程序片段的添加程序,其中添加了两个数字6789和1234,问题是该程序如何处理进位。

我知道当结果大于9时添加6,但是这个程序有很多步骤对我来说没什么意义。

auth.json

2 个答案:

答案 0 :(得分:3)

add ecx, F6F6F6F6

这使得进位通过添加6而在> 9的数字之外传播,并且通过添加F 10 + 6传递孔将导致16或更多,因此执行此操作蚕食下一个蚕食。 F加上进位使进位传播到下一个半字节,并在进位经过时留下0。

and eax, 60606060

这样就会产生一个掩膜,每个孔都有一个6,而这个孔没有穿过。

sub ecx, eax

修复在第一步中没有进位的数字。他们之前添加了6个,但没有包裹,所以他们在[6..F]的某个地方,现在又从他们中减去了6个。

答案 1 :(得分:0)

下面我提出我自己的问题解释,如果我错了,请纠正我

add     ecx,eax 
            //  ecx value after addition 07 09 0B 0D (this is simple Binary addition result , now need to convert it to BCD addition)

add     ecx,F6F6F6F6
               // after addition FE 00 02 03        // adding 6 to each digit to correct it , its is greater or equal to 9 
                                // the position where zoned bits becomes zero that means its a correction , but digits where zoned bits are not zero that means no correction is needed there ,
                                //and there is excess 6 due to additon of f6 , now need to subtract the 6 from the number 

mov     eax,ecx            // eax = ecx = FE 00 02 03
and     eax,60606060..............// eax = 60 00 00 00 // the position where excess 6 is there , is identified by this step 

shr     eax,04 ...................// eax = 06 00 00 00  // we can correct it by shifing the 6 adjacent to digit where is excess 6 


and     ecx,0F0F0F0F..............// FE 00 02 03 & 0F 0F 0F 0F = 0E 00 02 03(ECX)        // proper format , masking irrevelant bits of ecx


sub     ecx,eax ..................// 0E 00 02 03 - 06 00 00 00 = 08 00 02 03               // 8023 ans       // subtracting excess 6