Re: program logic based on endianness



sam_cit@xxxxxxxxxxx writes:

Hi Everyone,

I have a program unit which does >> and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.

Accordingly, if i need the program to work fine in a little-endian
system. I understand that the code needs to be changed.

No. At least if you work with integers.


I wanted to know if the above holds true for bitwise and (&) and
bitwise or (|). I think, the processor should take care of the
operation a&b or a|b irrespective of the endianness of the system.

Please provide your comments.

Integers (or bits), when stored in registers, are not endian in any
way. They're just integers (or bit fields).

Endianness is a mapping between the registers and the memory. This
problem appears when the size of the registers (the number of bits
they can take) is greater than the size of the addressable memory
unit.

We use addresses of octets (8 bits), and registers of 32 bits. So we
must find a way to map these 32 bits to 4 octets in memory, given an
address.

We could do:

address[0]=(register&0xff);
address[-100]=(register>>8)&0xff;
address[7]=(register>>24)&0xff;
address[6]=(register>>16)&0xff;

but this wouldn't be too practical or meaningful...


So the main two ways to do it are:

/* little endian */
address[0]=(register&0xff);
address[1]=(register>>8)&0xff;
address[2]=(register>>24)&0xff;
address[3]=(register>>16)&0xff;

/* big endian */
address[3]=(register&0xff);
address[2]=(register>>8)&0xff;
address[1]=(register>>24)&0xff;
address[0]=(register>>16)&0xff;


But there was also this popular mapping (in PDP IIRC):

address[1]=(register&0xff);
address[0]=(register>>8)&0xff;
address[3]=(register>>24)&0xff;
address[2]=(register>>16)&0xff;



So the question is whether your result is specified in terms of
integers, or in terms of bytes in memory.

If it was specified in terms of bytes in memory, you'd be better if
you'd compute each byte independently. (eg as in the above mappings).

If it's really specified for an integer, the byte order used to store
them to memory doesn't matter.

Possibly, your memory may be used as a buffer to write to a file with
a specified byte order, then you may have to store your integers in this
specified byte order. If you need to store big-endian integers, you can
use the functions htonl (and ntohl to read them), since the network
byte order is big-endian.

--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
.



Relevant Pages

  • Re: Reviewing history
    ... the machie I would architect would be remarkably like the ... Rm) and one more bit for the opcode be better than 8 registers with a 3 ... The instruction set access to memory was simply fabulous! ... Say you were going to sum two vectors and store it in a third vector: ...
    (comp.arch)
  • Re: RfD: Memory Access - v2 (Long)
    ... endian: BE for big-endian ... bytes for memory efficiency. ... wordset queries are a feature that is hardly used, ... Store the 8 LSBs of x at addr. ...
    (comp.lang.forth)
  • Re: Tiny CPUs in programmable logic
    ... 2 registers IP and working reg. ... So thats 5 bits op and any width dat per memory cell. ... A null memory store is when a direct store is left unused by never ... put the double indirect as a single indirect/no pre-post. ...
    (comp.lang.forth)
  • Re: equivalence with dummy arguments
    ... > It does, however, work nicely for VAX F and D floating-point values. ... > big-endian in putting the components together into a complete fp value. ... been originally developed on RSX-11M and saving memory was a 'good' ... can rely on the collating sequence of COMMON or PSECT names, ...
    (comp.lang.fortran)
  • [PATCH 1/3] Support BE8 mode kernel modules relocation
    ... When reading code from memory in the relocation ... procedure, these instructions are read according to big-endian, so they ... need to be inverted before writing to memory and after reading from memory. ...
    (Linux-Kernel)