You are on page 1of 13

Final-Term Exam Spring-2021

FACULTY OF ENGINEERING, SCIENCE AND TECHNOLOGY

Subject: Computer organization &Assembly language lab Submission Day: Wednesday


Instructor: Muhammad Daud Abbasi Submission Date: 2/06/2021
Program: BS (CS) Max. Marks: 40

Question No.1:
CLO [2] [05Marks]
write an Assembly language based program for MIPS Architecture based MARS simulator to find the
Factorial of 15

Input Coding
.data

msg: .asciiz "Enter a number"


answer: .asciiz "\nFactorial is: "

.text
li $v0, 4
la $a0, msg
syscall
li $v0, 5
syscall
move $a0, $v0
li $v0, 1
syscall
jal calculate_factorial
move $a1, $v0
li $v0, 4
la $a0, answer
syscall
move $a0, $a1
li $v0, 1
syscall
li $v0, 10
syscall
calculate_factorial:
addi $sp, $sp-4
sw $ra, ($sp)
li $v0, 1
multiply:
beq $a0, $zero, return
mul $v0, $v0, $a0
addi $a0, $a0, -1
j multiply
return:
lw $ra, ($sp)
jr $ra

OUTPUT
Question No.2: CLO [2] [10 Marks]

write an Assembly language based program for MIPS Architecture based MARS simulator to find
Y !+ 8!
X=
Z!
Here Y equals to last digit in your Iqra University student ID
Here Z equals to second last digit in your Iqra University student ID
Input Coding

.data
read_num: .asciiz "Input an integer v:"
fact: .asciiz "A (5!+8!/3!) = "
.text
main:

li $t0,5
move $a0, $t0
jal factorial
move $s1, $v0

li $t1,8
move $a0, $t1
jal factorial
move $s2, $v0

li $t2,3
move $a0, $t2
jal factorial
move $s3, $v0

div $s4,$s2,$s3
add $s5,$s4,$s1

li $v0, 4
la $a0, fact
syscall

li $v0, 1
move $a0, $s5
syscall

li $v0, 10
syscall

.text
factorial:

addi $sp, $sp, -8

sw $s0, 4($sp)
sw $ra, 0($sp)
bne $a0, 0, else
addi $v0, $zero, 1
j fact_return

else:
move $s0, $a0
addi $a0, $a0, -1
jal factorial

multu $s0, $v0


mflo $v0
fact_return:
lw $s0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra

OUTPUT
Question No.3: CLO [2] [10 Marks]

write an Assembly language based program for MIPS Architecture based MARS simulator to take two
numbers from range (0-9) input from user and perform below mentioned tasks:
i. If user press 1 perform Addition of both
ii. If user press 2 perform subtraction of both
iii. If user press 3 perform multiplication of both
iv. If user press 4 perform division of both

Input Coding
.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "

menu: .asciiz "Enter the number associated with the operation you want performed: 1 => add, 2 =>
subtract or 3 => multiply: "
resultText: .asciiz "Your final result is: "
.text
.globl main
main:

li $t3, 1
li $t4, 2
li $t5, 3

#asking the user to provide the first number


li $v0, 4
la $a0, prompt1
syscall

#the next block of code is for reading the first number provided by the user
li $v0, 5
syscall
move $t0, $v0

#asking the user to provide the second number


li $v0, 4
la $a0, prompt2
syscall

#reading the second number to be provided to the user


li $v0, 5
syscall
move $t1, $v0

li $v0, 4
la $a0, menu
syscall
#the next block of code is to read the number provided by the user
li $v0, 5
syscall
move $t2, $v0

beq $t2,$t3,addProcess
beq $t2,$t4,subtractProcess
beq $t2,$t5,multiplyProcess

addProcess:
add $t6,$t0,$t1

#The following line of code is to print the results of the computation above
li $v0,4
la $a0,resultText
syscall

#the following line of code prints out the result of the addition computation
li $v0,1
la $a0, ($t6)
syscall

li $v0,10
subtractProcess:
sub $t6,$t0,$t1
li $v0,4
la $a0,resultText
syscall

#the following line of code prints out the result of the addition computation
li $v0,1
la $a0, ($t6)
syscall

li $v0,10
multiplyProcess:
mul $t6,$t0,$t1
li $v0,4
la $a0,resultText
syscall

#the following line of code prints out the result of the addition computation
li $v0,1
la $a0, ($t6)
syscall

li $v0,10 #This is to terminate the program


OUTPUT
Question No.4:
Take a variable x as input from the users and implement the following equation
(4z+6)/(7z+2)
Verify that the program works by using several initial values for x, then check what happens when x = -
7.

Input Coding
.data
.text
.globl main
main:
ori $1, $0, -7 # put x in $1
ori $2, $0, 4 # put 3 in $2
mult $1, $2 # 3 * x
mflo $2 # hold value in $2
addiu $2, $2, 6 # add 7 to 3x

ori $3, $0, 7 # put two in $3


mult $1, $3 # multiply x*2
mflo $3 # put answer in $3
addiu $3, $3, 2 # add 8 to 2x put in $3

div $4 $2, $3 # (3x+7)/(2x+8)


mflo $1 # put quotient in $1
mfhi $2 # put the remainder in $2

move $a0, $4
li $v0, 1
syscall

OUTPUT
Question No.5: CLO [2] [05 Marks]

write an Assembly language based program for MIPS Architecture based MARS simulator to take any
String as input containing 20 characters. Save it in an Array and reverse it and Print.

Input Coding

.data
input: .space 256
output: .space 256

.text
.globl main
main:
li $v0, 8 # Ask the user for the string they want to reverse
la $a0, input
li $a1, 256
syscall

li $v0, 4
syscall

jal strlen

add $t1, $zero, $v0


add $t2, $zero, $a0
add $a0, $zero, $v0
li $v0, 1
syscall

reverse:
li $t0, 0
li $t3, 0

reverse_loop:
add $t3, $t2, $t0 # $t2 is the base address for our 'input' array, add loop
index
lb $t4, 0($t3)
beqz $t4, exit
sb $t4, output($t1)
subi $t1, $t1, 1
addi $t0, $t0, 1
j reverse_loop

exit:
li $v0, 4 # Print
la $a0, output # the string!
syscall

li $v0, 10 # exit()
syscall

strlen:
li $t0, 0
li $t2, 0

strlen_loop:
add $t2, $a0, $t0
lb $t1, 0($t2)
beqz $t1, strlen_exit
addiu $t0, $t0, 1
j strlen_loop

strlen_exit:
subi $t0, $t0, 1
add $v0, $zero, $t0
add $t0, $zero, $zero
jr $ra

OUTPUT

You might also like