Computer Architecture Lab
I Semester 2018-19
I Semester 2018-19
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)
#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
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