2017-02-07 8 views
1

2つの数字6789と1234が追加されているx86ダンプからアセンブリプログラムのスニペットを理解しようとしていますが、問題はこのプログラムがどのように処理するかです。BCD追加アセンブリプログラムロジック

結果が9より大きい場合は6を追加することがわかりますが、このプログラムは多くのステップがあり、ほとんど意味がありません。

and  ecx,0F0F0F0F   
       // masking zoned bits -> 06 07 08 09 
and  eax,0F0F0F0F    
      // masking zoned bits -> 01 02 03 04 
add  ecx,eax   
      // ecx value after addition 07 09 0B 0D (this is simple Binary add result,now need to convert it to BCD addition) 

add  ecx,F6F6F6F6 
      // after addition FE 00 02 03 

mov  eax,ecx     
      // eax = ecx = FE 00 02 03 
and  eax,60606060 
      // eax = 60 00 00 00 
shr  eax,04 
      // eax = 06 00 00 00 
and  ecx,0F0F0F0F 
      // FE 00 02 03 & 0F 0F 0F 0F = 0E 00 02 03(ECX) 
sub  ecx,eax 
      // 0E 00 02 03 - 06 00 00 00 = 08 00 02 03    // 8023 ans 

答えて

3
add ecx, F6F6F6F6 

これは6を添加し、16以上になりF. 10 + 6を追加することによって、穴を通過させることにより> 9である数字のうち、伝搬運ぶことができるので、キャリーがありますこのニブルから次のニブルへ。 F carryはキャリーを次のニブルに伝播させ、キャリーが通過した場所を0にします。

and eax, 60606060 

これは、キャリーが通過しなかったホールごとに6個のマスクを生成します。

sub ecx, eax 

最初のステップでキャリーアウトしなかった桁を修正します。彼らは前に6を追加しましたが、折り返さずに[6..F]のどこかにいるので、6が再び差し引かれます。私は質問の私自身、について説明を提示しています

+0

ていた場合、私を修正してください?可能であれば、Plzのヘルプ、詳細は – user143252

+1

@ user143252あなたが実際に働いているBCD数字の間の場所、たとえば01020304ではすべてゼロです。私はそれが意味をなさないと思ったので私はちょうど穴と呼ばれました、私が知る限り実際には実際の用語ではありません。 – harold

+0

キャリーの管理方法をバイナリ形式で記述することができます – user143252

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 
関連する問題