博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[Assembly] int type in memory

Posted on 2010-02-20 19:00  xuczhang  阅读(132)  评论(0编辑  收藏  举报

Here is a sample code to view the memory of an int type.

1 #include
2  int main (void)
3 {
4    int x = -3;
5   unsigned char const *p = (unsigned char const *) & x;
6   size_t i;
7   for (i=0; i < sizeof x; i++)
8   {
9     printf("%02X ", p[i]);
10   }
11   printf ("\n");
12   return 0;
13 }
14

 

 

The output is: FD FF FF FF (my machine is 32bits x86)
As the output is little endian, so the number should be FF FF FF FD
binary format is:11111111 11111111 11111111 11111101
complemental code: 10000000 00000000 00000000 00000011(=-3)
So the consequence is the int type stores in computer as complemental code.