PA1: Add all positive integers in a given array and print the sum. A skeleton code is given and can be downloaded here (array_skeleton.mas). |
PA2: Multiply two integers entered from keyboard and print the product. As MARIE architecture does not have multiplication instruction, we have to use addition to compute multiplication. A formula for this computation is given as below. |
When B is a non-negative integer, then
(2)
The following C++ code multiplies of two integers by using the formular (2). It gets two input integers X and Y from the keyboard. Then compute the absolute value of Y and save it in Z. Next, compute the product of X and Z and save it in C by using a loop based on formular (2). Next, if Y < 0, then the final product should be -C. Finally print result. |
int main(){ int X, Y, Z, C; cout << "Please enter two integers:" << endl; cin >> X >> Y; // Z = absolute value of Y if (Y < 0) Z
= -Y; else Z
= Y; // C = product of X and Z C
= 0; while (Z>0) { C
= C + X; Z
= Z - 1; } // If Y < 0, then C = -C if (Y < 0) C
= -C; cout << C << endl; // Print result return 0; } |
PA2.A: Your assignment is to convert the above C++ code into a MARIE assembly language code. A skeleton code is given here (MultLoopSkeleton.mas). You just need to fill in a part of code that computes the product of X and Z by using a while loop. |
The following C++ code shows to compute multiplication of two integers by using the formular (2). It is the same with the above C++ code except it uses a function to compute the product of X and Z. |
int main(){ int X, Y, Z, C; cout << "Please enter two integers:" << endl; cin >> X >> Y; // Z = absolute value of Y if (Y < 0) Z
= -Y; else Z
= Y; C
= Mult(X,
Z); // If Y < 0, then C = -C if (Y < 0) C
= -C; cout << C << endl; return 0; |
// Return the product of A and
B, // where A is an integer and // B is a non-negative integer int Mult(int A, int B) { int P; P
= 0; while (B > 0) { P
= P + A; B
= B - 1; } return P; } |
PA2.B: Your assignment is to convert the above C++ code into a MARIE assembly language code. A skeleton code is given here (MultFuncSkeleton.mas). You just need to fill in a part of code that computes the product of X and Z by using a function. That is to write a subroutine called Mult. Note that you need to use stacks that will hold the function’s inputs and output as well as the return address. MarieSimR should be used. |
PA2.C: (Bonus) Like in PA2.B, but you need to implement the subroutine Mult as a recursive function. You can use the same skeleton file in PA2.B and then rename it as MultRecFuncSkeleton.mas. Note that MarieSimR must be used. A C++ recursive function Mult is shown as below for your reference. |
// Recursive function // Return the product of A and
B, // where A is an integer and B
is a non-negative integer int Mult(int A, int B) { int P; if (B == 0) P
= 0; else P
= A + Mult(A, B-1); return P; } |
Please
submit your MARIE source codes in Canvas by the due day.