I’m puzzled by this problem when writting an ARM assembly simulator in C. I’ve found some similar questions in the forum, but none of them explain how to set the carry flag just using the relationship between two operands and the result.
Any reply is appreciated. Thanks in advance.
Regard.
You have to check the reference manual for the processor to know which instructions set the carry flags and in which way. I don’t know enough about ARM, but I’ve seen some variations in other processors:
some instructions which logically generates a carry may not set the carry flag
some instructions may use the carry flag with as some additional implicit operand or result without having a connection with addition/subtraction
after a subtraction, processors vary in which condition the carry flag is set (i.e some do it in the same way as after the addition of an inversed second operand, the other set it to the negation of that)
If what you want is a way to see if a carry should be generated for an addition in C, here are two ways (the first is straight from definition, the second comes from wrap around behavior of unsigned):