Beruflich Dokumente
Kultur Dokumente
EE/CS-320
Page 1 of 11
EE/CS-320
Use registers $t0, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $s0, $s1, $s2, $s3, $s4, $s5, $s6 and
$s7 to represent variables A, B, C, D etc.
Whenever you encounter a program with a procedure (function) call, you must ensure that
registers are properly preserved across the procedure call according to the conventions
discussed in class. Also use the input argument registers and return value registers as
discussed in class.
Collaboration with other students is strictly forbidden, stern disciplinary action will be
taken against anybody found guilty. We will be testing your codes for plagiarism.
Question 1: The following program finds the occurrences of a number in an array. Write the
equivalent MIPS Assembly Language code.
C++ Code:
int main( )
{
int array1[5] = {8, 4, 2, 9, 10};
int array2[5] = {3, 4, 5, 6, 7};
for (int i = 4; i >= 0; i --)
{
array2[ i ] = array1[ i ] + array2[ i ];
}
return 0;
}
Question 2: Write the equivalent MIPS Assembly Language code for the following C++
Code. (Remember, you cannot use any Pseudo-instructions other than la)
C++ Code:
int main( )
{
int array[10] = {2, 4, 5, 3, 9, 1, 10, 12, 13, 20};
for (int i = 0; i < 10; i++)
{
array[ i ] = ABS(array[ i ]);
}
return 0;
}
Page 2 of 11
EE/CS-320
C++ Code:
int main( )
{
int marks[10] = {50, 65, 44, 78, 87, 90, 49, 44, 80, 95};
int pass = 0, fail = 0, counter=0;
while (counter < 10)
{
if(marks[counter] >= 50)
{
pass ++;
}
else
{
fail ++;
}
counter ++;
}
return 0;
}
Page 3 of 11
EE/CS-320
Question 4: The following program takes two numbers, a and b, as input from the user and
compares them. The program prints 1 if a is greater than b, and 0 otherwise. Write the equivalent
MIPS Assembly Language code. This program uses two procedures (functions): Compare and
Subtract. It also involves nested procedure calling. You must ensure that your MIPS
Assembly Language Code includes the procedures and that the register preservation
protocols are properly followed along with any other protocols involved in Procedure Calls.
C++ Code:
int Compare (int a, int b);
int Subtract (int a, int b);
int main ()
{
int a, b, x;
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
x=Compare(a,b);
cout << "Result: ";
cout << x;
cout << "\n";
return 0;
}
int Compare (int a, int b)
{
if(Subtract(a,b)>=0)
return 1;
else
return 0;
}
int Subtract (int a, int b)
{
return a-b;
}
Page 4 of 11
EE/CS-320
Question 5: The following C++ program finds out whether the number entered by the user is a
prime number or not. Write the equivalent MIPS Assembly Language code.
C++ Code:
int main()
{
int n, c;
cout << "Enter a number: ";
cin >> n;
for ( c = 2 ; c <= n 1 ; c++ )
{
if ( n % c == 0 )
{
break;
}
}
if ( c != n )
{
cout << "Not prime.\n";
}
else
{
cout << "Prime number.\n";
}
return 0;
}
Page 5 of 11
EE/CS-320
Question 6: The following C++ program takes the size and elements of an array as input from
the user and stores the array in memory. It then stores and prints the array in the reverse
order. Write the equivalent MIPS Assembly Language code.
C++ Code:
int main()
{
int n, c, j, temp, a[100];
cout << "Enter the number of elements in array: ";
cin >> n;
cout << "Enter the array elements: \n";
for ( c = 0 ; c < n ; c++ )
{
cin >> a[c];
}
if( n%2 == 0 )
{
c = (n/2) 1;
}
else
{
c = n/2;
}
for ( j = 0 ; j <= c ; j++ )
{
temp = a[j];
a[j] = a[n j 1];
a[n j 1] = temp;
}
cout << "Reverse array is: \n";
for( c = 0 ; c < n ; c++ )
{
cout << a[c];
cout << "\n";
}
return 0;
}
Page 6 of 11
EE/CS-320
Question 7: The following C++ program takes a number as input, tries to find it in an array and
(if found) prints the index at which it is stored in the array. Write the equivalent MIPS Assembly
Language code. This program uses a procedure (function): Find. You must ensure that your
MIPS Assembly Language Code includes this procedure and that the register preservation
protocols along with other protocols are properly followed.
C++ Code:
int find(int arr[], int n, int x);
int main ()
{
int number, x;
int arr[10]={6,5,8,2,9,10,16,18,200,100};
cout << "Enter the number you want to find: \n";
cin >> number;
x = find (arr, 10, number);
if (x == 1)
{
cout << "The number cannot be found\n ";
}
else
{
cout << "The number is at the index: ";
cout << x;
cout << "\n";
}
return 0;
}
int find (int arr[], int n, int x)
{
int i;
for (i=0; i != n; i++)
{
if (arr[i]==x)
return i;
}
return 1;
}
Page 7 of 11
EE/CS-320
Question 8: Write a MIPS Assembly Language Code that finds out the number of times a
particular character occurs in a given string. The final character count is then printed at the
console. The .data segment of this code is given below; you are required to develop the rest of
the code for this particular data segment. The string labeled as Input is the given string. The
character to be checked is initialized as a single character labeled as Check.
.data
Input: .asciiz "\nThis is Assignment-2 of the course EE/CS-320 Computer Organization and
Assembly Language. "
Check: .asciiz "e"
Result: .asciiz "\nThe number of times this character occurs in the string is: "
.text
.globl main
main:
Write the rest of the MIPS Assembly Language Code yourself
Question 9: Given any string, write a MIPS Assembly Language Code that checks if the first
alphabet of every word in the string is in small form (a, b, c ) and if so, it converts that first
alphabet into its corresponding capital form (A, B, C, ). To clarify consider the example
below:
When your program receives the string: \nhello how are you. i am fine. have a nice day.
It should convert this string to: \nHello How Are You. I Am Fine. Have A Nice Day.
It also means that single alphabets in the string will be converted to their capital version.
The .data segment of this code is given below along with the structure of the main. You are
required to write this program using a procedure call to the Procedure Capital which does
the necessary conversions in the string. Complete the Code below:
Important: You have to write a generic program that works for any given string. If you write a
program that only works for the string defined in the .data segment below then you will NOT
receive any credit.
Page 8 of 11
EE/CS-320
.asciiz "\nthe school of science & engineering started functioning in 2008. the school is
# Procedure Call
Exit:
# Exit the Program
Capital:
Page 9 of 11
EE/CS-320
Question 10: Binary Search is a fast way to search for an element in a sorted array. The
element to be searched is specified as the key. The key is compared to the middle element of
the array, if they are found equal, then the search is finished and the index number of the element
is returned. However, if the key is less than the middle element of the array, then the search is
repeated on the first half of the array (i.e., to the left of the middle element). If the key is
greater than the middle element of the array, then the search is repeated on the second half of the
array (i.e., to the right of the middle element). This process is repeated until the element is found
or until the sub-array size reduces to zero in which case the element is not present in the array.
The C++ code for a Binary Search operation is given below. It has two parts, the first one being
the main and the second one being the function binarysearch. This code uses a recursive
implementation of Binary Search.
C++ Code:
int binarysearch(int arr[],int n,int low,int high);
int main()
{
int arr[10];
int n=0, terms=0, result=0, low=0, high=0;
cout<<"\nEnter the number of Terms : ";
cin>>terms;
for(int x=0;x<terms;x++)
{
cout<<"\nEnter the Array Element (Sorted in Ascending Order): ";
cin>>arr[x];
}
cout<<"\nEnter the Element to be searched: ";
cin>>n;
low = 0;
high = terms-1;
result = binarysearch(arr,n,low,high);
if(result == -1)
{
cout<<"\nElement not found in the Array.";
}
else
{
cout<<"\nThe Element is present in the Array at Position: "<<result<<"\n";
}
return 0;
}
Page 10 of 11
EE/CS-320
You are required to write the complete equivalent MIPS Assembly Language Code for this
high level language code. You can initialize the integer variables directly in registers. You
are required to use only the MIPS Core Instruction Set. However, you can use the pseudo
instruction Load Address (la) only. Mention the use of various registers in comments.
Page 11 of 11