Homework 2

Transcription

Homework 2
Homework 2
CDA 3101: Fall 2014
Due Date: 10/8/2014
11:30 PM
Primary TA: Sebo Kim
You are not allowed to take or give help in completing this assignment. Submit the PDF version
of the submission in e-Learning (Sakai) before the deadline. Please do not submit your
handwritten solution (no scanned PDF). Prepare your solution in Microsoft Word and convert it
to PDF (File  Save As  PDF). Please include the following sentence on top of your
submission: “I have neither given nor received any unauthorized aid on this assignment”.
Total Points: 30 pts
1.
[3 points] Subtract 31 from 56 using 8-bit 2’s complement arithmetic. Give corresponding
binary number (Show major steps).
2.
[3 points] Assume that we want to design an instruction named “multiply” that multiplies
two 32-bit source registers and places the result in a 32-bit destination register. You can
assume that the product always fits in 32-bit destination register. Show a sequence of real
MIPS instructions (no pseudo instructions) to implement “multiply $3, $1, $2”. You cannot
use “mul” instruction.
3.
[3 points] Show the computation for “mul $t0, $s0, $s1” using $s0=0x00000005 and
$s1=0x00000061. Indicate what value will be stored in $t0 at the end of computation. Give
corresponding binary or hexadecimal number (Show major steps).
4.
[3 points] Consider the following C-like pseudo code where variable ‘c’ maps to register
$12 and the integer array ‘x’ begins at memory location 1,048,600 ten. Each element of
array is 4 bytes. Write the corresponding MIPS assembly code?
x[9] = x[6] + c
5.
[3 points] Consider the following C-like pseudo-code where variable i corresponds to
register $s1 and variable j corresponds to register $s2. Write the corresponding MIPS
assembly code?
if (j <= i) goto L;
6.
[3 points] The following program tries to copy words (4 bytes) from the address in register
$4 to the address in register $5, counting the number of words copied in register $2. The
program stops copying when it finds a word with value 0 (i.e., 32 0’s). You do not have to
preserve the contents of registers $3, $4, and $5. The terminating word (last word with 32
0’s) should be copied but not counted.
Loop:
lw
addi
sw
addi
addi
bne
$3,0($4)
$2,$2,1
$3,0($5)
$4,$4,1
$5,$5,1
$3,$0,Loop
# Read next word from source
# Increment count of words copied
# Write to destination
# Advance pointer to next source
# Advance pointer to next destination
# go to loop if # of words copied = zero
There are multiple bugs in this MIPS program. Fix them and turn in a bug-free version of
this MIPS assembly program.
7.
[3 points] Find the shortest sequence of MIPS machine instructions that will exchange
(swap) the contents of registers $5 and $6 without use of any other register (and not
creating overflow). Write the shortest sequence of MIPS machine instructions for these
statements. You cannot use any add, sub, mul or div instructions. Justify why your solution
will always work irrespective of the value stored in $5 and $s6.
swap $5 $6
8.
[3 points] Consider the following assembly code where the variables a, b, c, d and e are
assigned to registers $t0, $t1, $t2, $t3 and $t4, respectively. Assume that the base address of
arrays P and Q are in registers $t6 and $t7, respectively. Write the C or C++ or Java code
corresponding to the following MIPS assembly. Show your steps: i) write comments next to
each assembly instruction, and then ii) write the equivalent C or C++ or Java code.
addi $s0, $t6, 4
add $s1, $t6, $0
sw $s1, 0($s0)
lw $s0, 0($s0)
add $t0, $s1, $s0
9.
[3 points] Consider $s0 holds the value 0x00101001. What is the value of $s2 after the
following instructions? Show your steps: i) write comments next to each assembly
instruction, and then ii) compute the value of $s2.
ELSE:
DONE:
slt
bne
j
addi
$s2, $0, $s0
$s2, $0, ELSE
DONE
$s2, $s2, 2
10. [3 points] Change the following loop into C or C++ or Java code. Assume that the C-level
integer k is held in register $s1, $t2 holds the C-level integer called result, and $t0 holds the
base address of the integer array X. Show your steps: i) write comments next to each
assembly instruction, and then ii) write the equivalent C or C++ or Java code.
THERE:
addi
lw
add
addi
addi
slti
bne
$s1, $0, $0
$t1, 0($t0)
$t2, $t2, $t1
$t0, $t0, 4
$s1, $s1, 1
$s2, $s1, 100
$s2, $0, THERE