Starting from:
$35

$29

CS Creating and Running Simple MIPS Assembly Language Programs Solution


Part 1. (25 points) Accessing array elements in MIPS: Write a program to check if an array is a symmetric array.

    • Define an array and state its size in a separate variable and test if it is a symmetric array. Display array elements and print a message such as "The above array is symmetric". If not symmetric "The above array is not symmetric". For array declaration and for declaring its size you may use lines similar to the following. By changing these declarations you may use your program with different arrays.

    • array:  .word 1, 2, 1

    • arrsize: .word 3

Part 2. (25 points) Calculating an arithmetic expression in MIPS: Write a program to evaluate the

following expression given below (all numbers are integers): x= a * (b - c) % d

    • Read a, b, c, d.

    • Perform the computation and print the result. (%: modulo operation)

    • Provide a simple user interface for input and output.

3
Table 1. Explanation for divide and multiplication instructions.
Instruction
How it Works
Example



div $t1,$t2
Divides $t1 by $t2 then sets LO to quotient and HI to remainder (use mfhi to access HI, mflo to

access LO). Note that LO and HI are two registers used for multiplication and division.


mult $t1,$t2
Multiplies $t1 and $t2 and sets HI to high-order 32 bits, LO to low-order 32 bits of the product of

$t1 and $t2 (use mfhi to access hi, mflo to access lo)


mfhi $t1
Moves from HI register : Set $t1 to contents of HI
mflo $t1
Moves from LO register : Set $t1 to contents of LO

Part 3 & 4. Lab Work (50 points)

Part 3. Accessing array elements in MIPS: Write a program to find min, maximum, and average value of the elements of an array.

(25 points)

    • Define an array and state its size in a separate variable as you have done in Part 1.

    • Find minimum, maximum, and average value of the array elements. Use integer arithmetic.

    • Display array elements and results as follows

Memory Address    Array Element

Position (hex)    Value (int)

=============    ===========

....    ....

....    ....

Average: ...

Max: ...

Min: ...

Hint. syscall with the code value 34 is used for printing integer numbers as a hexadecimal number.

Remember that memory address are integer numbers. See the code segment below.

# Print the contents of $t0 as an hexadecimal number.

li    $v0, 34

add    $a0, $zero, $t0

syscall

Part 4. Using MIPS for Mathematical Calculations (25 Points)

Write a program that prompts the user for one or more integer input values, reads these values from the keyboard, and computes a mathematical formula such as (a different formula may be given by your TA):

A=(B*C+D/B -C )%B

When the computation is finished, the program should print the result along with an explanatory comment to the user via the display. Note that all is done using integer instructions (no floating point).


4
Part 5. Submit Your Code for MOSS Similarity Testing

    1. Submit your Lab Work MIPS codes for similarity testing to Moodle.

    2. You will upload one file. Use filename StudentID_FirstName_LastName_SecNo_LAB_LabNo.txt

    3. Only a NOTEPAD FILE (txt file) is accepted. No txt file upload means you get 0 from the lab. Please note that we have several students and efficiency is important.

    4. Even if you didn’t finish, or didn’t get the MIPS codes working, you must submit your code to the

Moodle Assignment for similarity checking.

    5. Your codes will be compared against all the other codes in the class, by the MOSS program, to determine how similar it is (as an indication of plagiarism). So be sure that the code you submit is code that you actually wrote yourself !

Part 6. Lab Policies

    1. Attendance is mandatory. The preliminary work is graded only if you submit your lab work with the observation of your TA.

    2. You can do the lab only in your section. Missing your section time and doing in another day is not allowed.

    3. Students will earn their own individual lab grade. The questions asked by the TA will have an effect on your individual lab score.

    4. Lab score will be reduced to 0 if the code is not submitted for similarity testing, or if it is plagiarized. MOSS-testing will be done, to determine similarity rates. Trivial changes to code will not hide plagiarism from MOSS—the algorithm is quite sophisticated and powerful. Please also note that obviously you should not use any program available on the web, or in a book, etc. since MOSS will find it. The use of the ideas we discussed in the classroom is not a problem.

    5. Your lab attendance is tracked by the Zoom system. Please also see lab policies no. 1 item.


























5
APPENDIX

SELF STUDY: ASSEMBLING AND DEBUGGING A PROGRAM


A. Examining the Controls and Options in Mars

    1. Open the MARS simulator. Take a few minutes to explore each of the windows (Edit & Execute; MARS Messages & Run I/O; Registers, Coproc 1 and Coproc 2). Look at each of the controls on the pull-down menus from the task bar at the top (File, Edit, Run, Settings, Tools, Help) and discuss what you think it does. Change the run speed to 1 instuction/sec using the slide bar at the top.

    2. Now, starting from the left, slowly put the mouse over each of the icons in the row across the top, to see the action that it will cause (but don’t click the icon). Determine which actions, represented by the icons, also can also be selected from a pull-down menu. Click the “?” icon (or choose it from the Help pull-down menu) and in the Help window that open, click each of the tabs and briefly look at the Help contents for that topic. Note that the MIPS tab offers a box with scroll bar, and 6 tabs of its own. Similarly, the MARS tab opens a window with 8 tabs. Be sure to look at each of these.

    3. On the top menu, Tools offers a list of tools in the pull-down menu. Open each of these and look at it briefly to begin to understand the range of additional capabilities of MARS

B. Using a Simple Program in Mars

    4. Load in Program1 (Generates "hello world"), using File > Open, or the appropriate icon button. In the Edit window, examine this program, and learn what it does. Try to understand how it does it.

    5. Assemble the program, using Run > Assemble , or the appropriate icon button. In the Execute window, examine the code portion of memory (called Text Segment) and the data portion of memory (called the Data Segment). Note the beginning address of each, and the contents of each. Knowing that

    2 hex characters represent one byte, and remembering that ASCII characters are each coded as one byte, determine which characters are stored in which locations in the Data Segment. Examine the initial values of the registers—which ones are non-zero? Make a note of their values. Read the messages in the MARS Messages window. Check the Run I/O window.

    6. Set the Run Speed bar to 1 instruction/second. Now run the assembled program, using Run > Go, or the appropriate icon button. What happens to the yellow highlight bar in the Text Segment during execution? What is written in the MARS Messages window? In the Run I/O window? Compare the final values of the non-zero registers—did you expect to find $1, $4, and $2 changed by the program? What is the final value of the $pc register?

    7. Now edit the Program1 so that it produces a different output: Hello <a different name> , and make it print out that name.



6
C. Finding and Fixing Errors in Mars

    8. Load in Program2 (Conversion program), assemble it, and run it, providing the necessary input from the keyboard. What does this program do? Examine the MIPS assembly program to understand how it does it.

    9. Edit the program by changing li $v0, 5 to li v0, 5 . Then assemble it and read the error message. Then fix the error and re-assemble it.

    10. Edit the program by changing li $v0, 5 to $li v0 . Then assemble it and read the error message. Then fix the error and re-assemble it. Now run the program, at 1 instr/sec, and make note of the value of $v0 after the 2nd syscall is completed.

    11. Edit the program by changing mul $t0, $v0, 9 to mul $t0, $t0, 9 . Then assemble it and run it, and explain why the output value is what it is. Then fix the error and re-assemble it and re-run it, to verify that Program2 again works correctly.

    12. Change the Run speed back up to the maximum. Assemble the program, and instead of hitting the normal Run button, instead hit the “Run 1 step at a time” icon (looks like >1) repeatedly, to single-step through the program. Notice that you can go slowly, examining the memory and registers after each step, or go quickly. This single-step mode is good for debugging.

































7

More products