Subject: Re: unsigned chars (was Re: Possible bug in arm32 strongarm
To: Chris Gilbert <chris@buzzbee.freeserve.co.uk>
From: Neil Hoggarth <njh@kernighan.demon.co.uk>
List: port-arm32
Date: 10/28/2000 10:54:00
On Fri, 27 Oct 2000, Chris Gilbert wrote:

> Just cos I'm a curious bunny and it's not in the FAQ, but why do we use 
> unsigned chars as the default for chars?

They are regarded as more efficient.

Signed 8-bit manipulations are expensive on the ARM. Recall that the
architecture has no instructions/hardware for 8-bit arithmetic - all the
machine instructions deal with 32-bit words[1]. To do signed 8-bit
arithmetic with the 32-bit ALU then each time you fetch an 8-bit quantity
from somewhere into a register then you need to sign extend the sign bit
from the bottom byte to the top three bytes of the register, in software,
before you perform the required addition or whatever.

The rotations on non-aligned memory access that we were discussing earlier
exist for a similar historical reason: the ARM has no 8-bit memory
addressing modes. If you want to fetch a byte from memory then you have to
fetch the 32-bit word that contains that byte and then rotate and AND-mask
the value to extract the required 8-bits. The rotations that the hardware
performs on the data are designed to emulate byte-addressable memory: you
can step through memory one location at a time, fetching 32-bit words, and
the byte that your pointer is nominially pointing to will always end up in
the bottom 8 bits of the CPU register. All the programmer needs to do is
AND with 0xff to get an (unsigned) 8-bit value.

Regards,

Neil.

[1] I'm talking from memory, and only really about the ARM2/3 - I've never
dealt with the later versions of the architecture at a machine level.