Wednesday, January 30, 2013

My ARM Is Stronger Than My THUMB

In this chapter we'll have a look at ARM code vs THUMB code. For starters, the term ARM comes up a lot.  This is the ARMv5te architecture on which an ARM946E-S CPU runs made by a company called ARM.  Now to add one more level to that, the ARM assembly language is actually split up into 2 modes called ARM and THUMB.  ARM instructions are 32-bit.  They look like "E02D4010", for example.  THUMB instructions are 16-bit.  They look like "B510", for example.  So then, if THUMB is half the size, why not do everything in THUMB?  Obviously it would save space and since it fits more code into the instruction cache(don't worry about what this is for now, just saying) and makes for a smaller ROM, it's ideal.  Well, the down side is that since there's less room for information in a 16-bit instruction, you can't do as much with it.  Thus, in some situations, using ARM instructions for a routine is better. 

So when is THUMB a better choice?  Simple functions should pretty much always be in THUMB.   Get and set functions, functions with little complex math, boolean tests, simple loads and stores, and simple comparisons all work better in THUMB.  In THUMB, only branches can have conditional suffixes which is a big drawback for complex functions.  This is something we'll go into in the next part, but long story short, the instructions look like bne(branch if not equal), bhi(branch if higher), and bls(branch if less or same).  They're actually b-ne, b-hi, and b-ls where the last part is a suffix which causes the instruction to trigger based on specific conditions evaluated by the CPU.  This is actually really simple and we'll get to it next time. 

ARM is a better choice for complex algorithms and math, stuff like sound mixing and compression just to name a couple.  Any instruction can use those conditional suffixes which means that there's much, much less branching around. If there is more than one bit-related operation(shifting, masking, etc.) then ARM is likely the way to go. 

There are some limitations on THUMB, as well.  It can't fetch the program status registers nor does it have explicit access to r8-r12.  There are a few limited instructions that can access those registers, but they are not used much.  Also, due to the lack of registers, there is a greater use of the stack.  As I was saying before, only branches can be conditional.  This is kind of a big deal in functions with lots of conditionals and loops.  Due to the reduced capability of creating immediate values(and also because of the reduced number of registers), THUMB functions will also often have slightly larger literal pools.  This isn't a big deal since THUMB functions are usually already 60-70% the size of the equivalent ARM function.  We'll be dealing with literal pools soon.  All you need to know now is that they're caches of data that sit at the end of each assembly function that the function can load and use.


Next time we deal with conditionals in assembly and how that affects ARM vs THUMB.

No comments:

Post a Comment