3.6. Byte Swapping

Version II.0, February 1979

page217 Byte-swapping problems occur when code generated on one machine is transferred to another or programs which directly interface with memory (e.g. the Patch utility) are written one or for one machine and transferred to another which has a different ordering for its memory.

There are two different ways to order bytes in a given memory:

A) Byte Zero is the byte containing the least significant half of the word. Byte One contains the most significant half.
B) Byte Zero is the byte containing the most significant half of the word. Byte One contains the least significant half.

The difference between these is the way Byte quantities are read and stored in memory. Word quantities, such as integers, will be read and looked at in the sane way on both types of machines. However, byte quantities such as P-code or characters will be reversed within each word.

An example:

DEFINITION(A)    (B)
VALUE (Hex)
ls*         ms*
---------------
|  04  |  07  |
---------------
ms*         ls*
---------------
|  07  |  04  |
---------------
Byte:
0     1
0     1

(least/most significant bit, thereby least/most significant byte)

If both of the words shown above were read as an integer, a word quantity, they would give the value 3,588. However, if the value of byte Zero was wanted (as in C: PACKED ARRAY[O..1] OF CHAR;) then Definition A would show a value of 04H and Definition B would show a value of 07H. Both definitions would show the value 07H if the most significant byte were specified.

Byte-swapping is not a hard problem to solve, it just requires a little thought. The Patch utility has type declarations for both types of machines and a study of it should suffice to show how to satisfy your programming needs.
page218 page219 page220


This page last regenerated Sun Jul 25 01:09:11 2010.