I am required to write a factorial function in ARM assembly language that takes an integer $ n $ as input and returns the value of $ n! $. However, I can only use the $ 4 $ basic arithmetic operations $ + - * / $ (so no if-else or loop or recursion). Can anyone suggest a hint to do that? I can only do if I am allowed to use if-else or recursion.

  • 3
    $\begingroup$ Any other available operations? Read, write, compare etc.? $\endgroup$ – Evil Oct 10 '16 at 4:30
  • 1
    $\begingroup$ Think about how you would define the factorial function recursively as a mathematical function, then implement that. But "How do I code X in language Y?" is off-topic at Computer Science. $\endgroup$ – David Richerby Oct 10 '16 at 7:54

we were required to do something similar. We were given a number to calculate, which was 7!.r0 will hold the final value. I ended up doing the following:

MOV r1, #7 MOV r1, #6 MOV r1, #5 MOV r1, #4 MOV r1, #3 MOV r1, #2 MOV r1, #1

MUL r0, r1, r2 MUL r0, r0, r3 MUL r0, r0, r4 MUL r0, r0, r5 MUL r0, r0, r6 MUL r0, r0, r7


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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