0
votes

I know this is a newbie question, but I will ask it anyway because I couldn't find the answer. Here is the code I'm looking at.

LDI R15, 0x72     ;R15=114
LDI R16, 0x18     ;R16=24
ADD R16, R15      ;I know this causes signed overflow, but I'm not sure how avr handles this 
                  ;or if this number is unsigned

LDI R17, 0x91     ;R17=-111 if this is a signed number, which I assume it is. 
ADD R17, R16      ;no idea what the value is because of previous unkowns. 

I am basically trying to find what the SREG flags will be for this, but cannot due to the lack of understanding of the AVR. Please note that I don't have a microcontroller currently with me if not I would simply test to find the specified values.

Thanks for the help!

1

1 Answers

0
votes

If you don't have an actual chip, you can still use a simulator. Or, horribile dictu, read the manual. It has the formulas for all the SREG bits. It says:

H: Rd3·Rr3+Rr3·!R3+!R3·Rd3
   Set if there was a carry from bit 3; cleared otherwise
S: N ^ V, For signed tests.
V: Rd7·Rr7·!R7+!Rd7·!Rr7·R7
   Set if two's complement overflow resulted from the operation; cleared otherwise.
N: R7
   Set if MSB of the result is set; cleared otherwise.
Z: !R7· !R6 ·!R5· !R4 ·!R3 ·!R2 ·!R1 ·!R0
   Set if the result is $00; cleared otherwise.
C: Rd7 ·Rr7 +Rr7 ·!R7+ !R7 ·Rd7
   Set if there was carry from the MSB of the result; cleared otherwise.

As such, H=0, S=1, V=0, N=1, Z=0 and C=0. This means, if you are using unsigned arithmetic you had no overflow (C=0), the result (0x8a = 138) is valid. Signed overflow did occur however (S=1), since 0x8a signed means -118.