简体   繁体   English

已签名的未签名的AVR程序集

[英]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 . 123450x3039-100xfff6 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.

相关问题 具有无符号/有符号除法的汇编程序 - Assembly program with unsigned/signed division 识别汇编中的有符号和无符号值 - Identifying signed and unsigned values in assembly 汇编 x86 寄存器有符号或无符号 - Assembly x86 registers signed or unsigned c 编译器如何处理无符号和有符号整数? 为什么无符号和有符号算术运算的汇编代码相同? - how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same? 从有符号/无符号字符到无符号/有符号整数类型转换的 IA32 汇编代码 - IA32 Assembly code for type casting from signed/unsigned char to unsigned/signed int Howcome程序集没有签名和未签名的替换指令? - Howcome assembly does not have an signed and unsigned subraction instruction? 汇编代码如何知道值是带符号的还是无符号的? - How does assembly code know if a value is signed or unsigned? 汇编语言:使用十六进制的有符号与无符号整数表示 - Assembly Language: Signed vs Unsigned Integer Representation using Hexadecimal 如何在 x86 程序集中比较有符号值和无符号值 - How to compare a signed value and an unsigned value in x86 assembly 用arduino编程(AVR汇编) - programming with arduino (avr assembly)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM