在AVR组件中添加数字 [英] Add numbers in AVR assembly

查看:51
本文介绍了在AVR组件中添加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在AVR组装中执行此操作?

How can I do this in AVR assembly?

我在不同的规则中有2个数字(小尾数法).

I have 2 numbers (Little endian) in different reg.

# Number 1
LDI R16 0x…
LDI R17 0x…
LDI R18 0x…
LDI R19 0x…
# Number 2
LDI R20 0x…
LDI R21 0x…
LDI R22 0x…
LDI R23 0x…

我想将它们加在一起并将结果保存到R20-R23.

I want to add them together and save the result to R20 - R23.

推荐答案

关于其背后的数学":与十进制系统相同:

Regarding the "math" behind it: It's just the same as in the decimal system:

在添加两个个位数时,必须考虑两种情况.两者之和是一个新的单个数字(5 + 4 = 9),或者发生溢出",并且需要另一个数字(5 + 6 = 11).请注意,对于n位长度的任何两个数字(以10为基数,无论是10、2、256还是其他数),这两个数之和总是 ,小于其最低数的两倍. n+1位数字长度;假设ij是例如长度1的数字(以10为底),也就是说,两者都在09之间(包括两端).由于10是长度n+1 = 2的最小数目,因此它们的总和将始终小于2 x 10.

When adding two single digit numbers, two cases have to be considered. Either the sum of the two is a new single digit number (5+4 = 9), or an 'overflow' occurs and another digit is required (5+6 = 11). Note that for any two numbers (in any base, be it 10, 2, 256 or whatever) of n digits length the sum of the two will always be lower than two times the lowest number of n+1 digits length; let i and j be numbers (base 10) of, e.g., length 1, that is, both are between 0 and 9, inclusive. As 10 is the lowest number of length n+1 = 2, their sum will always be lower than 2 x 10.

当将两个数字相加时,可能没有溢出或可能恰好是1的溢出.进位位存储了上一次算术运算中的溢出;它是0或1.

When adding two numbers, there may be no overflow or there may be an overflow of exactly 1. The carry bit stores this overflow from the last arithmetic operation; it's either 0 or 1.

因此,当您将两个4x 8位的数字相加时(可以看作4个数字"的基数256),第一次加法将没有溢出要考虑,因此只有ADDADD可以视为x = x + y + 0的操作.但是,在第一次添加之后,可能需要考虑溢出,这可以通过使用ADDC完成; ADDC表示x = x + y + carry的操作,如上所述,其中carry只能是0或1.在将所有数字相加之后,最后的相加可能再次引起了溢出,该溢出将在以后的进位位中反映出来,并且可以被评估为对数字范围的溢出做出反应,例如:

So, when adding your two numbers of 4x 8 bit each (which can be seen as 4 'digits' base 256), there will be no overflow to be considered for the first addition, hence only ADD; ADD can be regarded as an operation for x = x + y + 0. After the first addition however there may be an overflow which needs to be taken into account, which is done by using ADDC; ADDC represents the operation of x = x + y + carry, where carry can only be 0 or 1 as mentioned above. After all digits have been added, the last addition may have caused an overflow again which will be reflected in the carry bit afterwards and can be evaluated to possibly react to the overflow of the numberrange, like:

x = x + y;

if ( carry == 1 ) {
  error "The sum is too big for the datatype of x";
}

这篇关于在AVR组件中添加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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