Task: Sum of First N Natural Numbers Squared
You will solve this exercise starting from the sum_n.asm file located in the tasks/sum-squared/support directory.
In the sum_n.asm program, the sum of the first num natural numbers is calculated.
Follow the code, observe the constructions and registers specific to working with bytes. Run the code.
IMPORTANT: Proceed to the next step only after you have understood very well what the code does. It will be difficult for you to do the next exercise if you have difficulties understanding the current one.
Start with the program sum_n.asm and create a program sum_n_squared.asm that calculates the sum of squares of the first num natural numbers (num <= 100).
TIP: You will use the
eaxandedxregisters for multiplication to compute the squares (using themulinstruction). Therefore, you cannot easily use theeaxregister to store the sum of squares. To retain the sum of squares, you have two options:
- (easier) Use the
ebxregister to store the sum of squares.- (more complex) Before performing operations on the
eaxregister, save its value on the stack (using thepushinstruction), then perform the necessary operations, and finally restore the saved value (using thepopinstruction).NOTE: For verification, the sum of squares of the first 100 natural numbers is
338350.
Testing
To test the implementation, enter the tests/ directory and run:
make check
In case of a correct solution, you will get an output such as:
test_sum_100 ........................ passed ... 50
test_sum_squares_100 ........................ passed ... 50
========================================================================
Total: 100/100
If you’re having difficulties solving this exercise, go through this reading material.