Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in the Hmmm assembler: In this exercise, you are to write a function that comput

ID: 3731533 • Letter: I

Question

in the Hmmm assembler:

In this exercise, you are to write a function that computes the sum of integers from 1^2 through n^2. Within the sum function, you’d have to call another function square which you will write. The main reason to have the function sum to call another function (both are relatively simple) is to force you to think about and implement mechanisms that save some register values such as the function return address and other registers that have to be saved in between function calls.

When completed successfully, the behavior of your program should look similar to the following.

Because of the limited flexibility of Hmmm programming in line numbering, you probably should write you “main” program first, followed by the sum function, then the square function. The main program should read an integer, n. It then calls the function sum to compute 1^2 + 2^2 + … + n^2. Finally the main program should print out the result.

Your sum function should just loop 1 through n, inclusive, where n is the given parameter. At each step of the loop, call another small function square which simply computes and returns the value of k * k where kis the given parameter, i.e., the current loop index. When the loop is completed, return the result through a register to the main program.

Before programming, you need to think through how you may use the registers. For example, which register is used to store n, which for result. In addition, when making function calls, you will have to save the return address register (the one you use for calln rx, n), and any registers that may be changed in the function. Write down as a section of comments in your main program for register use. Also do the same within the function sum. The function square is a “leaf” function, so it doesn’t have to save the return address.

Explanation / Answer

I am writing python code to implement above solution.

def main():

    stringval = read("Enter debugging mode? ")

    n = read("Enter number: ")

    res = sum(n)

    print(res)

def sum( n ):

     sum = 0

     for i in range(n):

         sum + = squareFunction(i)

     return sum

def squareFunction( k ):    

     return k*k

main() # calling main function