Sie sind auf Seite 1von 11

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

EE/CS-320

Computer Organization & A.L.

EE/CS-320 - Computer Organization & Assembly Language


(Fall Semester 2013-14)
Assignment 2
Given on Date: 22nd September 2013
Submission Deadline: 3rd October 2013 (02:00 pm)
Total Questions: 10; Total Marks: 200 (Marks are NOT uniformly distributed among parts)
Instructions
In this assignment, mostly you are given programs written in the high level language C++, you
are required to write the equivalent MIPS Assembly Language Code for each question.
Write the programs in proper .s assembly format such that if we want to run the code on
PCSPIM, we can do so without errors or problems. (Clearly define the .data field, .text field
and label names). Codes which do not run on PCSPIM will result into loss of marks.
Use ONLY the MIPS Core Instruction Set (found on the Book MIPS Reference Data Card).
Only one pseudo instruction will be accepted (Load Address (la). The use of other pseudo
instructions will not be accepted.
Submission Procedure: You are required to submit the .s files of your codes on LMS by
the due date and time. Zip all the .s files into a folder whose name is your roll number,
For example, a student with roll number 15100900 should place the 10 .s files of his 10
questions in a folder whose name is 15100900 and then zip it to 15100900.zip and submit
this single zipped file on LMS. Individual .s files inside the folder should have names
Q1.s, Q2.s, Q3.s and so on.
I will NOT ACCEPT any submissions over email. Only LMS submissions will be accepted.
LMS will ONLY accept submissions till 02:00 pm on 3rd October, beyond which time the
submission option will not be accessible.
NO Late submissions will be accepted.
You should test your code on PCSPIM for correctness.
Comment the code properly to increase readability. In comments explain the logic that you use in
converting the C++ Code to MIPS Assembly Language Code. There are marks in each question
for these comments.

LUMS SSE EE Department

Fall Semester 2013-14

Page 1 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

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;
}

LUMS SSE EE Department

Fall Semester 2013-14

Page 2 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

Question 3: Write the equivalent MIPS Assembly Language code.

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;
}

Turn to the Next Page

LUMS SSE EE Department

Fall Semester 2013-14

Page 3 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

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;
}

LUMS SSE EE Department

Fall Semester 2013-14

Page 4 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

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;
}

Turn to the Next Page

LUMS SSE EE Department

Fall Semester 2013-14

Page 5 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

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;
}

LUMS SSE EE Department

Fall Semester 2013-14

Page 6 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

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;
}

LUMS SSE EE Department

Fall Semester 2013-14

Page 7 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

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.

Turn to the Next Page

LUMS SSE EE Department

Fall Semester 2013-14

Page 8 of 11

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

EE/CS-320

Computer Organization & A.L.

MIPS Assembly Language Code:


.data
Str:

.asciiz "\nthe school of science & engineering started functioning in 2008. the school is

composed of 6 departments which are physics, chemistry, biology, mathematics, computer


science & electrical engineering. the year 2012 was a very important year for the school because
the first batch graduated in 2012."
.text
.globl main
main:

# Complete the Code in the Main part


# Print the string at Str before conversion by Procedure Capital

# Procedure Call

# Print the string at Str after conversion by Procedure Capital

Exit:
# Exit the Program

Capital:

# Procedure Capital Starts here

Complete the code

Turn to the Next Page

LUMS SSE EE Department

Fall Semester 2013-14

Page 9 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

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;
}

LUMS SSE EE Department

Fall Semester 2013-14

Page 10 of 11

EE/CS-320

Assignment-2 (Due: 3rd October 2013 at 02:00 pm)

Computer Organization & A.L.

int binarysearch(int arr[],int n,int low,int high)


{
int mid=0;
if (low > high)
{
return -1;
}
mid = (low + high)/2;
if (n == arr[mid])
{
return (mid+1);
}
else if(n < arr[mid])
{
high = mid - 1;
return binarysearch(arr,n,low,high);
}
else
{
low = mid + 1;
return binarysearch(arr,n,low,high);
}
}

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.

LUMS SSE EE Department

Fall Semester 2013-14

Page 11 of 11

Das könnte Ihnen auch gefallen