Вы находитесь на странице: 1из 5

DHA SUFFA UNIVERSITY

Department of Computer Science


CS-2101L Computer Organization and Assembly Language
Spring 2020

Lab Manual 06
Last Lecture
In the last lecture we studied following topics:
 Branches
 Jump statement
 Input from user in an array using for loop
 Output array using for loop

Task 6.1
Different ways of implementing for loop:
#include <iostream> .text .text
Using namespace std; .globl main .globl main
Int main() main: main:
{
for (int i = 0; i < 10; ++i) li $t0, 0 li $t0, 0
{ li $t1, 10 li $t1, 10
cout << i << endl;
} cond: cond:
return 0; blt $t0, $t1, loop bge $t0, $t1, end_loop
} j end_loop
li $v0, 1
loop: move $a0, $t0
li $v0, 1 syscall
move $a0, $t0
syscall addi $t0, $t0, 1
j cond
addi $t0, $t0, 1
j cond end_loop:
li $v0, 10
end_loop: syscall
li $v0, 10
syscall
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2101L Computer Organization and Assembly Language
Spring 2020

Lecture 06

Sorting
In last lab, we have seen how to take input in an array using for loop. Also, printing the elements of an
array using for loop. Now we will be using same code with little modification of swapping the values on
basis if condition.

Task 6.2
Bubble Sort
C++ Code Equivalent Assembly Code

#include <iostream>
using namespace std;
int main() Task 5.3.1
{
int arr[5];
#################Sorting Code################
for (int i = 0; i < 5; ++i) {
cout << “Enter num: “ << endl; li $t5, 5 # size of an array
cin >> arr[i]; li $t0, 0 # i = 0
} li $t1, 0 # j = 0
la $t2, array # arr[i]
for (int i = 0; i < 5; ++i) { la $t3, array # arr[j]
for (int j = i + 1; j < 5; ++j) {
if(arr[i] > arr[j]) { outer_loop:
int temp = arr[i]; # i >= 5, end_outer_loop
arr[i] = arr[j]; bge $t0, $t5, end_outer_loop
arr[j] = temp;
} #j=i+1
} addi $t1, $t0, 1
} # arr[j] = arr[i + 1]
addi $t3, $t2, 4
for (int i = 0; i < 5; ++i) {
cout << arr[i] << “ “; inner_loop:
} # j >= 5, end_inner_loop
cout << endl; bge $t1, $t5, end_inner_loop

cout << fact << endl; # $t7 = arr[i]


return 0; lw $t7, 0($t2)
} # $t8 = arr[j]
lw $t8, 0($t3)
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2101L Computer Organization and Assembly Language
Spring 2020

# arr[i] <= arr[j] continue, else swap


ble $t7, $t8, continue

# $t6 = arr[i]
lw $t6, 0($t2)
# $t7 = arr[j]
lw $t7, 0($t3)
# arr[i] = $t7
sw $t7, 0($t2)
# arr[j] = $t6
sw $t6, 0($t3)

# on each inner loop iteration


continue:
#j=j+1
addi $t1, $t1, 1
# arr[j] = arr[j + 1]
addi $t3, $t3, 4

j inner_loop

# when inner loop ends each time


end_inner_loop:
#i=i+1
addi $t0, $t0, 1
# arr[i] = arr[i + 1]
addi $t2, $t2, 4

j outer_loop
end_outer_loop:

Task 5.3.2

# ---- Exit Program ---- #


li $v0, 10
syscall
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2101L Computer Organization and Assembly Language
Spring 2020

Task 6.3
Write a program in MIPS which takes 10 integer values as an input from the user and prints the
maximum and minimum value entered by the user.
C++ Code

int main()
{
int arr[10]
int max = 0, min = 0;

for (int i = 0; i < 10; ++i) {


cin >> arr[i];
}

max = arr[0];
min = arr[0];

for (int i = 1; i < 10; ++i) {

if (arr[i] > max) {


max = arr[i];
}

if (arr[i] < min) {


min = arr[i];
}
}

cout << “Max: “ << max << endl;


cout << “Min: “ << min << endl;

return 0;
}
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2101L Computer Organization and Assembly Language
Spring 2020

Home Task
Take two array of size 10 as input from user. Find if both arrays contain exact elements.

Input:
10 20 5 25 70 20 18 12 23 7
10 7 12 5 20 25 70 20 18 23

Output:
True

Submission Instructions
1) Please upload .asm file compressed in .zip file according to the following naming pattern:
a) CSxxxxxx3B1.zip or CSxxxxxx3B2.zip, where xxxxxx is a place-holder for your roll-
number.
b) No extra print statements, Input and Output should be followed in a specific way which is
shown in the sample Input and sample Output.

Оценить