You are on page 1of 14

“Lab Report # 05

Functions”

Name:Muhammad Ahsan
Khan
Registration No:259812
CE 40-B

Submitted to:Sir Ismail


Tasks 1:
1. Write a program in MIPS having function named
timesTen . The function should receive an integer
that was input by user in the main program. When
timesTen is called, it should
calculate the product of number times ten and on
return to the main program it should display the
product.
CODE:
.data
str:.asciiz"enter integer="
str1:.asciiz"product="
.text
.globl main
main:

li $v0,4
la $a0,str
syscall
li $v0,5
syscall
move $t0,$v0

li $t1,10

move $a0,$t0
move $a1,$t1

jal timesten

move $s1,$v0
li $v0,4
la $a0,str1
syscall

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

j exit

timesten:
mul $s0,$a0,$a1
move $v0,$s0
jr $ra

exit:
OUTPUT:

TASK 2:
2. Write a program in MIPS that has a function that
receives four integer arguments input by user in main
program and returns their sum and product. The results
should be displayed in main program.
CODE:
.data
str:.asciiz"enter the integer="
str1:.asciiz"product of integer="
str2:.asciiz"sum of integer="
.text
.globl main
main:

li $v0,4
la $a0,str
syscall

li $v0,5
syscall
move $t0,$v0

li $v0,4
la $a0,str
syscall

li $v0,5
syscall
move $t1,$v0
li $v0,4
la $a0,str
syscall

li $v0,5
syscall
move $t2,$v0

li $v0,4
la $a0,str
syscall

li $v0,5
syscall
move $t3,$v0
move $a0,$t0
move $a1,$t1
move $a2,$t2
move $a3,$t3

jal fun

move $s1,$v0

li $v0,4
la $a0,str1
syscall
li $v0,1
move $a0,$s1
syscall

move $s2,$v1

li $v0,4
la $a0,str2
syscall

li $v0,1
move $a0,$s2
syscall
j exit

fun:

mul $s0,$a0,$a1
mul $s1,$a2,$a3
mul $s2,$s0,$s1
move $v0,$s2

add $s3,$a0,$a1
add $s4,$a2,$a3
add $s5,$s3,$s4
move $v1,$s5

jr $ra
exit:

OUTPUT:

3. Write a function in MIPS to calculate factorial of 5


without using recursion.
CODE:

.data
str: .asciiz "Enter the number (5 as given in task) :"
str1: .asciiz "Factorial="
.text
.globl main

main:

li $v0,4
la $a0,str
syscall

li $v0,5
syscall
move $a1,$v0

jal factorial

li $v0,4
la $a0,str1
syscall

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

j exit

factorial:

li $s0,1

here:
beq $a1,0,here1
mul $s0,$s0,$a1
sub $a1,$a1,1
j here

here1:

jr $ra
exit:

OUTPUT:

You might also like