/*------------------------------------------------------------------------
N位压缩BCD加法(ARM小端算法)---雁舞白沙出题
此题目的讨论见 http://bbs.21ic.com/club/bbs/ShowAnnounce.asp?id=2464366
HotPower@126.com 2007.3.13 于西安大雁塔菜地
------------------------------------------------------------------------*/
static unsigned char databuf[12];//32位BCD加法缓冲区
unsigned char DataAdd(unsigned char *,unsigned char *,unsigned int);
int main()
{
//32位BCD加法(12345678 + 23454543 = 35800221)
(unsigned int)databuf[4] = 0x12345678;
(unsigned int)databuf[8] = 0x23454543;
DataAdd(&databuf[4], &databuf[8], 4);//结果在val和databuf[0]~databuf[3]中
while(1);
}
unsigned char DataAdd(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned int DataLend)
{
unsigned char One, Two, Tmp;//中间变量
unsigned int i, Sum = 0;//考虑CY位故取整型
for (i = 0; i < DataLend; i ++) {
One = DataOneDptr;//取出正确的被加数
Two = DataTwoDptr;//取出正确的加数
Sum = One + Two + Sum;//二进制求和(注意上次低位向高位的进位)
Tmp = (One & 0xf0) + (Two & 0xf0);//为半进位做准备
if ((Tmp != (Sum & 0xf0)) || ((Sum & 0x0f) > 9)) {//BCD码低4位调整
Sum += 6;//凡加减10或9的都是"混球"~~~
}
if (Sum >= 0xa0) {//BCD码高4位调整
Sum += 0x60;//注意"混球"~~~
}
databuf = Sum;//只存入低8位
Sum >>= 8;//保留高8位做为下次低位向高位的进位
}
return Sum;//返回溢出标志(最高1位)
}
文章评论(0条评论)
登录后参与讨论