CSC 205 - Computer Organization: Homework #2 - MIPS Assembly

Verified

Added on  2019/09/25

|6
|982
|1
Homework Assignment
AI Summary
This document presents a comprehensive solution to CSC 205 Homework #2, focusing on MIPS assembly language and SPIM programming. The assignment includes several problems that require students to translate C code into MIPS assembly, write SPIM programs, and analyze existing MIPS code. Specific tasks involve converting a C program statement to a two-operand MIPS format, analyzing a given MIPS program to determine its function and the values of registers, writing MIPS code for C statements involving arithmetic operations, conditional statements, and loops, and implementing procedures in MIPS assembly. The solutions include SPIM source code and the corresponding output, demonstrating the execution and results of the programs. The document addresses topics such as instruction formats, register usage, and the implementation of control flow structures within the MIPS architecture.
Document Page
CSC 205 Homework #2
DIRECTIONS: Answer all problems as directed. Problems where SPIM source code is requested will
NOT receive credit without the source code. All SPIM programs should not use any instructions not
covered in class and should not use any pseudo instructions. Points will be deducted for any use of pseudo
instructions.
1. In Chapter 2, the MIPS arithmetic instructions are described as having exactly three
operands. Suppose that a new MIPS processor was introduced to the market and it
used only two operands for its arithmetic instructions. Translate the following C
program statement into this new MIPS instruction format:
A = B + C + D ;
Like the examples in your text, you may assume that the first operand in a MIPS
arithmetic instruction receives the sum or difference.
How does this compare with the 3-operand MIPS arithmetic instruction in terms of
number of instructions and variable handling?
- 1 -
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
2. Shown below is a MIPS program. It takes an array of values as input and performs an
operation on them. Assume the following situations:
$S0 is initialized to point to the beginning of an array (list of items).
$T2 contains a count of the items in the array.
$T0 stores the result
start:
lw $t0,0($s0) # Format? (See problem 5)
lw $t2,count
addi $s0,$s0,4 # Format? (See problem 5)
addi $t2,$t2,-1
loop0:
lw $t1,0($s0)
slt $t3,$t0,$t1 # Format? (See problem 5)
beq $t3,$zero,rept # Format? (See problem 5)
add $t0,$zero,$t1
rept:
addi $s0,$s0,4
addi $t2,$t2,-1
beq $t2,$zero,quit
j loop0 # Format? (See problem 5)
quit:
Using this code, answer these questions:
1. If the array contains these values {1,5,9,3,7,12,18,4,10}, what will $T0 contain
when the program finishes?
2. What kind of operation is this program performing on the array?
- 2 -
Document Page
3. Using SPIM, write the MIPS instruction(s) for this C statement:
A = B - 210 ;
Turn in your source code and SPIM output showing your results.
4. Using SPIM, write the MIPS instruction(s) for this C statement:
if ( q != r)
r = r + 1 ;
else
q = r * 2 ;
Turn in your source code and SPIM output showing your results.
5. Using the MIPS program in problem 2, determine the instruction format for the
instructions marked with “# Format? (See problem 5)”. Include the decimal values
for each instruction field and provide your answers in the table below.
6. Using SPIM, write MIPS assembly code for the following C segment*:
while ( list[I] != key )
I += 1;
key = I;
Your code can only branch or jump once per pass of the loop. You may code as
many branch or jump instructions in the loop as you need, however. Note that a
branch that “falls through” (e.g. does not branch) on each pass doesn’t count
towards this requirement.
Assume $S1 = I (initially zero)
- 3 -
Document Page
$S2 = key
$S3 = base of the array list (array of MIPS words)
Hint: you can set $S3 using the pseudoinstruction { la $S3,list }.
Place this instruction before your first use of $S3.
Be sure you enable use of pseudoinstructions in SPIM, and DO
NOT use any other pseudoinstructions in your code!
You should also assume that the key is always found in the array (e.g. you do not
have to account for the key not being found).
Turn in your source code and SPIM output showing your results.
- 4 -
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7. Using SPIM, and the class instructional materials on procedures, write MIPS
assembly code for the following C segment:
int compare (int a, int b) {
if (sub(a, b) >= 0)
return 1;
else
return 0;
}
int sub (int a, int b) {
return a – b;
}
You may assume that all C integers are 32-bits (MIPS words).
You may pass parameters via MIPS registers. Follow MIPS register conventions for
parameter passing and general register usage.
Turn in your source code and SPIM output showing your results using test data.
- 5 -
Document Page
*Example of how to access an array in SPIM
.data
myList: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 # 10 element array
myListA: .word myList # get address of myList
.text
lw $t1,0($s3) # get list item
addi $s3,$s3,4 # increment list index
The LW instruction obtains an item from the list while the ADDI instruction advances the
pointer register $S3 to the next element of the array. Put these instructions in a loop and
you can “walk” through an entire array!
- 6 -
chevron_up_icon
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]