ARM > Introduction to ARM > Task Two - Answers
Previous topic:
Task Two
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
C test shell:
#include <stdio.h>
extern int factorial(int N);
int main(void)
{
int i;
/* The largest factorial we can store in an int,
* or an ARM register, is 12! or 479,001,600. So
* loop from 0..12. */
for (i = 0; i < 13; i++)
printf("The factorial of %d is %d.\n", i, factorial(i));
}
ARM assembly language factorial routine:
AREA |.text|, CODE, READONLY
; int factorial(int N);
<span class="k">EXPORT</span> factorial
factorial ; On entry, N is stored in R0.
MOVS r1, r0 ; R1 is our loop counter. Copy N to R1 and test.
MOVEQ r0, #1 ; if (R1 == 0) Set result to 1 and fall through to return…
loop
SUBNES r1, r1, #1 ; if (R1 != 0) Decrement R1 and test.
MULNE r0, r1, r0 ; if (R1 != 0) Result = R1 * Result.
BNE loop ; if (R1 != 0) Loop.
MOV pc, r14 ; Return with result in R0.
<span class="k">END</span>