Question: 7. Learning Objective: To write MIPS32 assembly language instructions which perform arithmetic. To learn how a function is called, arguments are passed by value, and




7. Learning Objective: To write MIPS32 assembly language instructions which perform arithmetic. To learn how a function is called, arguments are passed by value, and how the function returns a value. Name your source code file poly3.s. For this exercise you will write a complete MIPS32 assembly language program which prompts the user to enter the three integer coefficients of a third degree integer polynomial equation (a cubic equation) of the form p3(x)=c3x3+c2x2+c1x+c0 and a value for x. It will then evaluate the polynomial at that value of x and print the result. Note that x and the coefficients will all be integers because I do not cover how to write assembly language code using floating point (its complicated and I feel confident that if you can write these programs using integers, then if you need to write assembly language code using floating point someday, that you will have no problem). The behavior of the program is illustrated by this sample run and output where user input is shown in bold: Enter c3? 37 Enter c2? 148 Enter c1? 120 Enter co? 47 Enter x?13 p(x)=37x3+148x2+120x+47atx=13is107908 Note that to keep this program simpler than it would be otherwise, when we print p(x), we are not going to properly handle the signs of the coefficients or cases when they are 0,1, or 1 because that would require some lengthy if and if-else statements. Instead, when you output the polynomial, just print the value of c3 followed by the string " x3+ ", then print the value of c2 followed by " x2+ ", then print c1 followed by " x+ ", next print co followed by " at x= ", and finally print the remainder of the output. We will write this program without the afore mentioned enhancements so we can avoid if and if-else statements. Although a good software design for this program would involve more than just a main() function, we will also forgo defining and using functions. We may revisit this exercise after we have had time to learn about if and if-else statements as well as functions. Note 1: According to the standard arithmetic operator precedence rules, exponentiation has higher precedence than the multiplicative operators (multiply, divide, modulus), which have higher precedence than the additive operators (addition, subtraction), which have higher precedence than the assignment operator. Operators which are at the same precedence level are evaluated in a left-to-right manner. Consequently, the order in which the operations to evaluate p3(x) should be performed is: (1) calculate x3; (2) calculate x2 (which in practice you should actually perform this operation before calculating x3 because in order to calculate x3, you need to first multiply x2 by x ). That completes the exponentiation operators, so next is multiplication: (3) calculate c3x3;(4)c2x2; and (5)c1x. That completes the multiplicative operators, so next is addition: (5) calculate c3x3+c2x2;(6) calculate the result from (5)+c1x;(7) calculate the result from (6)+c0; and finally, ( 8 ) make sure to assign the final result from (7) to the $v0 register because evalpoly() must return the result in $v0 by convention. The end result will be five multiplication instructions and three additions. Note 2: This note is informational and is included for educational completeness. If you wish, you may write your code using this method, but you do not have to. At the machine language level, multiplication instructions commonly require significantly more time to execute than addition instructions, so anytime we (or an optimizing compiler) can reduce the number of multiplications, that is a good thing. It is possible to simplify a polynomial equation using Horner's Rule. Using Horner's method, our rewritten equation is p3(x)=c0+x(c1+x(c2+xc3)), which would result in an assembly language implementation using these operations, in this order: (1) multiply x by c1; (2) add c2 to the result from (1); (3) multiply x by the result from (2); (4) add c1 to the result from (3); (5) multiply x by the result from (4); and (6) add coto the result from (5). Note that the number of required multiplications and additions are three and three, and that we have reduced the number of multiplications and instructions from the method described in Note 1 by two. Eliminating multiplication, division, and modulus instructions is always a good thing. 1. We will discuss the high-level pseudocode for the exercise in the lecture and I will help you assist you in writing the program. 2. Study the assembly language source code files listed in the course notes and posted on the course website. Format your code in a similar manner, i.e., an assembly language line containing an instruction consists of four columns of mostly-optional stuff: column 1 is aligned with the left margin and is reserved for an optional label; column 2 is indented from column 1 by around 4-8 spaces and is reserved for instruction mnemonics; column 3 is indented from column 2 and reserved for optional operands; column four is indented from column 3 and is reserved for optional comments. How many spaces or tabs you indent is up to you, the important thing is to line things up in columns and be consistent. 3. Assembly language code is very difficult to read and understand without considerable study, although copious wellwritten and descriptive comments can be of much help. For this reason, you shall write a good descriptive comment for just about, if not every, instruction. Some of credit for this exercise will be awarded based on how well your code is commented. 4. You shall not use any non-standard instructions and pseudo instructions which MARS supports, but you may use the pseudo instructions we discussed in $2.3.8 of the Ch. 2 lecture notes. 5. Your program must properly terminate by calling the SysExit() system call as the last instruction in main(). If you do not, then MARS will load PC with 0x0000_0000 and at the beginning of the next simulated clock cycle, when PC is sent to the memory to retrieve the word at the address in PC, your program will crash with an exception because 000000000 is not with the region of memory allocated for the .text section. 6. Your program must contain a header comment block, as shown below, although how you format it is mostly up to you. Please include the name of the source code file, a description explaining what the program does, and author information. If you work with a partner, then be sure to include both partners' author information. 6. Your program must contain a header comment block, as shown below, although how you format it is mostly up to you. Please include the name of the source code file, a description explaining what the program does, and author information. If you work with a partner, then be sure to include both partners' author information. \# FILE: poly3.s \# \# DESCRIPTION \# Describe the program's behavior here. \# \# COURSE INFORMATION \# CSE230, Summer 2022, Homework Assignment 1, Exercise 7 \# \# AUTHOR \# your-name (your-email-addr) \# your-partners-name (your-partners-email-addr)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
