As the instruction set of the PRIMA does not include a multiplication instruction, we use a nested loop to replace one multiplication with n additions. Due to the word-width of only 8-bits, the program is only useful for input arguments 0..5 with corresponding factorials 1, 1, 2, 6, 24, 120. For inputs larger than 5, the program only calculates the lower 8 bit of the result due to arithmetic overflow.
Open the memory editor (popup-menu, edit-component) to watch the execution of the program. The input argument is expected at addresss 0x55 and the output is written to memory address 0x56. Intermediate variables (counters) are kept at memory addresses 0x60..0x63.
Note: the program needs quite a few cycles to complete its calculation, even for the default input value of 3 (at address 0x55). Simply wait for the program to write address 0x56 (the factorial result) and to enter the endless wait loop at address 0x32. You might want to change the clock generator frequency via the popup-menu and edit-component to speed up the execution a bit. Try setting a clock period of 0.001 seconds and press apply or ok in the clock generator property sheet.
Note: to keep the schematics as simple as possible, this applet still uses the simplified datapath with 8-bit ALU and 8-bit accumulator register. However, the program here relies on the BZE (branch-if-zero) instruction. Therefore, the accumulator output is now connected to the corresponding input port of the control unit.
Run the applet | Run the editor (via Webstart)