Re: Typecasting goes the wrong way
On Jul 10, 12:52 pm, "Alf P. Steinbach" <a...@start.no> wrote:
* James Kanze:
[...] I'm not sure what one would call
the order 2301 (used for longs in PDP-11 and Intel 16 bit
processors). The easiest way to see this is to do something
like:
uint32_t x = 0x03020100 ;
uint8_t const* p = reinterpret_cast< uint8_t const* >( &x ) ;
std::cout << (int)( *p ++ ) ;
std::cout << (int)( *p ++ ) ;
std::cout << (int)( *p ++ ) ;
std::cout << (int)( *p ++ ) ;
std::cout << std::endl ;
This outputs 3210 on my Sparc, and 0123 on my PC (and 2301 with
earlier versions of the Microsoft compiler, on a PC).
As far as I know and remember all Intel 16 bit processors were little
endian. Regarding the x86 family, they're little endian. The result of
the above program should never be anything but 0123 on an x86 PC, so you
are probably remembering incorrectly how Microsoft's compiler behaved:
if it did produce the result you claim then programs using integers
would not work, which seems very very unlikely (I'm being careful here).
The hardware was decidedly little endian, but the hardware
didn't support 32 bit integers. The compiler emulated in
software, and put the high order 16 bit word before the low
order one. I think the 8087 floating point unit did, but it
generally wasn't supported on PC's.
I'm very sure of my case here, since I remember a lot of code
breaking when Microsoft changed the layout. Recompiling with a
new version of the compiler meant that code which just binary
dumped to disk for its persistency couldn't read the old files.
(Why do you think I'm so sensibilized to this problem:-)?)
The reason they changed, of course, was so that the binary
layout would be compatible with that on a 80386.
I think the source of your confusion (assuming it is) is that in a
debugger displaying byte values, on a little endian machine the value
0x3210 will typically be displayed as 0x1032. That's then because the
debugger is display memory contents from left to right, whereas our
number notation, inherited from the Arabic dominance of parts of Europe,
is designed for right to left. Simply display memory contents from
right to left, and on the little endian machine, such as a PC, integers
come out right, while of course then ordinary text is backwards...
I've never used a debugger on Intel. I have used the Intel ICE,
before the PC even appeared, but it displayed words as words,
not as bytes.
Regarding the PDP-11 it's been a long time since I did any assembly
programming on the PDP-11 (and the VAX), and what I did was very very
little anyway, but since the PDP-11 was very orthogonal in all ways,
very clean design except that silly stuff of having memory mapped
registers, I think maybe and probably you have that wrong too.
I'm pretty sure about that too. Again, hardware did one thing,
and who ever wrote the compiler did the opposite for the types
which weren't directly supported. (I'm not very sure, but I
have vague memories that the hardware floating point on a PDP-11
was big-endian with regards to 16 bit words, but little endian
with regards to bytes in each word. I never did much floating
point on a PDP-11, however.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34