============================================ Putting things together (it will be a mess!) [TOC] ============================================ Having all components for the factorial program (reading an integer, computing the factorial and printing an integer) we "just" have to put things together. However, you will see that this will be kind of a mess. That's because we haven't covered yet how two implement functions (this will be the topic of the next two sessions). Why is it a mess? ================= Each of our program uses labels. When we combine them then some of them would be defined multiple times. The program `print_two_strings.s` (you find the code below) prints two different strings: ---- SHELL (path=session08/put_together) --------------------------------------- ulmas -o print_two_strings print_two_strings.s ulm print_two_strings -------------------------------------------------------------------------------- It basically uses the code from the "hello, world!" program, i.e. :import: session08/hello/hello.s For printing two different strings the following modifications were applied: - two strings in the data segment with labels `msg1` and `msg2`, - the instructions are basically duplicated (except for the `halt` instruction). - instead of jumping to the halt instruction when the string was printed we now jump to an label after the loop. You can see that this pattern can be applied in a "mechanical style": :import: session08/put_together/print_two_strings.s What else might become a mess? ------------------------------ When we put things together for the factorial project each component uses (and modifies) some registers. In general it is very tedious to make sure that there are no conflicts. The complexity of this project is already at the limit where this is feasible. At least if we lower our demands. It would nice to have a program that can be used like that: ---- CODE (type=txt) ----------------------------------------------------------- theon$ ulm factorial n = 3 3! = 6 -------------------------------------------------------------------------------- But we limit it to this ---- CODE (type=txt) ----------------------------------------------------------- theon$ ulm factorial n = 3 n! = 6 -------------------------------------------------------------------------------- Do you see the difference? It just prints "n! = 6" instead of "3! = 6". It is interesting how much this simplifies an implementation that does not use functions. And actually you should try to write a program that does actually print the result in the stye of "3! = 6" just to see how hard it is. But it is not required. `quiz05`: Computing the factorial again ======================================= Write a program `factorial.s` that - prints "n = ", - reads in an unsigned integer $n$, - computes the factorial $n!$, - prints "n = " and - prints the computed factorial. Note that if the factorial of $n$ can not be represented as a 64-bit unsigned integer the program will print the factorial of $n$ modulo $2^{64}$, and that's ok. Submit you program on `theon` with the command `submit quiz05 factorial.s`. Submit your program with ---- CODE (type=txt) -------- submit hpc quiz05 factorial.s ----------------------------- Skeleton for the `quiz05` ========================= The code below is a skeleton for `quiz05`. In it's current for it just print two strings: ---- SHELL (path=session08/put_together) --------------------------------------- ulmas -o factorial_io_skeleton factorial_io_skeleton.s ulm factorial_io_skeleton -------------------------------------------------------------------------------- So obviously you have to fill in some gaps for reading in an integer, computing the factorial and printing the result. The skeleton already contains comments that suggest where to insert your code. Basically that will your code from the last three assignments (with some modifications as suggested in the skeleton). In order to avoid conflicts due to how the components make use of registers it also contains some `.equ` directives. These are supposed to map register names such that everything works seamlessly together. Depending on your code for the previous quizzes you might need to adjust these directives: :import: session08/put_together/factorial_io_skeleton.s :navigate: up -> doc:index back -> doc:session08/page04 next -> doc:session08/page06