Lesson 15 of 15
Putting It All Together
Putting It All Together
Time to combine everything you have learned: data sections, registers, arithmetic, control flow, functions, and string operations.
Your Task
Write a program that computes the factorial of 5 (5! = 120) and prints the result followed by a newline.
Since 120 is a multi-digit number, you will need to convert it to a string of ASCII digits. The algorithm:
- Compute 5! = 120 using a loop or function
- Convert 120 to the string "120" by repeatedly dividing by 10:
- 120 / 10 = 12, remainder 0 -> digit '0'
- 12 / 10 = 1, remainder 2 -> digit '2'
- 1 / 10 = 0, remainder 1 -> digit '1'
- The digits come out in reverse order, so store them on the stack and print in the right order
Hints
- Use
divwith divisor 10 to extract digits - Remember to
xor rdx, rdxbefore eachdiv - Push each digit onto the stack as you extract it
- Count how many digits you pushed
- Print them all at once, or one at a time
Print: "120\n"
x86_64 runtime loading...
Loading...
Click "Run" to execute your code.