[英]Signed unsigned, avr assembly
I have a few questions about instructions in asm 我对asm中的指令有一些疑问
1) add Rd, Rr; 1) 添加Rd,Rr; Are Rd and Rr unsigned or signed numbers? Rd和Rr是未签名或已签名的数字吗?
2) How to load signed numer in register? 2)如何在寄存器中加载带符号的数字? i know that LDI Rd,K is loading unisgned. 我知道LDI Rd,K正在加载。
3) Can you provide me with concrete example with adding two 16bit numbers, and is it poisable that one number is negative? 3)您能提供两个16位数字相加的具体例子吗,一个数字是否为负可能吗?
The nice thing about 2's complement arithmetic is that addition and subtraction work the same no matter whether the operands are signed or not. 关于2的补码算法的好处是,无论操作数是否带符号,加法和减法的工作原理都相同。 It's up to the programmer to interpret the operands and the result appropriately. 由程序员来适当地解释操作数和结果。 Detecting overflow is different though. 但是,检测溢出是不同的。
Let's add 12345
and -10
. 让我们添加12345
和-10
。 12345
is 0x3039
and -10
is 0xfff6
. 12345
是0x3039
, -10
是0xfff6
。 We'll just add the low bytes first, then the high bytes with the carry from the first operation. 我们将首先添加低字节,然后将高字节与第一个操作的进位相加。
LDI R16, 0x39 ; low byte of 12345
LDI R17, 0x30 ; high byte of 12345
LDI R18, 0xf6 ; low byte of -10
LDI R19, 0xff ; high byte of -10
ADD R16, R18 ; add low bytes without carry
; R16 is now 0x39 + 0xf6 = 0x2f and carry is 1
ADC R17, R19 ; add high bytes and the carry
; R17 is now 0x30 + 0xff + 0x01 = 0x30
So, the result is 0x302f
which is 12335
and is the expected value. 因此,结果是0x302f
,即12335
,是期望值。 Now, we could consider the initial -10
to be the unsigned number 65526
, the result would still be correct without any change, but in this case we'd have an overflow (wrap around). 现在,我们可以将初始-10
视为无符号数字65526
,结果将保持正确,而无需进行任何更改,但是在这种情况下,我们将发生溢出(环绕)。
You can read about 2's complement on wikipedia . 您可以在Wikipedia上阅读有关2的补码 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.