The Arm Architecture manual says for the ADC
instruction to set the C (carry) flag in the CPSR if the S-Flag is set and a carry "occured". From the book (page 155):
C Flag = CarrryFrom(Rn + shifer_operand + C Flag)
And according to the glossar the CarryFrom
is defined as follows:
CarryFrom
Returns 1 if the addition specified as its parameter caused a carry (true result is bigger
than 2^(32)−1, where the operands are treated as unsigned integers), and returns 0 in all other cases.
This delivers further information about an addition which occurred earlier in the pseudo-code. The addition is not repeated.
Now I'm wondering if the CarryForm operation is the same as an overflow check. Can anyone explain me, how I can "emulate" the CarryFrom
operation or how it works?
Simply binary addition, x is the carry in to the operation and y is the carry out. For a normal add carry in is a 0 and for a normal subtract carry in is a 1. (adders are used to do subtraction, one of the features of twos complement)
y x
1111
+ 0001
======
11110
1111
+ 0001
======
0000
So the result is 0000, the carry out is a 1. Some architectures (of all of them x86, arm, mips, pdp11, 6502, ...)(yep, I know about mips in this context) invert the carry out for a subtract and leave it not inverted for addition. In this case you are asking about ADC, so that is addition so it should not be modified by any architecture.
And 4 bits or 40 does not matter it all works the same.
So if you want to add 0x0F and 0x01 but you only have a 4 bit adder (again think 64 bits and 32 instead of 8 and 4, it all works the same).
We start with normal addition of the lower bits
11110
1111
+ 0001
======
0000
Then we do an add with carry and use the carry out of the prior addition as the carry in to the second (or next one since you can do this for as much code/memory space as you have)
1
0000
+ 0000
======
00001
0000
+ 0000
======
0001
And the end result is 0x10. 0x0F + 0x01 = 0x10
The first add here happens to have an unsigned overflow as indicated by a non-zero carry out/carry flag. If you focus only on that. If the adc also had an unsigned overflow then the whole result is bad as it won't fit in the number of bits. (if the programmer considered these to be unsigned values, if signed then you look at the V bit for overflow but the Carry still cascades from the first ADD to the ADC and then from each ADC to the next until you have covered the width of the higher level operation).