Question: Homework 6 Problem 4: Working with Hmmm Functions [30 points; individual or pair] Similar to any high level programming languages such as Python, Hmmm assembly
Homework 6 Problem 4: Working with Hmmm Functions
[30 points; individual or pair]
Similar to any high level programming languages such as Python, Hmmm assembly supports the use of functions.
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, youd 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.
$ hmmmAssembler.py -f hw6pr4.hmmm ... message of successful compilation ... $ hmmmSimulator.py Enter debugging mode? n Enter number: 4 30 $ hmmmSimulator.py Enter debugging mode? n Enter number: 5 55 $ hmmmSimulator.py Enter debugging mode? n Enter number: 10 385 $ hmmmSimulator.py Enter debugging mode? n Enter number: 6 91
Because of the limited flexibility of Hmmm programming in line numbering, you probably should write you main program first, followed by the sumfunction, 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 k is 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 doesnt have to save the return address.
Because some register values may be changed within the function yet these values are needed in other places, e.g., your main program, you will have to save certain register values onto stack. To use the stack, you need to specify where in the Hmmm memory is your stack. Typically you can specify the stack address (or stack pointer) in a high memory address. For Hmmm, we only have 256 memory words, so you could specify the stack address in memory as, for example, 255, 180, or the like. In reality, we would use the instruction setn to set the initial stack address, the constant we can use with setn has a value range of [-128, 127]. So you probably want to use a value that is less than or equal to 127. (You can certainly use other instruction to set this value.) Saving and restoring these registers could be done either in your main program, or in the function itself. For clarity, and probably it is a bit easier, you should consider doing this part within your function.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
