Byte Swapping
Overview
It is commonly known that a 16-bit integer occupies two bytes of memory. Many people do not realize, however, that there is no standard order in which computers and instruments store these two bytes in memory or transmit them. As a result, computers that use one storage method cannot transmit integer data to devices that use another without first programmatically swapping each pair of bytes.
The National Instruments NI-488.2™ software, however, can swap the two integer bytes automatically when transmitting and receiving integer data, and require little or no software overhead. Byte swapping also can be handled in the hardware for even better performance. This application note describes the different methods computers use to store and transmit integer data and explains how NI-488.2 software overcomes these differences.
Table of Contents
Methods of Integer Storage
Although all 16-bit integers are stored in memory as eight-bit bytes, these two bytes can be stored in two different orders. As an example, consider the number 16, which in hexadecimal notation is written 001016. The following two bytes make up this number:- 0016 – most significant byte
1016 – least significant byte
Computers based on the Intel 80x86 family of processors store the least significant byte in the lower address and the most significant byte in the higher address. Thus, on an Intel PC, if the integer 16 were stored at memory location 2000, the 1016 would be stored in byte 2000 and the 0016 in location 2001. This form of integer storage has been dubbed little-endian, because the least significant, or "little", byte is stored in the low end of the memory word.
Computers based on the Motorola 68000 family of processors, however, store integers in the opposite order. That is, in the example above, the most significant byte, 0016, would be stored at the lower address, 2000, while the least significant byte, 1016, would be stored at address 2001. As you might expect, this form of storage has been named big-endian, because the most significant, or "big", byte is in the lower address.
If two computers exchange integer data, each must know the order in which the data will be sent and received; otherwise, the bytes might be interpreted in reverse order. In the example above, if an Intel machine transferred the integer value 16 in its native storage mode to a Motorola computer, which in turn interpreted the two bytes in its own native storage mode, the Motorola computer would think it had received the integer 100016, which is the decimal integer 4096.
Just as there is no standardization of integer storage among computers, until recently there has been no standardization of the order of integer data transmission between computers and/or instruments. However, the ANSI/IEEE Standard 488.2-1987 established that its message exchange protocol would use big-endian order when transferring integers.
For all practical purposes, this standard means that programs running on Intel processors that need to transfer binary integer data according to IEEE 488.2 protocols must swap each pair of bytes before transmission in order to transfer the bytes in big-endian order. Programs running on Motorola-based computers do not need to adjust their integer data, as their integer data is already stored in the prescribed order.
Notice that byte swapping only applies to binary integer data and certain other forms of binary data. When ASCII characters are transmitted, as is frequently done with IEEE 488.2-based instrumentation, no byte swapping is required.
Byte Swapping
The National Instruments NI-488.2 software can swap the integer byte pairs automatically when transmitting and receiving data, even when working with large arrays of integers. National Instruments interface boards can swap the bytes as they are being transferred to or from the GPIB with no additional software overhead.
To enable byte swapping, use the general configuration routine, ibconfig, which is provided in NI-488.2 software. You can also use ibconfig to alter a variety of other configurable features in the driver by passing different parameters to the routine.
To enable byte swapping during write operations, such as ibwrt, ibwrta, ibwrtf, ibwrti, use a call of the following format in the C language:
ibconfig (ud, IBC_WRITE_ADJUST, 1);
The parameter IBC_WRITE_ADJUST, which is included in decl.h, informs ibconfig that you want to alter the byte order of write operations. The third parameter, 1, indicates that you want to turn on integer byte swapping. The first parameter, ud, is the unit descriptor of the board or device whose write operations you want to byte swap.
This call affects all future write operations. A call such as the following transfers the 500 integers in the integer array intbuf, with each integer byte-pair swapped:
ibwrt (bd, intbuf, 1000);
To enable byte swapping when reading data from an instrument, use the following ibconfig routine:
ibconfig (bd, IBC_READ_ADJUST, 1);
The parameters are similar, except that the parameter IBC_READ_ADJUST is used to indicate that read operations, such as ibrd, ibrda, ibrdf, ibrdi, should have their bytes swapped.
To disable byte swapping and resume normal unswapped transmission, use an ibconfig routine of the same format as above, passing 0 as the third parameter instead of 1. If your system includes a number of instruments that transfer integer data in different order, you may have to enable and disable byte swapping several times using the ibconfig routine.
There are only a few pitfalls to watch for when you use the byte swapping feature. When you use the hardware byte swapping feature, the buffers you use in byte-swapped read and write calls must begin on an even address. In addition, the byte count in the routine must be even; no extra bytes can be transferred, only integer byte-pairs. Fortunately, almost all compilers either begin integer arrays on even addresses or can be forced to do so with command-line flags, so you are unlikely to encounter a buffer that begins on an odd address. If by some chance you do transfer an odd-addressed buffer or an odd count to an I/O function while byte swapping is enabled, the error ECAP will result, and you should take pains to align the buffer properly.
Summary
The different methods of integer storage used by different computers have in the past required users to expend time and effort adjusting their integer data for accurate transmission. By automatically performing byte swapping, however, the National Instruments NI-488.2 software and hardware free the user from the programming tedium of manually swapping bytes, and ensure complete compatibility with the IEEE 488.2 standard with little or no additional software overhead.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
