CO Lab Manual 3rd Yr Btech
CO Lab Manual 3rd Yr Btech
(Analytics)
National Institute of Technology, Delhi
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 :-
3
Experiment 2
.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
.text
main:
## read A into $t0, B into $t1.
li $v0, 5 # syscall 5 = read_int
syscall
move $t0, $v0 # A = integer just read
loop:
move $a0, $t3 # print m.
li $v0, 1 # syscall 1 = print_int
syscall # make the system call.
b loop # iterate.
endloop:
la $a0, newline # print a newline:
li $v0, 4 # syscall 4 = print_string
syscall
7
Experiment 5
.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
.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
.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
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
.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
.text
.globl main
main:
li $v0, 4 # syscall 4 (print_str)
la $a0, msg_inputList # load the input message
syscall # execute message print
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
initSearchList:
li $v0, 4 # syscall 4 (print_str)
25
la $a0, msg_searchList # load the search items input message
syscall # execute message print
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
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:
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
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
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
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++
searchLower:
move $s7, $t4 # max point is now the old midpoint
searchHigher:
move $s6, $t4 # min point is now the old max
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
no:
li $v0, 1 # syscall 1 (print_int)
lw $a0, ($t7) # load current int
syscall # execute int printing
31
exit:
li $v0, 10 # syscall 10 (exit)
syscall # execute exit
32
Experiment 12
.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
.data
.text
main:
exit:
li $v0,10
syscall
printInteger:
subu $sp,$sp,8
sw $ra,0($sp)
sw $a0,4($sp)
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
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
sw $v0,nodes($s0)
add $s0,4
sw $v0,nodes($s0)
47
add $s0,4
#---------------------------------------
move $s0,$t1
sw $zero,4($s0)
sw $zero,8($s0)
move $s0,$t2
sw $zero,4($s0)
sw $zero,8($s0)
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
.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
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
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
52
Experiment 15
.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
li $v0, 10
syscall
53
loop:
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
loop_main:
beq $s0, $s1, loop_end
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
## 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
# ------------------------------------------------------------------
.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 newline
li $v0,4 # print_string syscall code = 4
la $a0, lf
syscall
# Exit
li $v0,10 # exit
syscall
# ------------------------------------------------------------------
59
# Return value is stored in $v0
# Return address is stored in $ra (put there by jal instruction)
# Typical function operation is:
60
lw $s0,0($sp) # Restore $s0
addi $sp,$sp,4 # Adjust stack pointer
# ------------------------------------------------------------------
61
Experiment 18
# ------------------------------------------------------------------
.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 newline
li $v0,4 # print_string syscall code = 4
la $a0, lf
syscall
# Exit
li $v0,10 # exit
syscall
# ------------------------------------------------------------------
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:
# ------------------------------------------------------------------
64
Experiment 19
.data
prompt: .asciiz "Enter a number: "
.text
.globl main
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
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
.data
input: .asciiz "Enter the numbers(-1 to exit): "
newline: .asciiz "\n"
.text
main:
# prompt for input
la $a0,input
li $v0, 4
syscall
loop:
# input in t0
li $v0, 5
syscall
move $t0, $v0
ever:
move $a1 ,$t1
move $a0 ,$t0
jal gcd
68
# v0-1 are used to return from function
move $t1, $v0
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
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
.data
prompt: .asciiz "Please enter a number: "
false: .asciiz "False\n"
true: .asciiz "True\n"
.text
.globl main
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $a0, $v0 # pass the integer read as an argument into isPrime
71
jal isPrime
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
isPrimeLoop:
prime:
addi $v0, $zero, 1
73
j isPrimeEnd
notPrime:
add $v0, $zero, $zero
isPrimeEnd:
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8
jr $ra
74
Experiment 22
.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
li $v0, 4
la $a0, linked
76
syscall
jal printList
createNode:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $s0, 4($sp)
li $v0, 9
li $a0, 8
syscall
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)
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_while:
beqz $s0, printList_end
79
syscall # execute
printList_end:
lw $s0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra
80
Experiment 23
.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
.text
main:
la $a0, mat
lw $a1, size
81
jal mat_print
li $v0, 10
syscall
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
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
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
.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
# 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
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
89
Experiment 26
#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
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
loop:
li $t0, 1
beq $s0, $t0, loop_continue # if lower val == 1, continue
92
add $s2, $s2, $v0 # res = res + isPrime($s0)
loop_continue:
addi $s0, $s0, 1 # increment $s0
j loop
loop_end:
####################################################
# ---- 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
.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
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) );
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
.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
la $a0, string_space
li $v0, 4
syscall
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
la $a0,newline
li $v0, 4
syscall
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
101
Experiment 29
.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
#######################################################
# ---- 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
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