Re: int vs char

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Y Khan writes:

 > Please let me know if this is not the right place to
 > ask this question.

OK, a bit off-topic, but ask away.

 > I had read somewhere that a 32 bit processor can
 > read/write only and only 32 bits in any one cycle.

That's not strictly true, as most 32-bit processors these days are
multiple-issue, but let's carry on.

 > So, I thought that if I needed a 'char' then 32 bits
 > would be read from memory into registers and then
 > would be manipulated there to get a 'char'. As such I
 > thought that it was logical that operations on int are
 > faster than on chars.

Not really, no, because reading a byte and extending it to 32 bits can
be done in a single operation.

 > I was reading an ARM processor manual some time ago
 > and I saw that processor has instructions to read a
 > single byte too.
 > 
 > So, I am confused now. I earlier thought that the
 > compiler generates such code that reads in 32 bits and
 > then manipulates those bits to get an 8-bit char
 > value.

That's not true.

 > Now, ARM processor manual says that the processor can
 > be instructed to read only one byte too. But the
 > processor registers are 32 bits. so, this is again
 > confusing.
 > 
 > This doesn't look very straightforward to me because
 > if the data bus is 32 bits wide then probably we are
 > still reading 32 bits.
 > 
 > So, the question is:
 > 
 > 1. Can only 8 bits be read from memory in a 32 bit
 > register?

Yes.

 > 2. When a processor gives an instruction to read only
 > 8 bits, then is it manipulating the 32 bits by itself?

No.  

 > 3. If the address of my 'char' is 0x5, then do we only
 > read the memory location 0x5, or from 0x4 to 0x7, and
 > then manipulate it to get the value at 0x5 in the
 > lowest 8 bits of the 32 bit register.

OK, this is how it usually works:

The processor reads a whole word.

The word is loaded into a barrel shifter (see
http://en.wikipedia.org/wiki/Barrel_shifter) and the byte we want is
shifted to the correct position in the word.

The high-order bits in the word are masked to zero (or the byte is
sign extended).

Writing a single byte is harder: we either have to do a
read/modify/write cycle or use byte select lines to the memory
devices.  This is where caches come into play: it is very likely that
the memory word we want to write is already in the cache, so even if
we do want to do a read/modify/write, that doesn't involve a read
access to external memory.

Andrew.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux