36

I'm fairly new to programming for ARM. I've noticed there are several architectures like ARMv4, ARMv5, ARMv6, etc. What is the difference between these? Do they have different instruction sets or behaviors?

Most importantly, if I compile some C code for ARMv6, will it run on ARMv5? What about ARMv5 code running on ARMv6? Or would I only have to worry about the difference if I were writing kernel assembly code?

5 Answers 5

42

The ARM world is a bit messy.

For the C programmers, things are simple: all ARM architectures offer a regular, 32-bit with flat addressing programming model. As long as you stay with C source code, the only difference you may see is about endianness and performance. Most ARM processors (even old models) can be both big-endian and little-endian; the choice is then made by the logic board and the operating system. Good C code is endian neutral: it compiles and works correctly, regardless of the platform endianness (endian neutrality is good for reliability and maintainability, but also for performance: non-neutral code is code which accesses the same data through pointers of distinct sizes, and this wreaks havoc with the strict aliasing rules that the compiler uses to optimize code).

The situation is quite different if you consider binary compatibility (i.e. reusing code which has been compiled once):


  • There are several instruction sets:
    1. the original ARM instruction set with a 26-bit program counter (very old, very unlikely to be encountered nowadays)
    2. the ARM instruction set with a 32-bit program counter (often called "ARM code")
    3. the Thumb instruction set (16-bit simplified opcodes)
    4. the Thumb-2 instruction set (Thumb with extensions)

A given processor may implement several instruction sets. The newest processor which knows only ARM code is the StrongARM, an ARMv4 representative which is already quite old (15 years). The ARM7TDMI (ARMv4T architecture) knows both ARM and Thumb, as do almost all subsequent ARM systems except the Cortex-M. ARM and Thumb code can be mixed together within the same application, as long as the proper glue is inserted where conventions change; this is called thumb interworking and can be handled automatically by the C compiler.

The Cortex-M0 knows only Thumb instructions. It knows a few extensions, because in "normal" ARM processors, the operating system must use ARM code (for handling interrupts); thus, the Cortex-M0 knows a few Thumb-for-OS things. This does not matter for application code.

The other Cortex-M know only Thumb-2. Thumb-2 is mostly backward compatible with Thumb, at least at assembly level.


  • Some architectures add extra instructions.

Thus, if some code is compiled with a compiler switch telling that this is for an ARMv6, then the compiler may use one of the few instructions with the ARMv6 has but not the ARMv5. This is a common situation, encountered on almost all platforms: e.g., if you compile C code on a PC, with GCC, using the -march=core2 flag, then the resulting binary may fail to run on an older Pentium processor.


  • There are several call conventions.

The call convention is the set of rules which specify how functions exchange parameters and return values. The processor knows only of its registers, and has no notion of a stack. The call convention tells in which registers parameters go, and how they are encoded (e.g. if there is a char parameter, it goes in the low 8 bits of a register, but is the caller supposed to clear/sign-extend the upper 24 bits, or not ?). It describes the stack structure and alignment. It normalizes alignment conditions and padding for structure fields.

There are two main conventions for ARM, called ATPCS (old) and AAPCS (new). They are quite different on the subject of floating point values. For integer parameters, they are mostly identical (but AAPCS requires a stricter stack alignment). Of course, conventions vary depending on the instruction set, and the presence of Thumb interworking.

In some cases, it is possible to have some binary code which conforms to both ATPCS and AAPCS, but that is not reliable and there is no warning on mismatch. So the bottom-line is: you cannot have true binary compatibility between systems which use distinct call conventions.


  • There are optional coprocessors.

The ARM architecture can be extended with optional elements, which add their own instructions to the core instruction set. The FPU is such an optional coprocessor (and it is very rarely encountered in practice). Another coprocessor is NEON, a SIMD instruction set found on some of the newer ARM processors.

Code which uses a coprocessor will not run on a processor which does not feature that coprocessor, unless the operating system traps the corresponding opcodes and emulates the coprocessor in software (this is more or less what happens with floating-point arguments when using the ATPCS call convention, and it is slow).


To sum up, if you have C code, then recompile it. Do not try to reuse code compiled for another architecture or system.

0
6

Think of this ARM vs ARM thing like a wintel computer vs an intel mac. Assume even you have the same intel chip (family) on both computers, so portions of your C code could be compiled one time and run on both processors just fine. Where and why your programs vary has nothing to do with the intel processor but everything to do with the chips and motherboard around it plus the operating system in this case.

With ARM vs ARM most of the differences are not the core but the vendor specific logic that surrounds the core. so it is a loaded question, if your C code is some application calling standard api calls then it should compile on arm or intel or powerpc or whatever. If your application gets into talking to on chip or on board peripherals then no matter what the processor type is, one board, one chip will vary and as a result your C code has to be written for that chip or motherboard. If you compile a binary for ARMv6 it can and will have instructions deemed undefined on an ARMv4 and will cause an exeception. If you compile for ARMv4 the ARMv6 should run it just fine.

At best, if you are in this application space, then what you will likely see is just performance differences. Some of which have to do with your choice in compiler options. And sometimes you can help with your code. I recommend avoiding divides and floating point wherever possible. I dont like multiplies but will take a multiply instead of a divide if pushed. x86 has gotten us spoiled with unaligned accesses, if you start now with aligned I/O, it will save you down the road as you get into other chips that also prefer aligned accesses, and or you get bit by the various was operating systems and bootloaders configure the ARM to react, none of which is what you were used to on an x86. Likewise keep this habit and your x86 code will run much faster.

Get a copy of the ARM ARM (google: ARM Architectural Reference Manual, you can download it for free many places, I dont know what the current rev is, rev I or something maybe). Browse through the ARM instruction set and see that most instructions are supported on all cores, and some were added over time like divide and byteswap and such. You will see there is nothing to fear between the cores.

Think from a systems perspective, the wintel vs the intel mac. ARM does not make chips, they make and license cores. Most vendors that use an ARM in their chip have their own special sauce around it. So it is like the wintel vs the mac with the same processor in the middle, but completely different when it comes to all the stuff the processor touches and has to use. It doesnt stop with the ARM core, ARM sells peripherals, floating point units, caches, etc. So few if any ARMv4s are the same for example. If your code touches the differences you will have problems if it doesnt you wont.

For the arm portions of the chip in addition to the ARM ARM there are TRMs (Technical Reference Manuals). but if you get the wrong trm for the component you are using it may give you headaches. The TRM may have register descriptions and other such things that the ARM ARM doesnt, but if you are living in application space you likely wont need any of them, nor the ARM ARM. The ARM ARM is good for educational purposes if nothing else. Understanding why you might not want to divide or use unaligned accesses.

2
  • I work on an operating system that runs on a variety of ARM processors. The software interface is mostly the same across all of them, so I'm mainly curious about binary compatibility. I'm under this impression that the difference is more analogous to i686 vs i386, or i686 with SSE3: mostly backward compatible with some new instructions. Is that accurate?
    – Jay Conrod
    Commented Dec 8, 2010 at 17:59
  • As far as the arm instructions go, yes, the newer cores added new instructions. The floating point units though are not binary compatible so you have to be careful there. In theory you could compile one least common denominator binary that works across the board, it might not perform well enough though. And/or you could have if-then-else code in the program that if this core or whatever is detected then use it otherwise dont. The ARM ARM will show you the ARM instruction compatibility.
    – old_timer
    Commented Dec 8, 2010 at 20:14
4

ARM its self is fairly compatible, provided you stick to user code (kernel code of course is different). In a hosted-OS environment, you will likely stick to ARMv5 (ARM926 processors).

The big difference comes from:

  1. Cache behavior is widely different. Cache on some ARMs is even virtually addressed, which can make process switches painful.
  2. The FPU comes in several flavors (VFP, NEON, and more!). Many smaller processors do not even have an FPU.
  3. Thumb mode has changed dramatically. Thumb mode between ARMv5 is not portable to Thumb2 (ARMv6+), nor backwards compatible.
2
  • This doesn't really answer the question as asked (which was "from a C programmer's perspective"). Commented Dec 8, 2010 at 9:17
  • 1
    Thanks for this answer. It sounds like binary compatibility is pretty good as long as you stay away from FP operations and thumb mode.
    – Jay Conrod
    Commented Dec 8, 2010 at 17:56
3

If the difference is really that important to you, you should be able to figure it out from ARM's public documentation.

But the whole point of writing in a higher-level language (even if it's only as "high" as C) is to not worry about it. All you do is recompile. Even within the kernel, not that much really needs to be written in assembly; and when you do have to write something in assembly (i.e. not just to get maximum performance), it's typically because of more than just the choice of CPU (e.g. what's been directly memory-mapped where?).

3

Very quick and dirty list of areas to check when porting between architectures in general:

  • Endianness: union usage, casting of data types, bitfields, data sharing
  • Alignment: alignment requirements but also performance characteristics of possible unaligned access
  • Memory model: weak vs strong?
  • Multi-core: how does coherency work?
  • Miscellaneous: signed vs unsigned data types, data structure packing, stack usage, enum data type...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.