简体   繁体   English

内存地址和偏移量

[英]Memory address and offset

I have a noob questions about how memory address stores values. 我对内存地址如何存储值有一个新手问题。

For example, 例如,

addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F  +0123456789ABCDEF
0000  50 61 78 20 69 73 20 61 20 72 65 61 6C 6C 79 20   Pax is a really 
0010  63 6F 6F 6C 20 67 75 79 00                        cool guy.

Is 0000 an address? 地址是0000吗? Is 50 61 78 20 69 73 20 61 20 72 65 61 6C 6C 79 20 the value stored in 0000 address? 50 61 78 20 69 73 20 61 20 72 65 61 6C 6C 79 20是存储在0000地址中的值吗?

Here 0000 is an address, from the label addr . 0000是地址,来自标签addr The only value that is stored at 0000 is 50 . 存储在0000处的唯一值是50 Each byte has it's own address: 每个字节都有其自己的地址:

------+--------
 addr |  data
------+--------
 0000 | 50
 0001 | 61
 0002 | 78
 0003 | 20
 0004 | 69
 0005 | 73
 0006 | 20
 0007 | 61
 0008 | 20
 0009 | 72
 000A | 65
 000B | 61
 000C | 6C
 000D | 6C
 000E | 79
 000F | 20

if you considered for example 0000 as the base address, you can say 20 , the last value is stored at F as 0000 + F = 000F , base address + offset from that address. 例如,如果您考虑将0000作为基地址,则可以说20 ,最后一个值存储在F0000 + F = 000F ,即基地址+从该地址的偏移量。

0000 is likely just an offset from a localized address. 0000可能只是本地地址的偏移量。 Basically, BaseAddress + 0000 , BaseAddress + 0010 , etc. 基本上, BaseAddress + 0000BaseAddress + 0010等。

The values are ASCII, and they match the characters on the right. 值是ASCII,并且与右侧的字符匹配。 You've stored a text message (or string) in that memory. 您已在该内存中存储了一条短信(或字符串)。

Each memory address stores one byte (also 8 bits, also 2 hexadecimal ("hex") digits). 每个存储器地址存储一个字节(也为8位,也为2个十六进制(“ hex”)数字)。 0000 ( 0000 +0 ) has the hex value 50 , 0001 ( 0000 +1 ) has the hex value 61 , and so on. 00000000 +0 )具有十六进制值5000010000 +1 )具有十六进制值61 ,依此类推。

addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F  +0123456789ABCDEF
0000  50 61 78 20 69 73 20 61 20 72 65 61 6C 6C 79 20   Pax is a really 
0010  63 6F 6F 6C 20 67 75 79 00                        cool guy.

0000 seems to be an address but might be a relative address (or offset). 0000似乎是一个地址,但可能是相对地址(或偏移量)。 This output seems to say that 此输出似乎表明

addr            value
0000+0 (0000)   0x50  
0000+1 (0001)   0x61
0000+2(0002)    0x78 
  ....

Yes, 0000 can be considered an address. 是的,可以将0000视为地址。 As to what is "stored" at 0000 , specifically the value 50 is stored there, and at 0001 the value of 61 is stored, and on and on. 关于在0000 “存储”什么,具体地,值50存储在那里,在0001 ,值61被存储,并且不停地打开。

However, with higher level data structures, such as 16 bit integers, or 32 bit integers, or Strings, or even record structures, the answer to "what is stored at 0000 " gets much muddier. 但是,对于更高级别的数据结构(例如16位整数或32位整数或字符串,甚至是记录结构),“存储在0000 ”的答案变得更加混乱。

If a 16 bit binary integer is stored at 0000 , for an Intel architecture, then the value would be 6150 , or 24912 in decimal. 如果对于Intel体系结构,将16位二进制整数存储在0000 ,则该值为6150或十进制24912。 This is because the Intel is "little endian", and stored the Most Significant bytes later in the address space. 这是因为Intel是“ little endian”的,并且后来在地址空间中存储了最高有效字节。 For a "big endian" processor, the value is 5160 or 20832 decimal. 对于“大端”处理器,该值为十进制5160或20832。

Now, this could be coded in Binary Coded Decimal, in that case the value would be 6150 or 5160 in decimal (depending on endian-ness). 现在,可以使用二进制编码的十进制编码,在这种情况下,该值将是十进制的6150或5160(取决于字节序)。

So, you can see, even for something as simple as an integer, there are different ways it can be stored within memory. 因此,您可以看到,即使对于整数这样的简单对象,也可以通过不同的方式将其存储在内存中。

So, at the byte level, the answer is pretty simple. 因此,在字节级别,答案非常简单。 Going beyond that, it depends on how the data being stored is represented at the byte level. 除此之外,还取决于如何以字节级别表示存储的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM