0% found this document useful (0 votes)
268 views

Assignment 2

This document contains 10 questions regarding translating between C code, MIPS assembly code, and machine language instructions. The questions cover a range of concepts including instruction types, register usage, memory addressing, and endian formats. Sample code snippets are provided involving arithmetic operations, array indexing, loops, conditional statements, and function calls that require translation between the different representation levels.

Uploaded by

faisal rehman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
268 views

Assignment 2

This document contains 10 questions regarding translating between C code, MIPS assembly code, and machine language instructions. The questions cover a range of concepts including instruction types, register usage, memory addressing, and endian formats. Sample code snippets are provided involving arithmetic operations, array indexing, loops, conditional statements, and function calls that require translation between the different representation levels.

Uploaded by

faisal rehman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment# 3

Computer Architecture
Chapter 2
Instruction Set Architecture
Due Date: 2-05-2019
Question # 1 (C to MIPS)

C to MIPS. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3,
and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and
$s7, respectively.

f = g + A[B[4]-B[3]];

For the C statement above, what is the corresponding MIPS assembly code?

Question # 2
while( A[i]== B[i])
i=i+1;

Convert the above C code to MIPS assembly. Base address of A and B are in $s0 and $s1
respectively and variable “i” is assigned to $s2.

Question # 3
For the following C statement, what is the corresponding MIPS assembly code? Assume that the
variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume
that the base address of the arrays A and B are in registers $s6 and $s7, respectively.
B[8] = A[i−j];

Question #4
Translate the following C code to MIPS assembly code. Use a minimum number of instructions.
Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also,
assume that register $s2 holds the base address of the array D.
for(i=0; i<a; i++)
for(j=0; j<b; j++)
D[4*j] = i + j;
Question # 5
Translate the following C code to MIPS assembly code. Use a minimum number of instructions.
Assume that the values of num1,num2 and result are in registers $a0, $a1, and $v0, a respectively

 int max(int num1, int num2) {


 int result;
 if (num1 > num2)
 result = num1;
 else
 result = num2;
 return result;
 }

Question # 6 (MIPS to C)
MIPS to C. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3,
and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and
$s7, respectively. Convert the following program into C.

MIPS Code:

 sll $t0, $s0, 2


 add $t0, $s6, $t0
 sll $t1, $s1, 2
 add $t1, $s7, $t1
 lw $s0, 0($t0)
 addi $t2, $t0, 4
 lw $t0, 0($t2)
 add $t0, $t0, $s0
 sw $t0, 0($t1)

Question # 7
Translate the following MIPS code to C. Assume that the variables f, g, h, i, and j are assigned to
registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A
and B are in registers $s6 and $s7, respectively. MIPS Code:

 addi $t0, $s6, 4


 add $t1, $s6, $0
 sw $t1, 0($t0)
 lw $t0, 0($t0)
 add $s0, $t1, $t0
Question # 8
Consider the following MIPS loop:
LOOP: slt $t2, $0, $t1
beq $t2, $0, DONE
subi $t1, $t1, 1
addi $s2, $s2, 2
j LOOP
DONE:
1. Assume that the register $t1 is initialized to the value 10. What is the value in register $s2
assuming $s2 is initially zero?
2. For each of the loops above, write the equivalent C code routine. Assume that the registers
$s1, $s2, $t1, and $t2 are integers A, B, i, and temp, respectively.
3. For the loops written in MIPS assembly above, assume that the register $t1 is initialized to
the value N. How many MIPS instructions are executed?

Question # 9 (MIPS to Machine)


For each MIPS instruction, show the value of the opcode (OP), source register (RS), and target
register (RT) fields. For the I-type instructions, show the value of the immediate field, and for the
R-type instructions, show the value of the destination register (RD) field.

 sll $t0, $s0, 2


 sub $t0, $s6, $t0
 lw $s0, 0($t0)
 addi $t2, $t0, 4
 lw $t0, 12($t2)
 add $t0, $t0, $s0
 sw $t0, 16($t1)

Question # 10
1. Provide the type and assembly language instruction for the following binary value:
0000 0010 0001 0000 1000 0000 0010 0000two

2. Provide the type and hexadecimal representation of following instruction: sw $t1, 32($t2)
3. Provide the type, assembly language instruction, and binary representation of instruction
described by the following MIPS fields:
op=0, rs=3, rt=2, rd=3, shamt=0, funct=34
4. Provide the type, assembly language instruction, and binary representation of instruction
described by the following MIPS fields:
op=0x23, rs=1, rt=2, const=0x4
5. Show how the value 0xabcdef12 would be arranged in memory of a little-endian and a
big-endian machine. Assume the data is stored starting at address 0.

You might also like