Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 2: a) Translate the following C code to MIPS. Assume f, g, h, i and j a

ID: 3888316 • Letter: Q

Question

Question 2:

a) Translate the following C code to MIPS. Assume f, g, h, i and j are assigned to registers $1, $2, $3, $4, $5

f = g + h;
i = j - 3;
j = j & 0xFFFE;
g = h - j;

b) For the following two MIPS instructions, show the value of the opcode, rs rt rd and immediate fields as appropriate for that type of instruction (R- or I-type).

addi $7, $9, 4
add $18, $9, $0

c) Assume for the following instructions, variable A is assigned to register $18, B is assigned to $19, x is assigned to $7 and y is assigned to $9. Translate the three instructions into C code.

addi $7, $9, 4
add $18, $9, $0
add $19, $7, $18

Explanation / Answer

2(a).

add $1,$2,$3 // add contents of register $2 and $3 and store the result to $1

addi $4, $5, -3 // add immediate contents of register $5 with immediate value -3 and store the result to $4

andi $5,$5,0xFFFE // bit -wise and of contents of $5 and immediate avlue hex value FFFE and store the result back to $5

sub $2,$3,$5 // subtract the contents of register $3 and $5 and store the result to register $2

(b)

2. add $18, $9, $0 - It is R-type of instruction

(c)

C-code instruction: x=y+4 (x = $7, y=$9)

C-code instruction : A = y + 0 (A=$18, B=$9 and $0 is hard-wired to 0 i.e its value is always 0)

C-code instruction : B=x + A (B=$19, x=$7 , A=$18)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote