Question

I quite understand a what 32 or 64 bit system means. so basically all registers or word length is either 32 or 64 bit.

For simplicity let us take a 32 bit system and say i'am writing a program in C. if i declare a int type say "int a = 5;" then a memory location of one word length is reserved for var a. so when ever i want to access it i can do so using word address for that memory location.

But say i have 4 characters " char a,b,c,d;" since it is one byte each all of them are placed in one word, so what do i do if i want to access only char b?(saying the memory is byte addressable) now say b is the third byte in the word....then how does it come on to the bus? is'nt the 3rd byte hard wired to 17th to 24th line in the bus? so what happens to the other 24 lines when only b is being accessed?

Was it helpful?

Solution

The answer to your question largely depends on which compiler you use and the internal workings of your CPU, memory controller and memory architecture (cache and external memory).
You only have control over the compiler (assuming you are using C or C++ compiler). Compilers have different modes for cases when you are using variables which are smaller than a word size. There are flags for speed optimization and memory optimization. Depending on which of those flags are turned on, the compiler may choose to generate code which packs all four variables (in your case) into one word. Or the compiler may choose to allocate a memory word for each of the variables but use a particular byte to store the actual value. The way the compiler will do it for each of the cases is to generate different set of instructions for the CPU. In the latter case, if the variable is read from memory then the entire word is put on the bus and then into a general purpose register. In the former case, the word is put in the register but then the word can get shifted bit wise and the other bits can be zeroed out by using logic AND operation. That way the right byte is going to be in the register. Or may be the CPU architecture supports byte level access in a word in which case it will be only one operation performed by the CPU. In the end, it is a good idea to understand what happens inside but you will not care much because the set of instructions generated by the compiler will work correctly from your standpoint. The only time you will care is when you write performance sensitive software. In that case you will need to know the details of your CPU and memory as well as the flags supported by the compiler.

OTHER TIPS

It depends on the assembler, it may choose to give one word of memory or a byte.
So now even if you have 4 different characters in the word, what would happen is all of them are accessed at once, but only the one you require is operated on. i.e. all of them come into the processor from the memory, then only the byte you want is considered, the others are rejected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top