You are on page 1of 4

Homework 3

Ex13:
1. Begin function f with input parameter k.
2. Declare an integer array v of size 100, and initialize variables i and s to 0.
3. For loop from i = 0 to k with a step of 2.
4. Within the loop, set v[i] equal to the result of calling function g with input parameter (k + 2).
5. Another for loop from i = 0 to k with a step of 2.
6. Within the second loop, increment s by v[i].
7. Return s as the output of the function f.
8. Define function g with input parameter k.
9. If k is even, return k squared plus 2.
10. Else, return k times -1.
f: # begin function f with input parameter k
addi $sp, $sp, -112 # allocate space on the stack for the array v and variables i and s
sw $ra, 108($sp) # save return address on the stack
sw $s0, 104($sp) # save $s0 on the stack
sw $s1, 100($sp) # save $s1 on the stack
li $s0, 0 # initialize i to 0
li $s1, 0 # initialize s to 0
forLoop1:
blt $s0, $a0, loop1Body # for loop from i = 0 to k with a step of 2
j done1 # exit the loop when i >= k
loop1Body:
addi $sp, $sp, -4 # allocate space on the stack for v[i]
add $t0, $a0, 2 # k + 2
jal g # call function g with input parameter (k + 2)
sw $v0, ($sp) # store the result in v[i]
addi $s0, $s0, 2 # increment i by 2
j forLoop1 # repeat the loop
forLoop2:
blt $s0, $a0, loop2Body # another for loop from i = 0 to k with a step of 2
j done2 # exit the loop when i >= k
loop2Body:
lw $t0, ($sp) # load v[i] into $t0
add $s1, $s1, $t0 # increment s by v[i]
addi $sp, $sp, 4 # deallocate space for v[i]
addi $s0, $s0, 2 # increment i by 2
j forLoop2 # repeat the loop
done2:
lw $ra, 108($sp) # restore return address from the stack
lw $s0, 104($sp) # restore $s0 from the stack
lw $s1, 100($sp) # restore

Ex14:
The instruction format for ADDV R1, R2, M is:
OpCode (6 bits) | R1 (3 bits) | R2 (3 bits) | Memory Address (4
bits).

Ex15:
a) The memory space addressable is 2^32 bytes.
b) The instruction format includes opcode and two 16-bit
address fields.
c) "lw $t0, 0(addr1) \n lw $t1, 0(addr2) \n sw $t0, 0(addr2) \n
sw $t1, 0(addr1)"
d) The range of addresses that can be addressed by addr1 and
addr2 is from 0 to (2^16-1) bytes.
Ex16:
a) One-word instruction format: opcode (6 bits) + operand(s)
(26 bits) + addressing mode (4 bits).
b) Maximum displacement for base-register addressing is +/-
2^9 - 1.
c) Range of addresses for unconditional branch instructions is 0
to 2^26 - 1.
d) This computer follows RISC model with register-register
execution and simple instruction format.

Homework 4:
Ex4:
a. Total time without pipelining = 5 * 10 = 50 cycles
b. Total time with pipelining = 5 + 220 + 170 + 220 + 150 + 150 =
915 ps
c. Total time with data dependency = 5 + 220 + 170 + 440 + 150
+ 150 = 1135 ps
d. Percentage improvement: (a - b)/a * 100 = 98.3%, (a - c)/a *
100 = 95.3%.

You might also like