0% found this document useful (0 votes)
80 views106 pages

CO Lab Manual 3rd Yr Btech

The lab manual contains 7 experiments on computer organization and MIPS assembly language programming. The experiments cover topics like using the SPIM MIPS simulator, writing simple MIPS programs to print "hello world" and perform addition, implementing control structures like if-else, writing a custom printf function, and determining if a string is a palindrome. Students are provided the assembly code and instructions to complete the various programming tasks.

Uploaded by

hugu hugu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views106 pages

CO Lab Manual 3rd Yr Btech

The lab manual contains 7 experiments on computer organization and MIPS assembly language programming. The experiments cover topics like using the SPIM MIPS simulator, writing simple MIPS programs to print "hello world" and perform addition, implementing control structures like if-else, writing a custom printf function, and determining if a string is a palindrome. Students are provided the assembly code and instructions to complete the various programming tasks.

Uploaded by

hugu hugu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

Computer Science & Engineering Department

(Analytics)
National Institute of Technology, Delhi

LAB MANUAL OF COMPUTER ORGANISATION


(Teacher in charge:-Dr. Sushila Maheshkar)
The Lab Works:-
Experiment 1

 Introduction: SPIM MIPS Simulator .

Spim is a self-contained simulator that runs MIPS32 programs. It reads and


executes assembly language programs written for this processor. Spim also
provides a simple debugger and minimal set of operating system services. Spim
does not execute binary (compiled) programs.Spim implements almost the
entire MIPS32 assembler-extended instruction set. (It omits most floating point
comparisons and rounding modes and the memory system page tables.) The
MIPS architecture has several variants that differ in various ways (e.g., the
MIPS64 architecture supports 64-bit integers and addresses), which means that
Spim will not run programs for all MIPS processors.

Spim comes with complete source code and documentation.Spim implements


both a terminal and windows interfaces. On Microsoft Windows, Linux, and Mac
OS X, the spim program offers a simple terminal interface and the QtSpim
program provides the windowing interface.

You can download SPIM from this link:-


https://sourceforge.net/projects/spimsimulator/files

The newest version of Spim is called QtSpim, and unlike all of the other version,
it runs on Microsoft Windows, Mac OS X, and Linux—the same source code and
the same user interface on all three platforms! QtSpim is the version of Spim
that currently being actively maintaned. The other versions are still available,
but are no longer maintained or updated.

2
A compiled, immediately installable version of QtSpim is available for Microsoft
Windows, Mac OS X, and Linux can be downloaded from: this link :-

https://sourceforge.net/projects/spimsimulator/files/ Full source code is also


available (to compile QtSpim, you need the Qt framework, a very nice cross-
platform UI framework that can be downloaded from this link http://qt-
project.org/downloads).

3
Experiment 2

 Write a program to print hello world .

.text
main:
la $a0, hello_msg # load the addr of hello_msg into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
li $v0, 10 # 10 is the exit syscall.
syscall # do the syscall.
# Data for the program:
.data
hello_msg: .asciiz "Hello World\n"
# end hello.asm

4
Experiment 3
 Write a program to perform Addition .
.text
main:
## Get first number from user, put into $t0.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $t0, $v0 # move the number read into $t0.
## Get second number from user, put into $t1.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $t1, $v0 # move the number read into $t1.
add $t2, $t0, $t1 # compute the sum.
## Print out $t2.
move $a0, $t2 # move the number to print into $a0.
li $v0, 1 # load syscall print_int into $v0.
syscall # make the syscall.
li $v0, 10 # syscall code 10 is for exit.
syscall # make the syscall.
# end of add2.asm.

5
Experiment 4

 Write a program to implement Multiples .

.text
main:
## read A into $t0, B into $t1.
li $v0, 5 # syscall 5 = read_int
syscall
move $t0, $v0 # A = integer just read

li $v0, 5 # syscall 5 = read_int


syscall
move $t1, $v0 # B = integer just read
blez $t1, exit # if B <= 0, exit.

mul $t2, $t0, $t1 # S = A * B.


move $t3, $t0 # m = A

loop:
move $a0, $t3 # print m.
li $v0, 1 # syscall 1 = print_int
syscall # make the system call.

beq $t2, $t3, endloop # if m == S, we’re done.


add $t3, $t3, $t0 # otherwise, m = m + A.
6
la $a0, space # print a space.
li $v0, 4 # syscall 4 = print_string
syscall

b loop # iterate.
endloop:
la $a0, newline # print a newline:
li $v0, 4 # syscall 4 = print_string
syscall

exit: # exit the program:


li $v0, 10 # syscall 10 = exit
syscall # we’re outta here.

## Here’s where the data for this program is stored:


.data
space: .asciiz " "
newline: .asciiz "\n"
## end of multiples.asm

7
Experiment 5

 Write a program to implement If else construct.

.text
main:
## Get first number from user, put into $t0.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $t0, $v0 # move the number read into $t0.
## Get second number from user, put into $t1.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $t1, $v0 # move the number read into $t1.
## put the larger of $t0 and $t1 into $t2.
bgt $t0, $t1, t0_bigger # If $t0 > $t1, branch to t0_bigger,
move $t2, $t1 # otherwise, copy $t1 into $t2.
b endif # and then branch to endif
t0_bigger:
move $t2, $t0 # copy $t0 into $t2
endif:
## Print out $t2.
move $a0, $t2 # move the number to print into $a0.
li $v0, 1 # load syscall print_int into $v0.
syscall # make the syscall.
## exit the program.
8
li $v0, 10 # syscall code 10 is for exit.
syscall # make the syscall.
# end of larger.asm.

9
Experiment 6

 Write a program to implement your own C-like Printf function.

.data
printf_buf: .space 2
input: .asciiz "%s"
in: .asciiz "sgru11"

.text
## Register Usage:
## $a0,$s0 - pointer to format string
## $a1,$s1 - format argument 1 (optional)
## $a2,$s2 - format argument 2 (optional)
## $a3,$s3 - format argument 3 (optional)
## $s4 - count of formats processed.
## $s5 - char at $s4.
## $s6 - pointer to printf buffer
printf:
subu $sp, $sp,36 # set up the stack frame,
sw $ra, 32($sp) # saving the local environment.
sw $fp, 28($sp)
sw $s0, 24($sp)
sw $s1, 20($sp)
sw $s2, 16($sp)
sw $s3, 12($sp)
10
sw $s4, 8($sp)
sw $s5, 4($sp)
sw $s6, 0($sp)
addu $fp, $sp,36
# grab the arguments:
la $a0,input
la $a1,in
move $s0, $a0 # fmt string
move $s1, $a1 # arg1 (optional)
move $s2, $a2 # arg2 (optional)
move $s3, $a3 # arg3 (optional)
li $s4, 0 # set # of formats = 0
la $s6, printf_buf # set s6 = base of printf buffer.
printf_loop: # process each character in the fmt:
lb $s5, 0($s0) # get the next character, and then
addu $s0, $s0, 1 # bump up $s0 to the next character.
beq $s5, '%', printf_fmt # if the fmt character, then do fmt.
beq $0, $s5, printf_end # if zero, then go to end.
printf_putc:
sb $s5, 0($s6) # otherwise, just put this char
sb $0, 1($s6) # into the printf buffer,
move $a0, $s6 # and then print it with the
li $v0, 4 # print_str syscall
syscall

11
b printf_loop # loop on.
printf_fmt:
lb $s5, 0($s0) # see what the fmt character is,
addu $s0, $s0, 1 # and bump up the pointer.
beq $s4, 3, printf_loop # if we’ve already processed 3 args,
# then *ignore* this fmt.
beq $s5, 'd', printf_int # if ’d’, print as a decimal integer.
beq $s5, 's', printf_str # if ’s’, print as a string.
beq $s5, 'c', printf_char # if ’c’, print as a ASCII char.
beq $s5, '%', printf_perc # if ’%’, print a ’%’
b printf_loop # otherwise, just continue.
printf_shift_args: # shift over the fmt args,
move $s1, $s2 # $s1 = $s2
move $s2, $s3 # $s2 = $s3 75
add $s4, $s4, 1 # increment # of args processed.
b printf_loop # and continue the main loop.
printf_int: # deal with a %d:
move $a0, $s1 # do a print_int syscall of $s1.
li $v0, 4
syscall
b printf_shift_args # branch to printf_shift_args
printf_str: # deal with a %s:
move $a0, $s1 # do a print_string syscall of $s1.
li $v0, 4

12
syscall
b printf_shift_args # branch to printf_shift_args
printf_char: # deal with a %c:
sb $s1, 0($s6) # fill the buffer in with byte $s1,
sb $0, 1($s6) # and then a null.
move $a0, $s6 # and then do a print_str syscall
li $v0, 4 # on the buffer.
syscall
b printf_shift_args # branch to printf_shift_args
printf_perc: # deal with a %%:
li $s5, '%' # (this is redundant)
sb $s5, 0($s6) # fill the buffer in with byte %,
sb $0, 1($s6) # and then a null.
move $a0, $s6 # and then do a print_str syscall
li $v0, 4 # on the buffer.
syscall
b printf_loop # branch to printf_loop
printf_end:
lw $ra, 32($sp) # restore the prior environment:
lw $fp, 28($sp)
lw $s0, 24($sp)
lw $s1, 20($sp)
lw $s2, 16($sp)
lw $s3, 12($sp)

13
lw $s4, 8($sp)
lw $s5, 4($sp)
lw $s6, 0($sp)
addu $sp, $sp, 36 # release the stack frame.
#jr $ra # return.
## end of printf.asm

14
Experiment 7

 Write a program to implement Palindrome .

.data
input: .asciiz "yes"
input_len: .word 3
IS_STRING: .asciiz " is"
NOT_STRING: .asciiz " not"
A_PAL_STRING: .asciiz " a palindrome"
.text
main:
la $a0, input_len # Load data
lw $a0, 0($a0)
la $a1, input
jal isPalindrome # Do the Palindrome check
add $a0, $v0, $zero
jal printRes # Print
addi $v0, $zero, 10
syscall # Exit

isPalindrome:
# Check base case
slti $t0, $a0, 2
bne $t0, $zero, returnTrue
15
# Make sure first and last are equal
lb $t0, 0($a1)
addi $t1, $a0, -1
add $t1, $t1, $a1
lb $t1, 0($t1)
bne $t0, $t1, returnFalse

# Shift pointer, length, recurse


addi $a0, $a0, -2
addi $a1, $a1, 1
j isPalindrome

returnFalse:
addi $v0, $zero, 0
jr $ra

returnTrue:
addi $v0, $zero, 1
jr $ra
printRes:
add $t4, $a0, $zero # Stash result
addi $v0, $zero, 4

16
la $a0, input
syscall # print "<WORD>"
la $a0, IS_STRING
syscall # print "is"
bne $t4, $zero, printResCont
la $a0, NOT_STRING
syscall # print "not"
printResCont:
la $a0, A_PAL_STRING
syscall # print "a palindrome."
jr $ra

17
Experiment 8
 Write a program for finding Fibonacci numbers.

.text
fib:
subu $sp, $sp, 32 # frame size = 32, just because...
sw $ra, 28($sp) # preserve the Return Address.
sw $fp, 24($sp) # preserve the Frame Pointer.
sw $s0, 20($sp) # preserve $s0.
sw $s1, 16($sp) # preserve $s1.
sw $s2, 12($sp) # preserve $s2.
addu $fp, $sp, 32 # move Frame Pointer to base of frame.
move $s0, $a0 # get n from caller.
blt $s0, 2, fib_base_case # if n < 2, then do base case.
sub $a0, $s0, 1 # compute fib (n - 1)
jal fib #
move $s1, $v0 # s1 = fib (n - 1).
sub $a0, $s0, 2 # compute fib (n - 2)
jal fib
move $s2, $v0 # $s2 = fib (n - 2).
add $v0, $s1, $s2 # $v0 = fib (n - 1) + fib (n - 2).
b fib_return
fib_base_case: # in the base case, return 1.
li $v0, 1
fib_return:

18
lw $ra, 28($sp) # restore the Return Address.
lw $fp, 24($sp) # restore the Frame Pointer.
lw $s0, 20($sp) # restore $s0.
lw $s1, 16($sp) # restore $s1.
lw $s2, 12($sp) # restore $s2.
addu $sp, $sp, 32 # restore the Stack Pointer.
jr $ra # return.

19
Experiment 9
 Write a program to perform Addition using Stack .

.data
aprompt: .asciiz "a="
bprompt: .asciiz "b="
cprompt: .asciiz "c="
msg: .asciiz "ans="
.text
main:
la $a0,aprompt # print the prompt for a(load address(la))
li $v0,4
syscall # syscall to store the value
li $v0,5 # read input as integer.
syscall
move $t0,$v0 # store a in $t0
la $a0, bprompt # print the prompt for b
li $v0, 4
syscall
li $v0, 5 # read input as integer
syscall
move $t1, $v0 # store variable b in $t1
la $a0, cprompt # print the prompt for b
li $v0, 4
syscall

20
li $v0, 5 # read input as integer
syscall
move $t2, $v0 # store variable c in $t2
add $t1,$t0,$t1 # (a+b)
addi $sp,$sp,-4 # decrement the stack pointer by 4
sw $t1,($sp) # push $t1 register value in the stack top
add $t1,$t0,$t2 # (a+c)
lw $t2,($sp) # pop the element from the stack and store it in $t2
addi $sp,$sp,4 # increment the stack pointer by 4
add $t1,$t1,$t2 # (a+b)+(a+c)
#move $a0,$t1 # print solution as integer

li $v0, 4
la $a0, msg
syscall

li $v0,1
la $a0,($t1)
syscall

21
Experiment 10

 Write a program to perform Subtraction using Stack .

.data
aprompt: .asciiz "a="
bprompt: .asciiz "b="
cprompt: .asciiz "c="
msg: .asciiz "ans="
.text
.text
main:
la $a0,aprompt # print the prompt for x(load address(la))
li $v0,4
syscall # syscall to store the value
li $v0,5 # read input as integer.
syscall
move $t0,$v0 # store a in $t0
la $a0, bprompt # print the prompt for b
li $v0, 4
syscall
li $v0, 5 # read input as integer
syscall
move $t1, $v0 # store variable b in $t1
la $a0, cprompt # print the prompt for b
li $v0, 4
22
syscall
li $v0, 5 # read input as integer
syscall
move $t2, $v0 # store variable c in $t2
sub $t1,$t0,$t1 # (a-b)
addi $sp,$sp,-4 # decrement the stack pointer by 4
sw $t1,($sp) # push $t1 register value in the stack top
sub $t1,$t0,$t2 # (a-c)
lw $t2,($sp) # increment the stack pointer by 4
sub $t1,$t2,$t1 # (a-b)-(a-c)
#move $a0,$t1 # print solution as integer

li $v0, 4
la $a0, msg
syscall

li $v0,1
la $a0,($t1)
syscall

23
Experiment 11
 Write a program to implement binary search.

.data
.align 2

msg_inputList: .asciiz "Please enter positive numbers in ascending order and


a 0 to terminate\n"
msg_searchList: .asciiz "Please enter a number to initSearch for\n"
msg_YES: .asciiz " - YES\n"
msg_NO: .asciiz " - NO\n"

.text
.globl main

main:
li $v0, 4 # syscall 4 (print_str)
la $a0, msg_inputList # load the input message
syscall # execute message print

li $v0, 9 # syscall 9 (sbrk)


la $a0, 4 # 4 bytes allocated for ints
syscall # execute memory allocation
move $s1, $v0 # store the start address of heap

24
li $s4, 0 # set list items counter to 0

inputList:
li $v0, 5 # syscall 5 (read_int)
syscall # execute int reading
move $t1, $v0 # store int in $t1
blez $v0, initSearchList # start search items input if 0 is input

li $v0, 9 # syscall 9 (sbrk)


la $a0, 4 # 4 bytes allocated for ints
syscall # execute memory allocation

li $t0, 4 # 4 bytes for an int


mul $t0, $s4, $t0 # length of the input storage address space
add $t0, $t0, $s1 # calculate end of address space
move $s5, $t0 # store end of address space
sw $t1, ($t0) # store the input on the heap
addi $s4 $s4, 1 # counter++

j inputList # take next input

initSearchList:
li $v0, 4 # syscall 4 (print_str)

25
la $a0, msg_searchList # load the search items input message
syscall # execute message print

li $s2, 0 # set search items counter to 0

searchList:
li $v0, 5 # syscall 5 (read_int)
syscall # execute int reading
move $t1, $v0 # move int to $t1
blez $v0, initSearch # start search if 0 was entered

li $v0, 9 # syscall 4 (sbrk)


la $a0, 4 # 4 bytes allocated for ints
syscall # execute memory allocation

li $t0, 4 # 4 bytes for an int


add $t2, $s4, $s2 # length of the list is counter1 + counter 2
mul $t0, $t2, $t0 # length of the input storage address space
add $t0, $t0, $s1 # calculate end of address spaces
move $s3, $t0 # store end of address space
sw $t1, ($t0) # store input on the heap
addi $s2, $s2, 1 # counter++

j searchList # take next input

26
initSearch:
move $t6, $s5 # store end address of input items
move $t7, $s3 # store end address of search items

search:
move $t5, $s5 # store end address of input items
beq $t7, $t6, exit # if there's nothing to search, exit

splitStep:

move $s6, $s1 # min is the start address of the heap


move $s7, $s5 # max is the end address of the heap
move $t0, $s4 # store the input list counter
move $t9, $s4 # store the input list counter
li $v1, 2 # store 2
div $t9, $v1 # divide the counter by 2
mflo $t9 # move result of division to $t9
add $t9, $t9, $v1

checkHigher:
li $v1, 2 # store 2
div $t0, $v1 # divide the counter by 2

27
mflo $v1 # store the division result
mflo $t0 # move the counter out

blez $t0, remainderStep # counter is at 0, check remainer step

j loopCheck # run the looping check

checkLower:
li $v1, 2 # store 2
div $t0, $v1 # divide the counter by 2
mflo $v1 # store the division result
mflo $t0 # move the counter out
mfhi $t1 # move Hi to $t1

blez $t0, failStep # If the counter equals zero and so does the
division remainder then print no

j loopCheck # run the looping check

failStep:
blez $t1, no # failed, return no

28
loopCheck:
beq $s6, $s7, no # max and min are now the same, didn't find the
number
blez $t9, no # lower counter is 0, we didn't find the number

mul $v1, $t0, 4 # multiply counter by 4 to get the address space


length
add $t4, $s6, $v1 # add the address space length to get the end
address

lw $a1, ($t7) # get value of $t7


lw $a2, ($t4) # get value of $t4

sub $t9, $t9, 1 # counter--

beq $a2, $a1, yes # we found it! yay


sub $t1, $a1, $a2 # is it greater than or less than the point?
blez $t1, searchLower # it's less than, run the search on the lower
segment
bgez $t1, searchHigher # it's greater than, run the search on the higher
segment

remainderStep:
mfhi $t8 # store result

29
bgtz $t8, incrementCounter # there's a remainder, move on to deal with
it
j loopCheck # no remainder, run the search

incrementCounter:
add $t0, $v1, $t8 # counter++

j loopCheck # run the search

searchLower:
move $s7, $t4 # max point is now the old midpoint

j checkLower # search lower segment

searchHigher:
move $s6, $t4 # min point is now the old max

j checkHigher # search higher segment

restartSearch:
sub $t7, $t7, 4 # counter - 4
30
j search # run search

yes:
li $v0, 1 # syscall 1 (print_int)
lw $a0, ($t4) # load current int
syscall # execute int printing

li $v0, 4 # syscall 4 (print_str)


la $a0, msg_YES # load yes message
syscall # execute message printing

j restartSearch # run search on the rest of the search items

no:
li $v0, 1 # syscall 1 (print_int)
lw $a0, ($t7) # load current int
syscall # execute int printing

li $v0, 4 # syscall 4 (print_str)


la $a0, msg_NO # load no message
syscall # execute message printing

j restartSearch # run search on the rest of the search items

31
exit:
li $v0, 10 # syscall 10 (exit)
syscall # execute exit

32
Experiment 12

 Write a program to implement Tree Sort .

.data
newline: .asciiz "\n"
out_of_mem_msg: .asciiz "Out of memory!\n"
.text
## Register usage:
## $s0 - the root of the tree.
## $s1 - each number read in from the user.
## $s2 - the sentinel value (right now, this is 0).
main:
li $s2, 0 # $s2 = the sentinel value.
## Step 1: create the root node.
## root = tree_node_create ($s2, 0, 0);
move $a0, $s2 # val = $s2
li $a1, 0 # left = NULL
li $a2, 0 # right = NULL
jal tree_node_create # call tree_node_create
move $s0, $v0 # and put the result into $s0.
## Step 2: read numbers and add them to the tree, until
## we see the sentinel value.
## register $s1 holds the number read.
input_loop:
li $v0, 5 # syscall 5 == read_int.
33
syscall
move $s1, $v0 # $s1 = read_int
beq $s1, $s2, end_input # if we read the sentinel, break.
# tree_insert (number, root);
move $a0, $s1 # number= $s1
move $a1, $s0 # root = $s0
jal tree_insert # call tree_insert.
b input_loop # repeat input loop.
end_input:
## Step 3: print out the left and right subtrees.
lw $a0, 4($s0) # print the root’s left child.
jal tree_print
lw $a0, 8($s0) # print the root’s right child.
jal tree_print
b exit # exit.
## end of main.
## tree_node_create (val, left, right): make a new node with the given
## val and left and right descendants.
## Register usage:
## $s0 - val
## $s1 - left
## $s2 - right
tree_node_create:
# set up the stack frame:

34
subu $sp, $sp, 32
sw $ra, 28($sp)
sw $fp, 24($sp)
sw $s0, 20($sp)
sw $s1, 16($sp)
sw $s2, 12($sp)
sw $s3, 8($sp)
addu $fp, $sp, 32
# grab the parameters:
move $s0, $a0 # $s0 = val
move $s1, $a1 # $s1 = left
move $s2, $a2 # $s2 = right
li $a0, 12 # need 12 bytes for the new node.
li $v0, 9 # sbrk is syscall 9.
syscall
move $s3, $v0
beqz $s3, out_of_memory # are we out of memory?
sw $s0, 0($s3) # node->number = number
sw $s1, 4($s3) # node->left = left
sw $s2, 8($s3) # node->right = right
move $v0, $s3 # put return value into v0.
# release the stack frame:
lw $ra, 28($sp) # restore the Return Address.
lw $fp, 24($sp) # restore the Frame Pointer.

35
lw $s0, 20($sp) # restore $s0.
lw $s1, 16($sp) # restore $s1.
lw $s2, 12($sp) # restore $s2.
lw $s3, 8($sp) # restore $s3.
addu $sp, $sp, 32 # restore the Stack Pointer.
jr $ra # return.
## end of tree_node_create.
## tree_insert (val, root): make a new node with the given val.
## Register usage:
## $s0 - val
## $s1 - root
## $s2 - new_node
## $s3 - root->val (root_val)
## $s4 - scratch pointer (ptr).
tree_insert:
# set up the stack frame:
subu $sp, $sp, 32
sw $ra, 28($sp)
sw $fp, 24($sp)
sw $s0, 20($sp)
sw $s1, 16($sp)
sw $s2, 12($sp)
sw $s3, 8($sp)
sw $s3, 4($sp)

36
addu $fp, $sp, 32
# grab the parameters:
move $s0, $a0 # $s0 = val
move $s1, $a1 # $s1 = root
# make a new node:
# new_node = tree_node_create (val, 0, 0);
move $a0, $s0 # val = $s0
li $a1, 0 # left = 0
li $a2, 0 # right = 0
jal tree_node_create # call tree_node_create
move $s2, $v0 # save the result.
## search for the correct place to put the node.
## analogous to the following C code:
## for (;;) {
## root_val = root->val;
## if (val <= root_val) {
## ptr = root->left;
## if (ptr != NULL) {
## root = ptr;
## continue;
## }
## else {
## root->left = new_node;
## break;

37
## }
## }
## else {
## /* the right side is symmetric. */
## }
## }
## ## Commented with equivalent C code (you will lose many
## style points if you ever write C like this...).
search_loop:
lw $s3, 0($s1) # root_val = root->val;
ble $s0, $s3, go_left # if (val <= s3) goto go_left;
b go_right # goto go_right;
go_left:
lw $s4, 4($s1) # ptr = root->left;
beqz $s4, add_left # if (ptr == 0) goto add_left;
move $s1, $s4 # root = ptr;
b search_loop # goto search_loop;
add_left:
sw $s2, 4($s1) # root->left = new_node;
b end_search_loop # goto end_search_loop;
go_right:
lw $s4, 8($s1) # ptr = root->right;
beqz $s4, add_right # if (ptr == 0) goto add_right;
move $s1, $s4 # root = ptr;

38
b search_loop # goto search_loop;
add_right:
sw $s2, 8($s1) # root->right = new_node;
b end_search_loop # goto end_search_loop;
end_search_loop:
# release the stack frame:
lw $ra, 28($sp) # restore the Return Address.
lw $fp, 24($sp) # restore the Frame Pointer.
lw $s0, 20($sp) # restore $s0.
lw $s1, 16($sp) # restore $s1.
lw $s2, 12($sp) # restore $s2.
lw $s3, 8($sp) # restore $s3.
lw $s4, 4($sp) # restore $s4.
addu $sp, $sp, 32 # restore the Stack Pointer.
jr $ra # return.
## end of node_create.
## tree_walk (tree):
## Do an inorder traversal of the tree, printing out each value.
## Equivalent C code:
## void tree_print (tree_t *tree)
## {
## if (tree != NULL) {
## tree_print (tree->left);
## printf ("%d\n", tree->val);

39
## tree_print (tree->right);
## }
## }
## Register usage:
## s0 - the tree.
tree_print:
# set up the stack frame:
subu $sp, $sp, 32
sw $ra, 28($sp)
sw $fp, 24($sp)
sw $s0, 20($sp)
addu $fp, $sp, 32
# grab the parameter:
move $s0, $a0 # $s0 = tree
beqz $s0, tree_print_end # if tree == NULL, then return.
lw $a0, 4($s0) # recurse left.
jal tree_print
# print the value of the node:
lw $a0, 0($s0) # print the value, and
li $v0, 1
syscall
la $a0, newline # also print a newline.
li $v0, 4
syscall

40
lw $a0, 8($s0) # recurse right.
jal tree_print
tree_print_end: # clean up and return:
lw $ra, 28($sp) # restore the Return Address.
lw $fp, 24($sp) # restore the Frame Pointer.
lw $s0, 20($sp) # restore $s0.
addu $sp, $sp, 32 # restore the Stack Pointer.
jr $ra # return.
## end of tree_print.
## out_of_memory -
## The routine to call when sbrk fails. Jumps to exit.
out_of_memory:
la $a0, out_of_mem_msg
li $v0, 4
syscall
j exit
## end of out_of_memory.
## exit -
## The routine to call to exit the program.
exit:
li $v0, 10 # 10 is the exit syscall.
syscall
## end of program!
## end of exit.

41
## Here’s where the data for this program is stored:
## end of tree-sort.asm

42
Experiment 13

 Write a program to implement BST .

.data

.text
main:
exit:
li $v0,10
syscall

printInteger:

subu $sp,$sp,8
sw $ra,0($sp)
sw $a0,4($sp)

#assuming $a0 contains int to print


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

lw $a0,4($sp)
lw $ra,0($sp)
addu $sp,$sp,8
43
jr $ra

printtree:

sub $sp,$sp,8
sw $a0,0($sp) #save orig $a0 as new TOS
sw $ra,4($sp) #save return address as TOS+4
lw $a0,8($sp) #access param1 in TOS+8
j leftmost
recur:
sub $sp,$sp,4
lw $a1,4($a0) #access node->left
sw $a1,0($sp) #push it to the stack
jal printtree
lw $a1,0($a0) # $a1 should contain node->data
jal printInteger

#node is assumed to be in $a0


lw $a0,8($a0) #access node->right

leftmost:
bne $a0,$zero,recur
lw $a0,0($sp) #hold the previous node
lw $ra,4($sp) #hold the return address

44
add $sp,$sp,12
jr $ra

locateSmall:
sub $sp,$sp,8
sw $ra,0($sp) #save return address
sw $t0,4($sp)
lw $t0,8($sp) #load param1
j seeIfZero

back1:
lw $t0,4($t0)
seeIfZero:
lw $a0,4($t0) #
bne $a0,$zero,back1

lw $ra,0($sp)
lw $t0,4($sp)
add $sp,$sp,4
jr $ra

malloc:
subu $sp,$sp,4
sw $ra,4($sp)

45
nop
nop
ret0:
lw $ra,4($sp)
add $sp,$sp,4
jr $ra

build123:
sub $sp,$sp,36
sw $ra,4($sp)
sw $t0,8($sp)
sw $t1,12($sp)
sw $t2,16($sp)
sw $t3,20($sp)
sw $t4,24($sp)
sw $v0,28($sp)
sw $s0,32($sp)

move $s0,$zero
#-----------add node 2-------------------------

sub $sp,$sp,4
li $t0,2
sw $t0,4($sp)

46
jal newNode
add $sp,$sp,4

sw $v0,nodes($s0)
add $s0,4

#-----------add node 1----------------------------


sub $sp,$sp,4
li $t0,1
sw $t0,4($sp)
jal newNode
add $sp,$sp,4

sw $v0,nodes($s0)
add $s0,4

#---------- add node 3-----------------------------


sub $sp,$sp,4
li $t0,3
sw $t0,4($sp)
jal newNode
add $sp,$sp,4

sw $v0,nodes($s0)

47
add $s0,4

#---------------------------------------

la $s0,nodes #get first nodes storage


lw $t0,0($s0) #get node 2
lw $t1,4($s0) #get node 1
lw $t2,8($s0) #get node 3

#-------linked them --------------------------------


move $s0,$t0 #get s0=node2
sw $t1,4($s0) #s0->left=t1
sw $t2,8($s0) #s0->right=t2

move $s0,$t1
sw $zero,4($s0)
sw $zero,8($s0)

move $s0,$t2
sw $zero,4($s0)
sw $zero,8($s0)

move $v1,$t0 #returns $v1 the topnode

48
lw $s0,32($sp)
lw $v0,28($sp)
lw $t4,24($sp)
lw $t3,20($sp)
lw $t2,16($sp)
lw $t1,12($sp)
lw $t0,8($sp)
lw $ra,4($sp)

add $sp,$sp,36
jr $ra

49
Experiment 14

 Write a program to implement "Bit-wise Add".

.data
prompt1: .asciiz "Enter a number: "
prompt2: .asciiz "Enter another number: "

.text
.globl main

main:
# Get first number
li $v0, 4
la $a0, prompt1
syscall
li, $v0, 5
syscall
move $s0, $v0
# Get second number
li $v0, 4
la $a0, prompt2
syscall
li, $v0, 5
syscall
move $s1, $v0
50
# loading the arguments and calling the adder function
move $a0, $s0
move $a1, $s1
jal bitwise_adder

move $a0, $v0


li, $v0, 1
syscall

# end the program


li $v0, 10
syscall
bitwise_adder:
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)

xor $s0, $a0, $a1 # $s0 holds the sum


and $s1, $a0, $a1 # $s1 holds the carry

adder_loop:
beq $s1, $zero, adder_end

51
sll $s1, $s1, 1 # carry = carry << 1
xor $t0, $s0, $s1 # temp = sum ^ carry
and $s1, $s0, $s1 # carry = sum & carry
move $s0, $t0 # sum = temp
j adder_loop

adder_end:
move $v0, $s0 # move sum into return value

# Restore the original values of the registers from the stack

lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra

52
Experiment 15

 Write a program to implement "Control flow".

.data
prompt: .asciiz "Enter an upper bound: "
STR_NEWLINE: .asciiz "\n"

.text
.globl main

main:

li $v0, 4
la $a0, prompt
syscall

li $v0, 5
syscall

move $a0, $v0


jal loop

li $v0, 10
syscall

53
loop:
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)

sub $s0, $s0, $s0 # $s0 (counter) = 0


move $s1, $a0 # store the argument into $s1

loop_main:
beq $s0, $s1, loop_end

# print the int


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

# print newline after int


li $v0, 4
la $a0, STR_NEWLINE
syscall

addi $s0, $s0, 1 # counter ++


j loop_main

54
loop_end:
# Restore the original values of the registers from the stack

lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra

55
Experiment 16

 Write a program to implement "Division".

# Telling it to interpret the following text as commands,


# instead of data
.text
main:
## Get first number from user, put into $a0.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $a0, $v0 # move the number read into $a0.
## Get first number from user, put into $a1.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $a1, $v0 # move the number read into $a1.
jal mydiv
move $a0, $t7 # move the number to print into $a0.
li $v0, 1 # load syscall print_int into $v0.
syscall # make the syscall.
li $v0, 10 # calls exit command (code 10)
syscall # end of program

## Divides two numbers, storing integer result on $t0 and rest on $t1
# $a0 Number we will divide
# $a1 Number we will divide for
56
mydiv:
addu $t7, $zero $zero # i = 0

mydiv_test:
slt $t6, $a0, $a1 # if ( a < b )
bne $t6, $zero, mydiv_end # then get out of here
sub $a0, $a0, $a1 # else, a = a - b
addi $t7, $t7, 1 # and i = i + 1
j mydiv_test # let's test again

mydiv_end:
addu $t0, $zero, $t7 # result = i
addu $t1, $zero, $a0 # rest = a
jr $ra

57
Experiment 17

 Write a program to implement "Function_with_Stack".

# Simple routine to demo functions


# USING a stack in this example to preserve
# values of calling function

# ------------------------------------------------------------------

.text

.globl main
main:
# Register assignments
# $s0 = x
# $s1 = y

# Initialize registers
lw $s0, x # Reg $s0 = x
lw $s1, y # Reg $s1 = y

# Call function
move $a0, $s0 # Argument 1: x ($s0)
jal fun # Save current PC in $ra, and jump to fun
move $s1,$v0 # Return value saved in $v0. This is y ($s1)
58
# Print msg1
li $v0, 4 # print_string syscall code = 4
la $a0, msg1
syscall

# Print result (y)


li $v0,1 # print_int syscall code = 1
move $a0, $s1 # Load integer to print in $a0
syscall

# Print newline
li $v0,4 # print_string syscall code = 4
la $a0, lf
syscall

# Exit
li $v0,10 # exit
syscall

# ------------------------------------------------------------------

# FUNCTION: int fun(int a)


# Arguments are stored in $a0

59
# Return value is stored in $v0
# Return address is stored in $ra (put there by jal instruction)
# Typical function operation is:

fun: # This function overwrites $s0 and $s1


# We should save those on the stack
# This is PUSH'ing onto the stack
addi $sp,$sp,-4 # Adjust stack pointer
sw $s0,0($sp) # Save $s0
addi $sp,$sp,-4 # Adjust stack pointer
sw $s1,0($sp) # Save $s1

# Do the function math


li $s0, 3
mul $s1,$s0,$a0 # s1 = 3*$a0 (i.e. 3*a)
addi $s1,$s1,5 # 3*a+5

# Save the return value in $v0


move $v0,$s1

# Restore saved register values from stack in opposite order


# This is POP'ing from the stack
lw $s1,0($sp) # Restore $s1
addi $sp,$sp,4 # Adjust stack pointer

60
lw $s0,0($sp) # Restore $s0
addi $sp,$sp,4 # Adjust stack pointer

# Return from function


jr $ra # Jump to addr stored in $ra

# ------------------------------------------------------------------

# Start .data segment (data!)


.data
x: .word 5
y: .word 0
msg1: .asciiz "y="
lf: .asciiz "\n"

61
Experiment 18

 Write a program to implement "Function_without_Stack".

# Simple routine to demo functions


# NOT using a stack in this example.
# Thus, the function does not preserve values
# of calling function!

# ------------------------------------------------------------------

.text

.globl main
main:
# Register assignments
# $s0 = x
# $s1 = y

# Initialize registers
lw $s0, x # Reg $s0 = x
lw $s1, y # Reg $s1 = y

# Call function
move $a0, $s0 # Argument 1: x ($s0)
jal fun # Save current PC in $ra, and jump to fun
62
move $s1,$v0 # Return value saved in $v0. This is y ($s1)

# Print msg1
li $v0, 4 # print_string syscall code = 4
la $a0, msg1
syscall

# Print result (y)


li $v0,1 # print_int syscall code = 1
move $a0, $s1 # Load integer to print in $a0
syscall

# Print newline
li $v0,4 # print_string syscall code = 4
la $a0, lf
syscall

# Exit
li $v0,10 # exit
syscall

# ------------------------------------------------------------------

# FUNCTION: int fun(int a)

63
# Arguments are stored in $a0
# Return value is stored in $v0
# Return address is stored in $ra (put there by jal instruction)
# Typical function operation is:

fun: # Do the function math


li $s0, 3
mul $s1,$s0,$a0 # s1 = 3*$a0 (i.e. 3*a)
addi $s1,$s1,5 # 3*a+5

# Save the return value in $v0


move $v0,$s1

# Return from function


jr $ra # Jump to addr stored in $ra

# ------------------------------------------------------------------

# Start .data segment (data!)


.data
x: .word 5
y: .word 0
msg1: .asciiz "y="
lf: .asciiz "\n"

64
Experiment 19

 Write a program to implement "Factorial of a number".

.data
prompt: .asciiz "Enter a number: "

.text
.globl main

main:

li $v0, 4
la $a0, prompt
syscall

li $v0, 5
syscall

move $a0, $v0


jal factorial
move $a0, $v0
li $v0, 1
syscall
li $v0, 10
syscall
65
factorial:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $s0, 4($sp)
move $s0, $a0
bne $s0, $zero, recursive_step
li $v0, 1 # base case
j factorial_end

recursive_step:
addi $a0, $a0, -1
jal factorial
mult $v0, $s0
mflo $v0
factorial_end:
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8
jr $ra

66
Experiment 20

 Write a program to implement "GCD of two number".

# 2.asm-- A program that finds GCD of any number of inputs


# Registers used:
# t0 - used to hold the latest input
# t1 - used to hold the last number/gcd
# t2 - integer constant -1
# t3 - a flag for first round
# v0- syscall parameter.

.data
input: .asciiz "Enter the numbers(-1 to exit): "
newline: .asciiz "\n"

.text
main:
# prompt for input
la $a0,input
li $v0, 4
syscall

# gcd in t1 , init w/ 1, integer const -1


li $t1, 1
li $t2, -1
67
li $t3, 1

loop:
# input in t0
li $v0, 5
syscall
move $t0, $v0

beq $t0, $t2, end

# if a number is entered, find gcd of it and the previous number


# a0-3 are used for passing function parameters
beqz $t3, ever

move $t1, $t0


li $t3, 0

ever:
move $a1 ,$t1
move $a0 ,$t0

jal gcd

68
# v0-1 are used to return from function
move $t1, $v0

move $a0, $t1


li $v0, 1
syscall

la $a0,newline
li $v0, 4
syscall

b loop
end:
li $v0, 10 # system call code for exit = 10
syscall # call operating sys
gcd: # standard algo for gcd -- result is stored in v0

subu $sp, $sp, 32 # Allocate a 32-byte stack frame


sw $ra, 20($sp) # Save Return Address
sw $fp, 16($sp) # Save old frame pointer
addiu $fp, $sp, 28 # Setup new frame pointer
bgt $a0, $a1, gt
beq $a0, $a1, eq
# if a0 < a1

69
sub $a1, $a1, $a0
jal gcd
b done
gt: # if a0 > a1
sub $a0, $a0, $a1
jal gcd
b done
eq: # if a0 == a1
move $v0, $a0
# li $v0, 7
b done
done: lw $ra, 20($sp) # Restore return address
lw $fp, 16($sp) # Restore frame pointer
addiu $sp, $sp, 32 # Pop stack
jr $ra

70
Experiment 21

 Write a program to check "a number is prime or not".

.data
prompt: .asciiz "Please enter a number: "
false: .asciiz "False\n"
true: .asciiz "True\n"

.text
.globl main

main:

# Prompt the user with input

li $v0, 4
la $a0, prompt
syscall

# read an integer value

li $v0, 5
syscall
move $a0, $v0 # pass the integer read as an argument into isPrime

71
jal isPrime

# print out whether the number is Prime


bne $v0, $zero, success # if $v0 == 1, then prime, branch to print success
message
li $v0, 4
la $a0, false
syscall
j program_end # jump to the end of the program

success:
li $v0, 4
la $a0, true
syscall

program_end:
li $v0, 10
syscall

####################################################
# ---- Function to check if integer is prime ----- #
####################################################

72
isPrime:
# Following MIPS calling conventions, allocate space in stack to store callee
saved registers
# that you will use

addi $sp, $sp, -8


sw $ra, 0($sp)
sw $s0, 4($sp)

li $s0, 2 # $s0 starts at 2 and goes up to n-1

isPrimeLoop:

beq $a0, $s0, prime # if $s0 = n, end

div $a0, $s0


mfhi $t0
beq $t0, $zero, notPrime

addi $s0, $s0, 1


j isPrimeLoop

prime:
addi $v0, $zero, 1
73
j isPrimeEnd
notPrime:
add $v0, $zero, $zero
isPrimeEnd:

# Restore the original values of the registers from the stack

lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8
jr $ra

74
Experiment 22

 Write a program to implement "Link list data structure".

.data
head: .word 0
NEWLINE: .asciiz "\n"
prompt: .asciiz "Enter value of node: "
linked: .asciiz "Now link list is: \n"

.text
.globl main

main:
# Insert node into linked list
li $v0, 4
la $a0, prompt
syscall
## Get first number from user, put into $a0.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $a0, $v0 # move the number read into $a0.
jal createNode
move $a0, $v0
jal insertNode

75
## Get first number from user, put into $a0.
li $v0, 4
la $a0, prompt
syscall
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $a0, $v0 # move the number read into $a0.
jal createNode
move $a0, $v0
jal insertNode

## Get first number from user, put into $a0.


li $v0, 4
la $a0, prompt
syscall
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $a0, $v0 # move the number read into $a0.
jal createNode
move $a0, $v0
jal insertNode

li $v0, 4
la $a0, linked

76
syscall
jal printList

# Exit the program


li $v0, 10
syscall

createNode:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $s0, 4($sp)

move $t0, $a0

li $v0, 9
li $a0, 8
syscall

sw $t0, 0($v0) # set node value


sw $zero, 4($v0) # node->next = NULL

lw $s0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 8

77
jr $ra
# Insert node into the linked list
insertNode:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $s0, 4($sp)

# $a0 stores address of the node


move $s0, $a0
# temp reference to head
lw $t0, head

bnez $t0, insert_while


# if head not defined
sw $s0, head
j insertNode_end

insert_while:
lw $t1, 4($t0)
beqz $t1, insert_while_end
move $t0, $t1
j insert_while
insert_while_end:
sw $s0, 4($t0)

78
insertNode_end:
lw $s0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra

# Print the linked list


printList:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $s0, 4($sp)

# Get ref to head


lw $s0, head

print_while:
beqz $s0, printList_end

li $v0, 1 # system call #1 - print integer


lw $a0, 0($s0)
syscall # execute

li $v0, 4 # system call #4 - print string


la $a0, NEWLINE

79
syscall # execute

# node = node -> next


lw $s0, 4($s0)
j print_while

printList_end:
lw $s0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra

80
Experiment 23

 Write a program to print "Matrix".

# Does operations on martrixes


# `mat` means
# 1, 2, 3
# 4, 5, 6
# 7, 8, 9
# `MAT` means
# 9, 8, 7
# 6, 5, 4
# 3, 2, 1

.data
mat: .word 1, 2, 3, 4, 5, 6, 7, 8, 9
MAT: .word 9, 8, 7, 6, 5, 4, 3, 2, 1
size: .word 3

endl: .asciiz "\n"


tab: .asciiz "\t"

.text
main:
la $a0, mat
lw $a1, size
81
jal mat_print

li $v0, 10
syscall

## Pretty-prints a matrix into standard output


# $a0 Address of matrix's first word
# $a1 Matrix's size
mat_print:
add $t3, $zero, $a0
add $t0, $zero, $zero #i=0

mat_print_while1:
add $t7, $zero, $zero
slt $t7, $t0, $a1 # if (i < `size`) continue
beq $t7, $zero, mat_print_end # if not, we've finished printing the whole
matrix

add $t1, $zero, $zero #j=0

mat_print_while2:
add $t6, $zero, $zero
slt $t6, $t1, $a1 # if (j < `size`) continue
beq $t6, $zero, mat_print_end_line # if not, we've finished printing the
current line

82
mul $t5, $t0, $a1 # c = i * `size`
add $t5, $t5, $t1 # c += j
sll $t4, $t5, 2 # (converting j to address size)
add $t5, $t4, $t3 # c = `matrix [i * size + j]
li $v0, 1
lw $a0, 0($t5) # printf("%d", c)
syscall
li $v0, 4
la $a0, tab # printf("\t")
syscall
addi $t1, $t1, 1 # j++
j mat_print_while2
mat_print_end_line: # printf("\n")
li $v0, 4
la $a0, endl
syscall
addi $t0, $t0, 1 # i++
j mat_print_while1
mat_print_end: # goodbye!
jr $ra

83
Experiment 24

 Write a program to count "number of bits".

# The procedure is not complicated, but the idea.


# We'll calculate how many bits we need to represent
# an unsigned number.
#
# For example, take 4-bit 6:
# 0110
#
# The number of bits necessary to represent it is 3,
# because it has a left-zero.

main:
## Get first number from user, put into $a0.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $a0, $v0 # move the number read into $a0.
jal no_of_bits
move $a0, $t0 # move the answer to print into $a0.
li $v0, 1 # load syscall print_int into $v0.
syscall # make the syscall.
li $v0, 10 # exit
syscall

84
## Returns the number of bits necessary to represent an unsigned number.
# $a0 The unsigned number we'll calculate
# $v0 The ammount
no_of_bits:
add $t0, $zero, $zero

no_of_bits_test:
beq $a0, $zero, no_of_bits_end
srl $a0, $a0, 1
addi $t0, $t0, 1
j no_of_bits_test

no_of_bits_end:
add $v0, $zero, $t0
jr $ra

85
Experiment 25

 Write a program to implement "Pass_by_referance".

#Program showing how to pass data into functions by reference, thereby


allowing you to work with values inside

.data
.align 2
x: .word 9
y: .word 4
STR_NEWLINE: .asciiz "\n"
COMMA: .asciiz ", "

.text
.align 2
.globl main

main:
# Load locations in memory of x and y global variables
la $s0, x
la $s1, y

# printing x
li $v0, 1
lw $a0, 0($s0)

86
syscall

# formatting
li $v0, 4
la $a0, COMMA
syscall

# printing y
li $v0, 1
lw $a0, 0($s1)
syscall

# newline
li $v0, 4
la $a0, STR_NEWLINE
syscall

move $a0, $s0


move $a1, $s1
jal swap

# printing x
li $v0, 1
lw $a0, 0($s0)

87
syscall

# formatting
li $v0, 4
la $a0, COMMA
syscall

# printing y
li $v0, 1
lw $a0, 0($s1)
syscall

# Exit program exectution


li $v0, 10
syscall

swap:
# Allocate space in stack to save registers
addi $sp, $sp, -8
sw $s0, 0($sp)
sw $s1, 4($sp)

lw $s0, 0($a0) # s0 = *x
lw $s1, 0($a1) # s1 = *y

88
sw $s0, 0($a1) # *y = *x
sw $s1, 0($a0) # *x = *y

# Pop saved variables off the stack


lw $s0, 0($sp)
lw $s1, 4($sp)
addi $sp, $sp, 8
# Jump to return address
jr $ra

89
Experiment 26

 Write a program to find "Number of Prime between two bound".

#Program that finds the number of primes between an upper and lower bound
.data
lowPrompt: .asciiz "Enter a lower bound: "
highPrompt: .asciiz "Enter an upper bound: "

.text
.globl main

main:

li $v0, 4
la $a0, lowPrompt
syscall

li $v0, 5
syscall
move $t0, $v0 # store lower bound in temp register

li $v0, 4
la $a0, highPrompt
syscall

90
li $v0, 5
syscall
move $a1, $v0
move $a0, $t0

jal primeTime

# print out how many primes between 4 and 10 there are

move $a0, $v0


li $v0, 1
syscall

li $v0, 10
syscall

###################################################################
###
# ---- Returns the number of prime numbers between $a0 and $a1 ----- #
# #
# @param $a0: lower bound #
# @param $a1: upper bound #
###################################################################
###

91
primeTime:
addi $sp, $sp, -16
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp) # will hold the result

# Move arguments into permanent registers

move $s0, $a0


move $s1, $a1

add $s2, $zero, $zero

loop:

beq $s0, $s1, loop_end

li $t0, 1
beq $s0, $t0, loop_continue # if lower val == 1, continue

move $a0, $s0 # make $s0 the argument to isPrime macro


jal isPrime

92
add $s2, $s2, $v0 # res = res + isPrime($s0)

loop_continue:
addi $s0, $s0, 1 # increment $s0
j loop

loop_end:

move $v0, $s2


lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
addi $sp, $sp, 16
jr $ra

####################################################
# ---- Function to check if integer is prime ----- #
####################################################

isPrime:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $s0, 4($sp)

93
li $s0, 2 # $s0 starts at 2 and goes up to n-1

isPrimeLoop:
beq $a0, $s0, prime # if $s0 = n, end
div $a0, $s0
mfhi $t0
beq $t0, $zero, notPrime
addi $s0, $s0, 1
j isPrimeLoop
prime:
addi $v0, $zero, 1
j isPrimeEnd
notPrime:
add $v0, $zero, $zero
isPrimeEnd:
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8
jr $ra

94
Experiment 27

 Write a program to find "fibonacci numbers wih Recursion".

.data
msg1:.asciiz "Give a number: "
.text
.globl main
main:

li $v0,4
la $a0,msg1
syscall #print msg
li $v0,5
syscall #read an int
add $a0,$v0,$zero #move to $a0

jal fib #call fib

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

li $v0,10
syscall

95
fib:
#a0=y
#if (y==0) return 0;
#if (y==1) return 1;
#return( fib(y-1)+fib(y-2) );

addi $sp,$sp,-12 #save in stack


sw $ra,0($sp)
sw $s0,4($sp)
sw $s1,8($sp)

add $s0,$a0,$zero

addi $t1,$zero,1
beq $s0,$zero,return0
beq $s0,$t1,return1

addi $a0,$s0,-1

jal fib
add $s1,$zero,$v0 #s1=fib(y-1)
addi $a0,$s0,-2
jal fib #v0=fib(n-2)
add $v0,$v0,$s1 #v0=fib(n-2)+$s1

96
exitfib:
lw $ra,0($sp) #read registers from stack
lw $s0,4($sp)
lw $s1,8($sp)
addi $sp,$sp,12 #bring back stack pointer
jr $ra
return1:
li $v0,1
j exitfib
return0 : li $v0,0
j exitfib

97
Experiment 28

 Write a program to implement "reverse of a string".

# A program that prints reverse of string


# Registers used:
# t0 - used to hold the latest input
# t1 - used to hold the last number/gcd
# t2 - integer constant -1
# t3 - a flag for first round
# v0- syscall parameter.

.data
input: .asciiz "Enter the string: "
newline: .asciiz "\n"
string_space : .space 104

.text
main: # Prompt for an input
la $a0,input # copy input prompt string location to a0
li $v0, 4 # load immediate v0 with int const 4
# 4 - function code for printing strings
syscall

# read in input to space string_space of max size 104


# syscall code for reading string -- 8
98
la $a0, string_space
li $a1, 104
li $v0, 8
syscall

la $a0, string_space
li $v0, 4
syscall

# find loc of \n for ease


# a loop
# t0 stands for curr string ctr
# t1 stands for curr byte
# t2 stands for len

la $t0, string_space
li $t2, 1

loop:
lb $t1, ($t0)
beq $t1, 10, found
addi $t0, $t0, 1
addi $t2, $t2, 1
b loop

99
found:
# use the address in t0 for end of string - maybe pass it as parameter
# set up parameters here in a0-3
# param 1 - a0 - init addr of input
# param 2 - a1 - effective len of string to be used for recursion

move $a1, $t2


la $a0, string_space
jal printreverse

la $a0,newline
li $v0, 4
syscall

li $v0, 10 # system call code for exit = 10


syscall # call operating sys
printreverse:
subu $sp, $sp, 32 # Allocate a 32-byte stack frame
sw $ra, 20($sp) # Save Return Address
sw $fp, 16($sp) # Save old frame pointer
addiu $fp, $sp, 28 # Setup new frame pointer
# check for end case
li $t4, -1

100
beq $a1, $t4, done
addi $t5, $a1, -1 # num - 1
move $t6, $a0
add $t6, $t6, $a1
lb $t7, ($t6)

# save a0 before
move $t1, $a0

# print char
move $a0, $t7
li $v0, 11
syscall

move $a0, $t1


# call recursive
move $a1, $t5
jal printreverse
done: lw $ra, 20($sp) # Restore return address
lw $fp, 16($sp) # Restore frame pointer
addiu $sp, $sp, 32 # Pop stack
jr $ra

101
Experiment 29

 Write a program to implement "Selection_sort".

.data
.align 2
array: .word 5 88 4 56 23 1 90
COMMA: .asciiz ", "
NEWLINE: .asciiz "\n"

.text
.globl main

main:
# Print the original array
jal printArray

# Print newline
li $v0, 4 # system call #4 - print string
la $a0, NEWLINE
syscall # execute

# Call the sort function


jal selection_sort
# Print the sorted array
jal printArray
102
# End the program with a syscall
li $v0, 10
syscall

#######################################################
# ---- Function that sorts an array of integers ----- #
#######################################################

selection_sort:
# Savingc callee saved registers in stack
addi $sp, $sp, -16
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
la $s0, array # Load address of array into $s0
addi $s0, $s0, 28 # $s0 = address of last el
la $s1, array # $s1 = address of first el (for outer loop)

outer_loop:
# if at end of list, branch to end of sort
beq $s1, $s0, sort_end
la $s2, array # $s2 = address of first el (for inner loop)
inner_loop:

103
# if at end of list, branch to outer loop end
beq $s2, $s0, outer_loop_end

# Load contents of array[i], array[j]


lw $t0, 0($s1) # $t0 = array[i]
lw $t1, 0($s2) # $t1 = array[j]

# if array[i] < array[j]


bgt $t0, $t1, inner_loop_end

# pass in address locations of array[i], array[j] into swap


move $a0, $s1
move $a1, $s2
jal swap

# End of inner loop


inner_loop_end:
addi $s2, $s2, 4 # Increment $t1 (j++)
j inner_loop
# End of outer loop
outer_loop_end:
addi $s1, $s1, 4 # Increment $t0 (i++)
j outer_loop
sort_end:

104
# restore registers from stack
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
addi $sp, $sp, 16
jr $ra # Exit function

######################################################
# ----- Function that swaps two ints in array ------ #
# @param $a0: address of a #
# @param $a1: address of b #
######################################################

swap:
# getting array[i], array[j]
lw $t0, 0($a0)
lw $t1, 0($a1)
# Switching the elements
sw $t0, 0($a1)
sw $t1, 0($a0)
# Exiting function
jr $ra

105
#######################################################
# ---- Function that prints an array of integers ---- #
#######################################################

printArray:
# Load address of first el and last el + 4
la $t0, array
la $t1, array
addi $t1, $t1, 28
print_loop:
# End loop if at end of array
beq $t1, $t0, print_end
lw $a0, 0($t0)
li $v0, 1
syscall # Printing the integer
# Printing a comma
li $v0, 4 # system call #4 - print string
la $a0, COMMA
syscall # execute
# Incrementing address by 4 bytes
addi $t0, $t0, 4
j print_loop
print_end:
jr $ra

106

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy