Sum of first N natural numbers squared
You will solve this exercise starting from the sum_n.asm
file located in the drills/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_square.asm
that calculates the sum of squares of the first num
natural numbers (num
<= 100).
TIP: You will use the
eax
andedx
registers for multiplication to compute the squares (using themul
instruction). Therefore, you cannot easily use theeax
register to store the sum of squares. To retain the sum of squares, you have two options:
- (easier) Use the
ebx
register to store the sum of squares.- (more complex) Before performing operations on the
eax
register, save its value on the stack (using thepush
instruction), then perform the necessary operations, and finally restore the saved value (using thepop
instruction).NOTE: For verification, the sum of squares of the first 100 natural numbers is
338350
.
If you're having difficulties solving this exercise, go through this reading material.