CO Lab Manual
Course Page
  • Course Information
    • Welcome
    • Introduction
    • Your Contributions
    • Lab Sessions and Etiquette
    • Team Setup
    • Assumed Prior Knowledge
  • Setup Guides
    • GitHub Repository Setup
    • Technical Setup
      • Windows
      • Linux
      • macOS
    • GitHub SSH Setup
    • Framework Setup
  • Reference Documentation
    • Introduction to the Documentation
    • A Brief History Lesson
    • Syntax (Intel vs. AT&T)
      • Section Exercises
    • Memory
      • Memory Management
      • Section Exercises
    • Registers
      • Section Exercises
    • Instructions
    • Subroutines
      • Calling Subroutines
      • Writing Subroutines
      • Section Exercises
    • Input/Output
      • Printing to the Terminal
      • Reading from the Terminal
      • Section Exercises
    • Programming Constructs
    • Assembler Directives
    • C/C++ vs Assembly
    • Building and Running Programs
    • Address Sanitization
    • A0: A Running Example
  • Assignments
    • Introduction to the Assignments
    • Mandatory Assignments
      • A1: Subroutines and I/O
      • A2: Recursion
    • Extra Assignments
      • A3-a: Fibonacci Calculator
      • A3-b: Fibonacci REPL
      • A4: Diff
      • A5: Printf
      • A6: HPC
      • A7: Bitmap
      • A8: Game
  • Appendix
    • Acknowledgments
    • Rules and Regulations
    • Frequently Asked Questions
    • How to use a Debugger
Powered by GitBook
On this page
  • Assignment
  • Subroutine
  • Main Program
  • Recommended Approach
  • Required Knowledge
  1. Assignments
  2. Mandatory Assignments

A2: Recursion

CMake target: a2

cmake --build .build --target a2

If you were thinking of getting through this course without encountering everybody's favorite coding topic, we're sorry to disappoint... By now, you should have a fairly thorough understanding of the stack mechanism and its uses (e.g., local subroutine variables). This assignment will put your stack knowledge and abilities to the test.

A recursive subroutine is a subroutine that calls itself, usually with a modified parameter, until a so-called base case is reached (at which point the fun of properly returning from the depths of your call stack begins).

This assignment requires you to write a recursive subroutine. The subroutine itself will not be long with around 15 instructions in total, however, writing it will be fairly difficult due to its recursive nature. Do not get discouraged if it takes you a few hours to get it working (after all, we're not competing for the best time-per-instruction-written score here and a concise but well-thought-out program is usually better than a rushed and messy one).


Assignment

Write a program that asks the user for an unsigned 64-bit integer (nnn) and then prints the factorial (n!n!n!) of that number. The factorial should be calculated using a subroutine.

Subroutine

The subroutine should have the following signature:

uint64_t my_factorial(uint64_t n)

As the name suggests, it should calculate the factorial of the given argument and return the result.

Even though it is possible (and likely easier) to implement this subroutine iteratively (as opposed to recursively), iterative solutions will not pass the assignment!

There is an automated test on CodeGrade that indicates whether your solution is likely recursive. Some iterative solutions may also pass this test. Thereby, even though you may pass this test, the TA handling your hand-in will again check for recursiveness and will reject your submission if it calculates the result iteratively.

Main Program

Your main program should get the needed input from the user, call my_factorial to calculate the result, and finally print the result. The final program output should look like this:

Enter a number: <n>
<n>! = <n!>

where <n> is the given input and <n!> is the factorial of n.


Recommended Approach

  1. Write a (pseudocode) specification for a recursive factorial algorithm. You may get your specification checked with a TA if you want to. Alternatively, you may want to again implement the specification in a higher-level language (like Python or C++) first to test for correctness.

  2. Implement your specification in Assembly.

  3. Write the remaining parts of the program.


Required Knowledge

PreviousA1: Subroutines and I/ONextExtra Assignments

Last updated 4 months ago

Technically, this assignment does not introduce any concepts that have not appeared before. However, as you are now working with recursive subroutines, it is essential that you have a strong foundational understanding of the concepts of and .

Stack Frames
Local Variables