AVR Assembly Addition溢出

时间:2014-09-18 03:36:47

标签: assembly arduino avr

我知道这是一个新手问题,但无论如何我会问它,因为我无法找到答案。这是我正在查看的代码。

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. 

我基本上试图找到SREG标志的内容,但由于缺乏对AVR的理解而无法做到。请注意,我现在没有微控制器,如果不是,我只是测试找到指定的值。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果您没有实际的芯片,您仍然可以使用模拟器。 或者,horribile dictu,阅读手册。它具有所有SREG位的公式。它说:

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.

因此,H=0S=1V=0N=1Z=0C=0。这意味着,如果使用无符号算术,则没有溢出(C=0),结果(0x8a = 138)有效。然而,签名溢出确实发生(S=1),因为0x8a签名意味着-118

相关问题