Download Computer Systems HW #1 Solution

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Systems HW #1 Solution
Due: Friday, January 25 (3 PM in ITT 305 mailbox or under my office door, ITT 313)
Common MIPS Instructions (and psuedo-instructions)
Type of Instruction
Memory Access
(Load and Store)
Move
Load Address
Arithmetic Instruction
(reg. operands only)
Arithmetic with Immediates
(last operand must be an integer)
Conditional Branch
Unconditional Branch
MIPS
Assembly Language
lw $4, Mem
sw $4, Mem
lw $4, 16($3)
sw $4, 16($3)
move $4, $2
li $4, 100
la $5, mem
add $4, $2, $3
mul $10, $12, $8
sub $4, $2, $3
addi $4, $2, 100
mul $4, $2, 100
bgt $4, $2, LABEL
(bge, blt, ble, beq, bne)
j LABEL
Register Transfer Language
Description
$4 [Mem]
Mem $4
$4 [Mem at address in $3 + 16]
[Mem at address in $3 + 16] $4
$4 $2
$4 100
$4 load address of mem
$4 $2 + $3
$10 $12 * $8 (32-bit product)
$4 $2 - $3
$4 $2 + 100
$4 $2 * 100
(32-bit product)
Branch to LABEL if $4 > $2
Always Branch to LABEL
sumPos = 0;
sumNeg = 0;
for i = 0 to length-1 do
if numbers[i] < 0 then
sumNeg = sumNeg + numbers[i]
else
sumPos = sumPos + numbers[i]
end if
end for
1. Write MIPS Assembly Language code for the above algorithm that sums the array's elements.
numbers:
length:
sumPos:
sumNeg:
.data
.word 20, 30, 10, 40, 50, 60, 30, 25, 10, 5
.word 10
.word 0
.word 0
.text
.globl main
main:
# hw1q1.s Solution to HW #1 Question #1.
# Implement the algorithm:
# sumPos = 0;
# sumNeg = 0;
# for i = 0 to length-1 do
#
if numbers[i] < 0 then
#
sumNeg = sumNeg + numbers[i]
#
else
#
sumPos = sumPos + numbers[i]
#
end if
# end for
numbers:
length:
sumPos:
sumNeg:
.data
.word 20, 30, 10, 40, 50, 60, 30, 25, 10, 5
.word 10
.word 0
.word 0
.text
.globl main
main:
li $5, 0
li $6, 0
la $7, numbers
for_initialize:
li $8, 0
lw $9, length
for_loop:
bge $8, $9, end_for
if:
lw $10, 0($7)
bge $10, 0, else
then: add $6, $6, $10
j end_if
else:
add $5, $5, $10
end_if:
addi $8, $8, 1
addi $7, $7, 4
j for_loop
end_for:
sw $5, sumPos
sw $6, sumNeg
li $v0, 10
syscall
# Use $5 for sumPos
# Use $6 for sumNeg
# Use $7 for address of number[i]
# Use $8 for i
# Use $9 for length
# Load numbers[i] into $10
# increment i
# move pointer to numbers[i] by 4 (size of a word)
2. Compare zero-, one-, two-, three-address, and the load & store machines by writing programs to compute
X = A * B + C * D;
Y = (A + B) / (X - C );
for each of the five machines. The programs are as follows:
3 Address
2 Address
MUL X, A, B
MUL T, C, D
ADD X, X, T
MOVE X, A
MUL X, B
MOVE T, C
MUL T, D
ADD X, T
ADD Y, A, B
SUB T, X, C
DIV Y, Y, T
MOVE Y, A
ADD Y, B
MOVE T, X
SUB T, C
DIV Y, T
1 Address
(Accumulator
machine)
LOAD C
MUL D
STORE X
LOAD A
MUL B
ADD X
STORE X
SUB C
STORE Y
LOAD A
ADD B
DIV Y
STORE Y
0 Address
(Stack machine)
Load & Store
Machine
PUSH A
PUSH B
MUL
PUSH C
PUSH D
MUL
ADD
POP X
PUSH A
PUSH B
ADD
PUSH X
PUSH C
SUB
DIV
POP Y
LOAD R1, A
LOAD R2, B
MUL R3, R1, R2
LOAD R4, C
LOAD R5, D
MUL R5, R4, R5
ADD R5, R3, R5
STORE R5, X
ADD R3, R1, R2
SUB R5, R5, R4
DIV R3, R3, R5
STORE R3, Y
3. Assume 8-bit opcodes, 32-bit absolute addressing, 5-bit register numbers, and 32-bit operands. Compute the
number of bits needed in programs from question 2 by completing the following table.
3 Address
2 Address
1 Address
0 Address
Load & Store
13 * (8+32)
10 * (8+32)
Number of bits
6 * (8+3*32)
10 * (8+2*32)
6 * (8+5+32)
needed to store the
= 624
= 720
= 520
+6*8
+ 6 * (8+3*5)
program
= 448
= 408
Number of bits of
6 * (3*32)
4 * (2*32)
13 * 32
10 * 32
6 * 32
data transferred to
= 576
+ 6 * (3*32)
= 416
= 320
= 192
and from memory
= 742
Total number of bits
read and written
1,200
1,462
936
768
600
while the program
executes