ARM > Introduction to ARM > Task One - Answers
Previous topic:
Task One
iOS Wedding Vector Trace Toy Toolbar Tip The Great Escape TargetedOptimisation Spectrum Slide Site Sinclair Simpsons Script Risc PC Reverse Engineering Retro Recreation RISC OS QuickFiler Python Project PrivateEye Pixel Art PhotoFiler Optimisation Ocean MotionMasks Logo Links LEGO Iyonix Isometric IntroductionToARM Hardware Hard Disc Groening GitHub Geminus Game Futurama Electron EfficientC Dump Doodle Disenchantment Disassembly Containers Chase H.Q. Blog Blender BasicOptimisation BBC Micro Article Archimedes Aha Acorn ARM 3D 2D
The routine turns an ASCII character lower-case. In C it would be written like this:
int mystery(int c)
{
unsigned int t;
t = c - 'A';
if (t <= 'Z' - 'A')
c += 'a' - 'A';
return c;
}
The tricky thing here is the coercion to unsigned int which allows us to replace two comparisons with a single one. This trick, the unsigned range optimisation, is discussed in the Efficient C for ARM course.
We can write it in a more expected way like this:
int mystery2(int c)
{
if (c >= 'A' && c <= 'Z')
c += 'a' - 'A';
return c;
}