问题
How can I do this in AVR assembly?
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…
I want to add them together and save the result to R20 - R23.
回答1:
Regarding the "math" behind it: It's just the same as in the decimal system:
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
.
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.
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";
}
回答2:
Pretty simple operation. Use add for the first operation, and add-with-carry for subsequent additions
# 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…
# Add LSB of 1 and 2, result will be in R20
ADD R20,R16
# Add remaining bytes using the add-with-carry operation
ADC R21,R17
ADC R22,R18
ADC R23,R19 # MSB
The result will overwrite the value in R20:R23.
I know you were just loading constants into the registers as an example, but don't forget that you can add constants using the subi and sbci opcodes. For example, to add 5 to R18:R19:
SUBI R18,-5
SBCI R19,-1 # This isn't intuitive, but needs to be -1, not zero
To subtract 5 from R18:R19:
SUBI R18,5
SBCI R19,0
来源:https://stackoverflow.com/questions/10879819/add-numbers-in-avr-assembly