Search code examples
assemblymemorycpu-architectureram

Calculating required bytes to store an integer


Given that our RAM unit has 6 address bits going into it and a 32-bit architecture, how many bytes would be needed to store one integer value? The answer says 4 bytes, but I don't understand why that is the case.

Could someone please help me understand how they got 4?


Solution

  • The number of address bits doesn't really factor in to the basic of storing an integer.  (More on that later.)

    You need to know:

    1. how many bits an integer takes
    2. how many bits are stored per memory location/address

    Given those you can compute the number of memory locations needed to hold an int of that size, i.e.

    32 bits per integer / 8 bits per memory location = 4 memory locations per integer.

    If the machine stores 8 bits per memory address, then it is what we call byte addressable, and it will necessarily take multiple bytes to store anything larger than 8 bits.  Using multiple bytes also means using multiple addresses: the most common convention is that the entire multi-byte object is referred to by the (numerically) lowest address it uses.  Because a multi-byte entity takes multiple addresses, the next available address for some other purpose needs to be beyond the final byte of the multi-byte item.

    Some machines are word addressable, meaning that a single memory address stores more than 8 bits, sometimes 16, or 18, or larger.

    It is useful to have an integer size that can store a pointer, but in your case, a pointer only needs 6 bits, whereas on most 32-bit machines a pointer needs 32 bits.