在汇编中如何为未签名的int分配负数? [英] How does in assembly does assigning negative number to an unsigned int work?

查看:114
本文介绍了在汇编中如何为未签名的int分配负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了有关2's Complement的信息,并对其进行了未签名和已签名的int运算.因此,我决定测试我的知识,据我所知,负数是以2's complement的方式存储的,因此加法和减法不会有不同的算法,并且电路也很简单.

I Learned About 2's Complement and unsigned and signed int. So I Decided to test my knowledge , as far as i know that a negative number is stored in 2's complement way so that addition and subtraction would not have different algorithm and circuitry would be simple.

现在如果我写

int main()
{
  int a = -1 ;
  unsigned int b = - 1 ;

  printf("%d %u \n %d %u" , a ,a , b, b);
}

输出变为-1 4294967295 -1 4294967295.现在,我查看了位模式和各种东西,然后我意识到2的补码中的-111111111 11111111 11111111 11111111,所以当我使用%d解释它时,它给出了-1,但是当我使用%u解释时,它将其视为正数,因此给出4294967295.我检查了汇编的代码是

Output Comes To Be -1 4294967295 -1 4294967295 . Now , i looked at the bit pattern and various things and then i realized that -1 in 2's complement is 11111111 11111111 11111111 11111111 , so when i interpret it using %d , it gives -1 , but when i interpret using %u , it treat it as a positive number and so it gives 4294967295. I Checked the Assembly Of the code is

.LC0:
    .string "%d %u \n %d %u"
main:
    push    rbp
    mov     rbp, rsp
    sub     rsp, 16
    mov     DWORD PTR [rbp-4], -1
    mov     DWORD PTR [rbp-8], -1
    mov     esi, DWORD PTR [rbp-8]
    mov     ecx, DWORD PTR [rbp-8]
    mov     edx, DWORD PTR [rbp-4]
    mov     eax, DWORD PTR [rbp-4]
    mov     r8d, esi
    mov     esi, eax
    mov     edi, OFFSET FLAT:.LC0
    mov     eax, 0
    call    printf
    mov     eax, 0
    leave
    ret

现在,这里-1都以unsigned和signed两次移动到寄存器中.我想知道的是,仅重新解释很重要,那么为什么我们有两种类型的unsignedsigned呢?printf格式字符串%d%u很重要?

Now here -1 is moved to the register both the times in unsigned and signed . What i want to know if reinterpretation is only that matters , then why do we have two types unsigned and signed , it is printf format string %d and %u that matters ?

当我给一个无符号整数赋负数时,进一步发生了什么(我得知初始化程序将此值从int转换为unsigned int.),但是在汇编代码中,我没有看到这样的事情.那么到底发生了什么??

Further what really happens when i assign negative number to a unsigned integer (I learned That The initializer converts this value from int to unsigned int. ) but in the assembly code I did not saw such a thing. So what really happens ??

并且机器如何知道何时必须执行2's complement,何时不知道,它是否看到负号并执行2's complement?

And How does Machine knows when it has to do 2's complement and when not , does it see the negative sign and performs 2's complement?

我已经阅读了几乎所有问题,并回答了您可能认为该问题与重复的情况,但是我找不到令人满意的解决方案.

I have read almost every question and answer you could think this question be duplicate of , but I could not find a satisfactory solution.

推荐答案

有符号的和无符号的都是内存,根据操作的不同,内存的行为也很重要.

Both signed and unsigned are pieces of memory and according to operations it matters how they behave.

加法或减法没有什么区别,因为由于2补码,所以运算完全相同.

It doesn't make any difference when adding or subtracting because due to 2-complement the operations are exactly the same.

当我们比较两个数字时很重要:-1小于0,而4294967295显然不是.

It matters when we compare two numbers: -1 is lower than 0 while 4294967295 obviously isn't.

关于转换-对于相同的大小,它只是简单地获取可变内容并将其移动到另一个-因此4294967295变为-1.对于更大的尺寸,首先对其进行签名扩展,然后再进行内容移动.

About conversion - for the same size it simply takes variable content and moves it to another - so 4294967295 becomes -1. For bigger size it's first signed extended and then content is moves.

现在如何加工-根据我们使用的说明.机器有不同的指令来比较有符号和无符号,或者为它们提供不同的标志(x86具有进行无符号溢出"和溢出用于有符号溢出").

How does machine now - according the instruction we use. Machines have either different instructions for comparing signed and unsigned or they provide different flags for it (x86 has Carry for unsigned overflow and Overflow for signed overflow).

此外,请注意,C放松了如何存储带符号的数字,它们不必是2的补码.但是如今,所有常见的体系结构都像这样存储签名.

Additionally, note that C is relaxed how the signed numbers are stored, they don't have to be 2-complements. But nowadays, all common architectures store the signed like this.

这篇关于在汇编中如何为未签名的int分配负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆