Professional Documents
Culture Documents
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
.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:
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
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
#the next block of code is for reading the first number provided by the user
li $v0, 5
syscall
move $t0, $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
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
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
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