# Byte order reversal

## About this recipe

This recipe gives a compact ARM Instruction Sequence to perform byte order reversal ie. reversing the endianess of a word.

## The details

Changing the endianess of a word can be a common operation in certain applications. For example when communicating word sized data as a stream of bytes to a receiver of the opposite endianess.

This operation can be performed efficiently on the ARM, using just four instructions. The word to be reversed is held in a1 both on entry and exit of this instruction sequence. ip is used as a temporary register (For more information about these register names see Interfacing Assembly Language and C):

``

        EOR    ip, a1, a1, ror #16
        BIC    ip, ip, #&ff0000
        MOV    a1, a1, ror #8
        EOR    a1, a1, ip, lsr #8

A demonstration program which should help explain how this works has been provided in source form in the examples directory. To compile this program and run it under armsd first set your current directory to be examples and then use the following commands:

``

    >armcc bytedemo.c -o bytedemo -li -apcs 3/32bit
    >armsd -li bytedemo
    A.R.M. Source-level Debugger, version 4.10 (A.R.M.) [Aug 26 1992]
    ARMulator V1.20, 512 Kb RAM, MMU present, Demon 1.01, FPE, Little endian.
    Object program file bytedemo
    armsd: go
        ... demonstration program executes ...

Note that this program uses ANSI control codes, so should work on most terminal types under Unix and also on the PC.