Search code examples
assemblymemory

How does the computer stores the values in memory?


am learning assembly and I have been wondering how exactly does computer stores the memory works for example : there is a decimal value of 200078 which is 0x30D8E in hex and 110000110110001110 in binary as far as I know each address holds one byte of data so if we store this value in the memory as and represent in binary using little endian will it be like this:

Address | Value
0x100   | 10001110
0x101   | 00001101
0x102   | 00000011

and if we represent it in hex will it look something like this

Address | Value
0x100   | 8E
0x101   | 0D
0x102   | 03

because we are not adding the values in these memory addresses like 8E + OD + 03 to get the value we want but instead we are putting them like a "string" and laying them from the starting, like we will first put E and then 8 before that which will make it 8E and then D before that and now it will be D8E and so on, right? or am i wrong and the memory stores the values in a different way?


Solution

  • When considering a 32 bit value for example 0x12345678. Being stored at address 0x200. From a byte address perspective and if you were to write the 32 bit word to 0x200 then do byte reads of these addresses this is what you would find (for a little endian arm).

    0x200 0x78
    0x201 0x56
    0x202 0x34
    0x203 0x12
    

    If the thought of a string or an array helps the understanding then yes. From a byte address perspective the number is broken in to bytes and the least significant byte (the little end) is first, meaning the lowest address.

    Same would go for a 16 bit value 0xAABB

    0x302 0xBB
    0x303 0xAA
    

    Where you went off the rails is trying to then understand the mechanism as to how it gets to memory. It is not 4 bits at a time. Furthermore hexadecimal notation or decimal or octal or other is for the convenience of the humans. The computer stores bits, the 16 bit value 0b10101010101101011 is the same as 0xAABB, just easier for humans to carry the 0xAABB around when communicating to each other or when writing software. The two versions of that 16 bit value is just that same 16 bit value and the memory stores it as individual bits (written all at once in this case).

    What memory actually looks like and how these bits get there is a much longer story.