Could someone answer these questions or even few parts of it? It uses MIPS assem
ID: 3713512 • Letter: C
Question
Could someone answer these questions or even few parts of it? It uses MIPS assembly language.
Thanks!
Question 1
~~~~~~~~~~
Using Appendix A, translate each of the following pseudocode expressions into efficient
MIPS assembly language. You may test assemble your code using MIPSym, but it is only
required if your instructor specifically requests it.
(a) t9 = t1 + t2 - t3;
(b) s3 = s1 - s2 / s3;
(c) sp = sp - 32;
(d) s1 = s1 * a0;
(e) cin >> t3;
(f) a1 = &array;
(g) for (t0 = 'm'; t0 <= 'x'; t0++) *a0++ = t0;
(h) *(a1 + 4) = 0xABCD;
(i) cout << "Some Message";
(j) while ( t4 != 0) { s0 = s0+t4; t2 = t2+4; t4 = *t2 };
(k) do {t3 *= t1; t1--;} while (t1 > 0);
(l) if (t0 >= 0) t1 = 0 - t0 else t1 = t0;
(m) for ( t3 = a1; t3 < a2; t3++) v0 += t3;
(n) t1 = *a1; //assume a1 is an int pointer;
(o) t5 = 2147483647 - 2147483648;
(p) s0 = -1 * s0;
(q) cout << t2;
(r) s2 = sqrt(s0*s0 + 212) / a3;
(s) s1 = t0 / (s0 - 0xFF);
(t) s4 = s4 * 16;
Question 2
~~~~~~~~~~
Analyze the assembly language code that you developed for each of the above
pseudocode expressions and calculate the number of clock cycles required to fetch and
execute the code corresponding to each expression. Assume it takes one clock cycle to
fetch and execute every instruction except multiply, which requires 32 clock cycles,
divide, which requires 38 clock cycles, and syscall instructions, which will require a
fixed overhead, oh, plus a per character time of 6 clock cycles. User input time can be
included as ui.
Question 3
~~~~~~~~~~
Show how the following expression can be evaluated in MIPS assembly language, without
modifying the contents of the “s” registers:
t0 = (s1 - s0/s4) * s2;
Question 4
~~~~~~~~~~
Show how the following pseudocode expression can be efficiently evaluated (meaning
fewest clock cycles) in MIPS assembly language. Assume normal operator precedence.
t0 = s0/4 - 2*s1 + s2;
Explanation / Answer
# Question 1:-
#a) t9 = t1+t2=t3
sub $t2,$t2,$t3
add $t9,$t1,$t1
#b)s3 = s1-s2/s3
div $s2,$s2,$s3
sub $s3,$s1,$s2
#c)sp = sp -32
subi $sp,$sp,32
#d)s1 = s1*a0
mul $s1,$s1,$a0
#e)cin>>t3
li $v0,5 #input integer
syscall
la $t3,($v0)
#f)a1 = $array
la $a1,array
#h)*(a1 + 4) = 0xABCD;
li $t1,ABCD
sw $t1,4($a1)
#i)cout << "Some Message";
str1: .asciiz "Some Message"
la $a0,str1 #print string str1
li $v0,4
syscall
#j)while ( t4 != 0) { s0 = s0+t4; t2 = t2+4; t4 = *t2 };
loop:
beqz $t4,loopexit
add $s0,$s0,$t4
addi $t2,$t2,4
mul $t4,$t4,$t2
j loop
#k)do {t3 *= t1; t1--;} while (t1 > 0);
loop:
mul $t3,$t3,$t1
subi $t1,$t1,1
blt $t1,0,loopexit
j loop
#l) if (t0 >= 0) t1 = 0 - t0 else t1 = t0;
bge $t0,0,loop #if condition satisfied code junp to "loop"
move $t1,$t0
loop:
subi $t1,-$t0,-0
#n) t1 = *a1; //assume a1 is an int pointer;
lw $t1,($a1)
#o) t5 = 2147483647 - 2147483648;
addi $t5,$t5,2147483647
subi $t5,$t5,2147483648
#p)s0 = -1*s0
mul $s0,$s0,-1
#q) cout<<t2
li $v0,1 # print $t2(integer)
move $a0,$t2
syscall
#t) s4=s4+16
mul $s4,$s4,16
#Question 3
#t0 = (s1 - s0/s4) * s2;
div $t0,$s0,$s4
sub $t0,$s1,$t0
mul $t0,$t0,$s2