Computer Architecture Lab

I Semester 2018-19

Lab -1 (Programs in QTSPIM)


# 1. Program to print the hello Word (Hello.s)

.datamsg: .asciiz "Hello World"
.text main: li $v0, 4 # syscall 4 (print_str) la $a0, msg # argument: string syscall # print the string li $v0, 10 # syscall 10 (exit) syscall #


#2. program to accept Two number and find the sum(sumprogram.s)

.datamsg1: .asciiz "Enter No 1: "msg2: .asciiz "Enter No 2: "msg3: .asciiz "The sum is: "no1: .word 0no2: .word 0sum: .word 0crlf: .byte 0xd, 0xa, 0
.text .globl main
main: li $v0, 4 # syscall 4 (print_str) la $a0, msg1 # argument: string syscall # print the string
li $v0, 5 syscall sw $v0, no1
li $v0, 4 # syscall 4 (print_str) la $a0, msg2 # argument: string syscall # print the string
li $v0, 5 syscall sw $v0, no2
ld $t0, no1 ld $t1, no2 add $t2, $t0, $t1 sw $t2, sum
li $v0, 4 # syscall 4 (print_str) la $a0, msg3 # argument: string syscall # print the string
li $v0, 1 ld $a0, sum syscall
#li $v0, 4 # syscall 4 (print_str) #la $a0, crlf # argument: string #syscall # print the string

li $v0, 10 # syscall #
.end main

#3. Program to find the sum of first 10 numbers

## int i, sum = 0;# for (i=0; i < N; i++)# sum = sum + i#
.datamsg1: .asciiz "Enter N: "msg2: .asciiz "Sum = "no: .word 0sum: .word 0crlf: .byte 0xd, 0xa, 0
.text .globl main
main: li $v0, 4 # syscall 4 (print_str) la $a0, msg1 # argument: string syscall # print the string
li $v0, 5 syscall sw $v0, no
li $t0, 0 # sum = 0 li $t1, 1 # $t1 (i = 1) lw $t2, no # $t2 (t2 = no)next: bgt $t1, $t2, over add $t0, $t0, $t1 # sum = sum + i; addi $t1, $t1, 1 # i = i + 1 j next
over: sw $t0, sum
li $v0, 4 # syscall 4 (print_str) la $a0, msg2 # argument: string syscall # print the string
li $v0, 1 lw $a0, sum syscall
li $v0, 4 # syscall 4 (print_str) la $a0, crlf # argument: string syscall # print the string

li $v0, 10 # syscall #
.end main


01-CS F342-CompArch-Lab1 (6).pptx
Instruction set.pdf
MIPStextSMv11.pdf
MIPSReference.pdf

Lab-2


#1.Program to check prime numbers

.datamsg1: .asciiz "Enter the no: "msg_is_prime: .asciiz "The no is prime\n"msg_is_not_prime: .asciiz "The no is not prime\n"num: .word 0

.text .globl main
main: li $v0, 4 # syscall 4 (print_str) la $a0, msg1 # argument: string syscall # print the string
li $v0, 5 syscall sw $v0, num
lw $t0, num # $t0 = num srl $t1, $t0, 1 # $t1 = num/2 li $t2, 1 # $t2 = isPrime = 1 li $t3, 2 # $t3 = i = 2next_i: bleu $t3, $t1, calc_rem # if (i < num/2) then calc_rem j check_is_primecalc_rem: divu $t0, $t3 # num % i mfhi $t4 # $t4 = rem (hi) bne $t4, $0, try_next_i # if (rem != 0) try_next_i li $t2, 0 # if (rem == 0) isPrime = 0; break j check_is_prime # try_next_i: addiu $t3, $t3, 1 # i = i + 1 j next_i # goto next_i
check_is_prime: beq $t2, $0, is_not_prime # isPrime == 0 la $a0, msg_is_prime j print_msgis_not_prime: la $a0, msg_is_not_prime
print_msg: li $v0, 4 syscall
li $v0, 10 # exit syscall #


#2.Program to search number using binary search


# isFound = 0# lo = 0# high = n-1# while (lo <= high) {# mid = (lo + high)/2;# if (data == A[mid]) { isFound = 1; break; }# if (data < A[mid]) { high = mid - 1; continue;}# low = mid + 1;#}
.dataA: .word -32, -16, 0, 8, 16, 32, 64n: .word 7data: .word -16msg1: .asciiz " Found at pos: "msg2: .asciiz " Not found"
.text .globl mainmain: lw $t0, data # $t0 = (data) li $t1, 0 # $t1 = 0 (isFound) li $t2, 0 # $t2 = 0 (lo) lw $t3, n # $t3 = n add $t3, $t3, -1 # $t3 = n-1 (high)L1: bgt $t2, $t3, end_loop # if (lo > hi) end_loop add $t4, $t2, $t3 # $t4 = lo + high srl $t4, $t4, 1 # shift right logical: $t4 = $t4/2 (mid) sll $t5, $t4, 2 # shift left logical: $t5 = $t4 * 4 lw $t6, A($t5) # $t6 = A[mid] bne $t0, $t6, not_equal # $t0 != $t6 (data != A[mid]) li $t1, 1 # $t1 = 1 (isFound = 1) j end_loop # jmp outside loopnot_equal: blt $t0, $t6, less_than # $t0 < $t6 (data < A[mid]) addiu $t2, $t4, 1 # ($t2)lo = ($t4)mid + 1 j L1 # continue from begining.less_than: addiu $t3, $t4, -1 # ($t3)high = ($t4)mid - 1 j L1 # continue from begining.
end_loop: li $v0, 1 # print int move $a0, $t0 # $a0 = data
syscall li $v0, 4 beq $t1, $zero, msg_nf # if (isFound == 0)
la $a0, msg1 # isFound == 1 display msg syscall
li $v0, 1 # move $a0, $t4 # $a0 = mid syscall j exitmsg_nf : la $a0, msg2 # isFound == 0 display msg syscallexit: li $v0, 10 # exit syscall #


Lab-3

1: Write the MIPS ALP for the high level code


a = a * b / c % d + e;


2: Write MALP to check if a given no is odd/even &

print approprite msg


3: Write a MALP to check if a given year is a leap year

cond for leap year (read from kdb and display msg on console)

(year % 4 == 0 && year % 100 != 0) ||

year % 400 == 0)


4: Write a MALP for checking if an given no is Amstrong no (assum e 3 digits)

eg 153 ( 1^3 + 5^3 + 3^3 = 153)


5. Write a MAPLP to search for no in a array

i) linear search

ii) binary search


6. Write a MALP for Pascals Triangle

1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10


7. Write a MALP for generating ficonacci series (up to N)

8. Count the no of bits that are set (ie 1s) in a 32bit no.

9. Find the GCD of 2 nos.

10. Tower of Hanoi

11. Exchange with and with out pointers

12. strcat(char *dst, char *src)

Lab-4

#1.Program to get power of a number

#int power (int a, int n)

#{

# if (n == 1) return a;

# return a * power(n - 1);

#}

#main()

#{

# pow = power(a, n);

#}



.data

a: .word 9

n: .word 5

pow: .word 0



.text

.globl main

main:

lw $a0, a

lw $a1, n

jal power

sw $v0, pow


li $v0, 1

lw $a0, pow

syscall


li $v0, 10

syscall


power:

addi $sp, $sp, -8 # adjust stack for 2 items

sw $ra, 4($sp) # save return address

sw $a0, 0($sp)

addi $t1, $zero, 1 # $t1 = 1

bgt $a1, $t1, L1 # if $a1(=n) > $t1(=1) goto L1

move $v0, $a0 # if $a1(=n) == $t1(=1), result is $a0(=n)

addi $sp, $sp, 8 # pop 2 items from stack

jr $ra # and return

L1:

addi $a1, $a1, -1 # decrement n

jal power # recursive call: $v0=power(a, n-1)

lw $a0, 0($sp) # restore original a

mul $v0, $a0, $v0 # pseudo inst: multiply $v0=$a0(=a)*$v0(=power(a,n-1))


lw $ra, 4($sp) # retrive return address

addi $sp, $sp, 8 # restore sp

jr $ra # and return


Lab 4 -Part II

1. To read a number N from input and check if it is prime or not. Display the result to the user.

flag = 1

for (i = 2; i <= N / 2; i++)

{

if (n % i == 0)

{

flag = 0;

break;

}

if (flag == 1) {

printf("%d is a prime number", n);

}

else {

printf("%d is not a prime number", n);

}

Solution:

.data

msg1: .asciiz "Enter the no: "

msg_is_prime:

.asciiz "The no is prime\n"

msg_is_not_prime:

.asciiz "The no is not prime\n"

num: .word 0

.text

.globl main

main:

li $v0, 4 # syscall 4 (print_str)

la $a0, msg1 # argument: string

syscall # print the string

li $v0, 5

syscall

sw $v0, num

lw $t0, num # $t0 = num

srl $t1, $t0, 1 # $t1 = num/2

li $t2, 1 # $t2 = isPrime = 1

li $t3, 2 # $t3 = i = 2

next_i:

bleu $t3, $t1, calc_rem # if (i < num/2) then calc_rem

j check_is_prime

calc_rem:

divu $t0, $t3 # num % i

mfhi $t4 # $t4 = rem (hi)

bne $t4, $0, try_next_i # if (rem != 0) try_next_i

li $t2, 0 # if (rem == 0) isPrime = 0; break

j check_is_prime #

try_next_i:

addiu $t3, $t3, 1 # i = i + 1

j next_i # next_i

check_is_prime:

beq $t2, $0, is_not_prime # isPrime == 0

la $a0, msg_is_prime

j print_msg

is_not_prime:

la $a0, msg_is_not_prime

print_msg:

li $v0, 4

syscall

li $v0, 10 # exit

syscall #

2. To read a number N from input and find if the number is odd or even. Display the result to the user.

.data

msg1: .asciiz "Enter the no: "

msg_is_odd: .asciiz "The no is odd\n"

msg_is_even: .asciiz "The no is even\n"

num: .word 0

.text

.globl main

main:

li $v0, 4 # syscall 4 (print_str)

la $a0, msg1 # argument: string

syscall # print the string

li $v0, 5

syscall

move $t0, $v0 # $t0 = num

li $t1, 2

div $t0, $t1

mfhi $t0

beq $t0, $zero, even_no

la $a0, msg_is_odd

j print_msg

even_no:

la $a0, msg_is_even

print_msg:

li $v0, 4

syscall

li $v0, 10 # exit

syscall #

.end main

3. To find the sum of the digits of a number N. Display the result to the user.

sum = 0;

while (N != 0)

{

remainder = N % 10;

sum = sum + remainder;

N = N / 10;

}

Solution

.data

msg1: .asciiz "Enter the no: "

msg_sum: .asciiz "The sum is:"

.text

.globl main

main:

li $v0, 4 # syscall 4 (print_str)

la $a0, msg1 # argument: string

syscall # print the string

li $v0, 5

syscall

move $t0, $v0 # $t0 = num

li $t1, 0 # $t1 = sum

li $t2, 10 # $t2 = 10

next:

beq $t0, $zero, print_result

div $t0, $t2

mfhi $t3

mflo $t0

add $t1, $t1, $t3

j next

print_result:

la $a0, msg_sum

li $v0, 4

syscall

move $a0, $t1

li $v0, 1

syscall

li $v0, 10 # exit

syscall #

.end main

4. Write the MIPS ALP for the high level code. Observe precedence and associativity rules.

a = a * b / c % d + e;

#a = a * b / c % d + e;

Solution

.data

var_a: .word 10

var_b: .word 10

var_c: .word 10

var_d: .word 7

var_e: .word 10

msg_sum: .asciiz "The sum is:"

.text

.globl main

main:

lw $t0, var_a

lw $t1, var_b

mul $t0, $t0, $t1

lw $t1, var_c

div $t0, $t1

mflo $t0

lw $t1, var_d

div $t0, $t1

mfhi $t0

lw $t1, var_e

add $t0, $t0, $t1

la $a0, msg_sum

li $v0, 4

syscall

move $a0, $t0

li $v0, 1

syscall

li $v0, 10 # exit

syscall #

.end main