CS 2504 Homework 2: Basic MIPS Assembler and Assembly Code Optimization, Assignments of Computer Science

Information about homework 2 for the cs 2504 intro computer organization course. It includes instructions for submitting answers, three programming questions related to the mips assembler and assembly code optimization, and a short mips assembly program with an equivalent c/c++ program. The document also includes a buggy mips assembly code for copying words of memory and a request to describe the logical errors.

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-0o2-1
koofers-user-0o2-1 🇺🇸

8 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2504 Intro Computer Organization Homework 2: Basic MIPS
Last modified: 9/6/2006 10:05 AM 1
Prepare your answers to the following questions in a plain text file. Submit your file to the Curator system by the posted
deadline for this assignment. No late submissions will be accepted.
You will submit your answers to the Curator System (www.cs.vt.edu/curator) under the heading HW2.
1. [25 points] Consider adding support for the following pseudo-instruction to the MIPS assembler:
asum $rd, $rl, $rr # place sum of |$rl| and |$rr| in $rd;
# the contents of $rl and $rr are not changed
This will require that the assembler replace an occurrence of the pseudo-instruction above with a sequence of one or
more native MIPS instructions. What instruction(s) might the assembler use for the replacement? (Note: the native
MIPS absolute value instructions abs.f and abs.d are not suitable for integer values.) You may use any of the
temp registers ($tx) as needed.
2. [25 points] In section 2.6 of P&H, a MIPS assembly translation is given for the following segment of C code:
while ( save[i] == k )
i += 1;
However, the given translation uses both a conditional branch and an unconditional branch operation each time
through the loop. Only poor compilers, and poor MIPS assembly programmers, would write something that
inefficient. Rewrite the assembly code so that it uses at most one branch or jump operation each time through the loop.
3. [25 points] Consider the following short MIPS assembly program:
.data
x: .word 12
y: .word 17
z: .word 0
.text
main:
lw $s0, x
lw $s1, y
blt $s0, $s1, less
add $s0, $s0, $s1
sw $s0, z
j finish
less: sub $s1, $s1, $s0
sw $s1, z
finish:
li $v0, 10 # exits program
syscall
Write an equivalent C/C++ program.
pf2

Partial preview of the text

Download CS 2504 Homework 2: Basic MIPS Assembler and Assembly Code Optimization and more Assignments Computer Science in PDF only on Docsity!

CS 2504 Intro Computer Organization Homework 2: Basic MIPS

Last modified: 9/6/2006 10:05 AM 1

Prepare your answers to the following questions in a plain text file. Submit your file to the Curator system by the posted deadline for this assignment. No late submissions will be accepted.

You will submit your answers to the Curator System (www.cs.vt.edu/curator) under the heading HW2.

1. [25 points] Consider adding support for the following pseudo-instruction to the MIPS assembler:

asum $rd, $rl, $rr # place sum of |$rl| and |$rr| in $rd;

the contents of $rl and $rr are not changed

This will require that the assembler replace an occurrence of the pseudo-instruction above with a sequence of one or more native MIPS instructions. What instruction(s) might the assembler use for the replacement? (Note: the native MIPS absolute value instructions abs.f and abs.d are not suitable for integer values.) You may use any of the temp registers ($tx) as needed.

2. [25 points] In section 2.6 of P&H, a MIPS assembly translation is given for the following segment of C code:

while ( save[i] == k ) i += 1;

However, the given translation uses both a conditional branch and an unconditional branch operation each time through the loop. Only poor compilers, and poor MIPS assembly programmers, would write something that inefficient. Rewrite the assembly code so that it uses at most one branch or jump operation each time through the loop.

3. [25 points] Consider the following short MIPS assembly program:

.data x: .word 12 y: .word 17 z: .word 0

.text main: lw $s0, x lw $s1, y

blt $s0, $s1, less add $s0, $s0, $s sw $s0, z j finish

less: sub $s1, $s1, $s sw $s1, z

finish: li $v0, 10 # exits program syscall

Write an equivalent C/C++ program.

CS 2504 Intro Computer Organization Homework 2: Basic MIPS

Last modified: 9/6/2006 10:05 AM 2

4. [25 points] The MIPS assembly program below is intended to copy words of memory from offsets beginning at the address source to words of memory beginning at the address target. The number of words the program should copy is specified by the value at the address size.

.data # 1 source: .word 42, 17, 23, 93, 46, 79, 88, 30 # 2 target: .space 8 # 3 size: .word 8 # 4

.text # 5 main: move $s0, $zero # 6 la $t0, size # 7 move $t1, $zero # 8

loop: ble $t0, $t1, done # 9 lw $s1, source($s0) # 10 sw $s1, target($s0) # 11 addi $t1, $t1, 1 # 12 addi $s0, $s0, 1 # 13 b main # 14 done: li $v0, 10 # 15 syscall # 16

Unfortunately, although it is syntactically correct, the given code is somewhat buggy. Clearly describe all the logical errors in the given assembly language code. You should refer to the line numbers of the relevant statements, and describe how the statement should be changed to eliminate the corresponding logical error.