BCD加法汇编程序逻辑 [英] BCD Addition assembly program logic

查看:83
本文介绍了BCD加法汇编程序逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to understand below snippet of assembly program from an x86 dump for addition program, where two numbers 6789 and 1234 is added, the problem is how this program deals with carries.

我知道在结果大于9时加6,但是该程序有太多步骤,对我来说意义不大.

I know adding 6, when result is greater than 9, but this program has so many steps that make little sense to me.

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

推荐答案

add ecx, F6F6F6F6

这使得进位传播,通过加6来> 9的数字之外,并通过加F来传递空位.10 + 6将导致16或更多,因此这个进位有一个进位蚕食.带有进位的F加号使进位传播到下一个半字节,并在进位经过的位置留下0.

This makes the carries propagate, out of the digits that are >9 by adding 6, and passing the holes by adding F. 10 + 6 will result in 16 or more, so there's a carry out of this nibble into the next nibble. F plus that carry makes the carry propagate into the next nibble, and leaves a 0 where the carry went through.

and eax, 60606060

这将产生一个掩膜,其中一个进位未通过的每个孔都包含一个6.

This produces a mask containing a 6 for every hole that a carry did not pass through.

sub ecx, eax

修复第一步中没有进位的数字.他们之前添加了6,但没有包装,因此它们位于[6..F]中的某个位置,现在又从它们中减去了6.

Fixes the digits that didn't have a carry-out in the first step. They had a 6 added to them before but didn't wrap so they're somewhere in [6..F], now that 6 is subtracted from them again.

这篇关于BCD加法汇编程序逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆