========================================= Quiz: Computing the factorial recursively [TOC] ========================================= Having subprograms that can call other subprograms is sufficient to implement a program that computes the factorial recursively. Use the C code below as a pseudo code description for your assembly program `factorial.s`. On `theon` submit your program with the command `submit hpc quiz11 factorial.s` Of course it is intended that you make use of the example program from the __previous page__. C code as pseudo code for your program ====================================== This code describes a program with subprograms `factorial`, `getui`, `puts`, `putui` and `main` that use the global variable `arg` for communication. Execution of the program begin with a call of `main`. :import: session10/subprog/factorial.c Some suggestions for "how to approach" ====================================== Personally I would begin with an "empty" function `factorial`, i.e. an implementation that can be called but just returns: ---- CODE (type=s) ----------------- factorial: jmp %RET, %0 ------------------------------------ Then I would complete the subprogram `main`, so that all this tedious calling of subprograms, passing and receiving arguments is done. All this stuff is just copy and paste from the example on the __previous page__. The desired result after that step would be a program that could be used on the command line as follows: ---- CODE (type=txt) --------------- theon$ ./factorial n = 5 5! = 5 ------------------------------------ Having completed that would mean that I have a working framework. After checking in the debugger that everything works and observing how it works I would begin with the implementation of the function `factorial`. Now the trick is to figure out the right strategy for more sub-steps for doing that. Each sub-step should be going into the right direction, easy to accomplish and easy to validate. Easy to validate means, that you should have an idea how you can check if it is working without spending hours in the debugger. In my opinion, a good next step would be to implement this non-sense in assembly: ---- CODE (type=c) ---------------------------------- void factorial() { uint64_t n; n = arg; arg = arg - 1; putui(); putchar('\n'); arg = n; } ----------------------------------------------------- If you do it right then the program would do the following ---- CODE (type=txt) --------------- theon$ ./factorial n = 5 5! = 4 5 ------------------------------------ This `4` would show up because my non-sense factorial prints the argument minus one. You still would see the last 5 in "5! = 5" because my non-sense factorial restored the argument with the value stored in the local variable. So it would be easy to check if my implementation can do the most relevant things: Store something in a local variable, call a function, fetch something from a local variable. Even if some of this is not working at first it would be easy to figure out the problem and fix it. However, what strategy is most suitable for you depends on your experience and personal preferences. Some variant that compiles with gcc and works ============================================= :import: session10/subprog/factorial_gcc.c [fold] ---- SHELL (path=session10/subprog, fold) -------------------------------------- gcc -o factorial factorial_gcc.c -------------------------------------------------------------------------------- :links: previous page -> doc:session10/page06