You are on page 1of 12

CS 211

Name - Aditya
Roll no. - 2004201
Branch - EE
Part B
Q1 ; Evaluate the expression ‘ab-10a+20b+16’. Consider that only $t0 and $t1 are
available to store temporary values. Store a=10 and b=20 in data section. Use stack for
other memory requirements. Display the sum.
Ans ;
# To find the expresssion of ab-10a+20b+16
.data
n1: .word 10
n2: .word 20
result: .word 0
.text
main:
li $t0, 10
sw $t0, 0($sp)
addi $sp, $sp, -4
li $t1, 20
sw $t1, 0($sp)

addi $sp, $sp, -4


lw $t0, 8($sp) lw
$t1, 4($sp) mult
$t0, $t1 mflo $t2
sw $t2, 0($sp)
addi $sp, $sp, -4
mult $t0, $t0
mflo $t2
sw $t2, 0($sp)
addi $sp, $sp, -4
mult $t1, $t1
mflo $t2
sw $t2, 0($sp)
addi $sp, $sp, -4
lw $t0, 12($sp) lw
$t1, 8($sp) sub
$t0, $t0, $t1 lw
$t1, 4($sp) add
$t0, $t0, $t1 addi
$t0, $t0, 16 add
$a0, $0, $t0 li $v0,
1
syscall
# end of the programe.

Displaying output result.

Q2 ; Find the maximum of the three expressions: x*x ; x*y ; y*5. Take x and y as
input from user. Write a subroutine to calculate values of these expressions. Write a
subroutine to find maximum of two integers and use it to find the maximum of
these three expressions. Display the result.
Ans ;
# To find the maximum value.
.data
inputX: .asciiz "please give the value of x "
inputY: .asciiz "please give the value of y "
output: .asciiz "the maximmum value of x*x , x*y , y*5 = "
output1: .asciiz "max = x*x and it is equal to "
output2: .asciiz "max = x*y and it is equal to "
output3: .asciiz "max = 5*y and it is equal to "

.text

addi $t0,$0,5 li
$v0,4
la $a0,inputX
syscall
li $v0,5
syscall
move $t1,$v0
li $v0,4
la $a0,inputY
syscall
li $v0,5
syscall
move $t2,$v0 mul
$t3,$t1,$t1 mul
$t4,$t1,$t2 mul
$t5,$t2,$t0 jal
maxi
maxi:
bge $t3,$t4,maximum1 ble
$t3,$t4,maximum2
maximum1:
bge $t3,$t5,exit1
jal exit3
maximum2:
bge $t4,$t5,exit2
jal exit3
exit1:
li $v0,4
la $a0,output1
syscall
li $v0,1 move
$a0,$t3
syscall
li $v0,10
syscall
exit2:
li $v0,4
la $a0,output2
syscall

li $v0,1 move
$a0,$t4
syscall
li $v0,10
syscall
exit3:
li,$v0,4
la $a0,output3
syscall
li $v0,1 move
$a0,$t5
syscall
li $v0,10
syscall
# end of the programe.
Displaying output result.
Q3 ; (A variation of (Q2)) Find the maximum of the three expressions: x*x ; x*y ;
y*5. Take x and y as input from user in one file. Write a global subroutine, in
another file, to calculate values of these expressions. Write a global subroutine, in
another file, to find maximum of two integers and use it to find the maximum of
these three expressions. Display the result.
Ans ;
Part a.
.data
inputX: .asciiz "please give the value of x "
inputY: .asciiz "please give the value of y "
output: .asciiz "the maximum value of x*x , x*y , y*5 = "
output1: .asciiz "max = x*x and it is equal to "
output2: .asciiz "max = x*y and it is equal to "
output3: .asciiz "max = 5*y and it is equal to "

.text

addi $t0,$0,5 li
$v0,4
la $a0,inputX
syscall
li $v0,5
syscall
move $t1,$v0
l
i

$
v
0
,
4
la $a0,inputY
syscall
li $v0,5
syscall
move $t2,$v0

part b.
.text
output: .asciiz "the maximum value of x*x , x*y , y*5 = "
.data

mul $t3,$t1,$t1
mul $t4,$t1,$t2
mul $t5,$t2,$t0
jal maxi
maxi:
bge $t3,$t4,maximum1 ble
$t3,$t4,maximum2
maximum1:
bge $t3,$t5,exit1
jal exit3
maximum2:
bge $t4,$t5,exit2
jal exit3

part c.
.data
.text
i value of x " inputY: .asciiz "please give the
n value of y "
p output: .asciiz "the maximmum value of x*x , x*y , y*5 ="
u output1: .asciiz "max = x*x and it is equal to "
t output2: .asciiz "max = x*y and it is equal to "
X output3: .asciiz "max = 5*y and it is equal to "
:

.
a
s
c
i
i
z

"
p
l
e
a
s
e

g
i
v
e

t
h
e
exit1:
li $v0,4
la $a0,output1
syscall
li $v0,1 move
$a0,$t3
syscall
li $v0,10
syscall
exit2:
li $v0,4
la $a0,output2
syscall

li $v0,1 move
$a0,$t4
syscall
li $v0,10
syscall
exit3:
li,$v0,4
la $a0,output3
syscall
li $v0,1 move
$a0,$t5
syscall
li $v0,10
syscall
#end of the programe.
Displaying output result.

You might also like