Array MIPS
Array MIPS
.data
myArray: .space 12 #12 bytes for 3 integers
.text
addi $s0, $zero, 4
addi $s1, $zero, 10
addi $s2, $zero, 12
addi $t0, $zero, 0 #index = $t0
sw $s0, myArray($t0) #Store contents of s0 in first position of array
addi $t0, $t0, 4 #increment the index by 4
sw $s1, myArray($t0) #Store contents of s2 in second position of array
addi $t0, $t0, 4 #increment the index by 4
sw $s2, myArray($t0) #Store the contents of s3 in third location of array
lw $t6, myArray($zero) #load the word in the first location of myArray into $t6
li $v0, 1
addi $a0, $t6, 0 #Print the value of t6
syscall
.data
myArray: .space 12 #declare an array of 3 elements
newline : .asciiz “\n”
.text
main:
addi $s0, $zero, 4
addi $s1, $zero, 10 #The three values are stored in 3 registers
addi $s2, $zero, 12
addi $t0, $zero, 10 #index is at t0
sw $s0, myArray($t0)
addi $t0, $t0, 4
sw $s1, myArray($t1)
addi $t0, $t0,4
addi $t0, $zero, 0
while:
beq $t0, 12, exit
lw $t6, myArray($t0)
addi $t0, $t0, 4
li $v0, 1
addi $a0, $t6, 0 #Print the no
syscall
li $v0,4
la $a0, newline #Print a new line
syscall
j while
exit:
Array Initialization
.data
myArray: .word 100:3 #Declare an array in RAM having 3 elements each initialized
to 100
newline : .asciiz “\n”
.text
main:
add $t0, $zero, 0
while:
beq $t0, 12, exit
lw $t6, myArray($t0)
addi $t0, $t0, 4
li $v0, 1
move $a0, $t6
syscall
#Print a new line
li $v0, 4
la $a0, newline
syscall
j while
exit:
li $v0, 10
syscall
Introduction to Recursion
Process: A program is in execution
Advantage:
Efficient for Searching and Sorting
Easier for complex problem, divides a problem into smaller problem until we reach base case
.data
promptMessage: .asciiz “enter a number to find factorial”
resultMessage: .asciiz “\n the factorial of the number is ”
theNumber: .word 0
theAnswer: .word 0
.text
.global main
main:
# Read the number from the user
li $v0,4
la $a0,promptMessage
syscall
li $v0,5
syscall
sw $v0,theNumber
# Call the factorial function
lw $a0,theNumber
jal findfactorial
sw $v0,theAnswer
# Display the results
li $v0,4
la $a0,resultMessage
syscall
li $v0,1
lw $a0, theAnswer
syscall
# Tell the OS that this is the end of program
li $v0,10
syscall
.globl findFactorial
findFactorial:
subu $sp,$sp,8
sw $ra,($sp) # storing the value of ra to stack
sw $s0,4($sp)
# Base Case
li $vo,1
beq $a0,0,factorialDone # Factorial will rewind
move $so,$a0 # findFactorial(number-1)
sub $ao,$ao,1
jal findFactorial # it will execute when
mult $v0,$s0,$v0 # recursion will be unwinding
factorialDone:
lw $ra,($sp) # Restoring ra from stack
lw $s0,4($sp)
addi $sp,Sp,8
jr $ra
Multi-Dimensional Array in MIPS
In reality memory is a single dimensional entity.
Ways to represent multi-dimensional array- Row major and Column major
int array[3][4]
[2][0] [2][1] [2][2] [2][3]
[1][0] [1][1] [1][2] [1][3]
[0][0] [0][1] [0][2] [0][3]
Row Major: Place all rows sequentially (one after the another)
8 9 10 11
11 Arr[2][3]
4 5 6 7 10 Arr[2][2]
0 1 2 3 9 Arr[2][1]
8 Arr[2][0]
7 Arr[1][3]
6 Arr[1][2]
5 Arr[1][1]
4 Arr[1][0]
3 Arr[0][3]
2 Arr[0][2]
1 Arr[0][1]
0 Arr[0][0]