I was trying to write some MIPS code that simulates a C function, but it seems like I've come across an obstacle that I cant get over it.
int partition(int f, int l) {
int pivot = v[l];
int i = f;
for (int j = f; j < l; j++)
if(v[j] < pivot)
swap(i++,j);
swap(i, l);
return (i);
}
that was the function in C and this is what I have written so far in MIPS assembly language:
partition:
addi $sp, $sp, -8 #creates space for 2 words
sw $ra, 0($sp) #stores ra in stack
sw $a1, 4($sp) #stores a1 in stack
la $s0, v #stores ad. of v[0] in s0
sll $t0, $a1, 2 #stores 4 * l in t0
add $t0, $t0, $s0 #stores ad. of v[l] in t0
lw $s1, 0($t0) #loads v[l] in s1, s1 is pivot
add $t1, $a0, $zero #loads f in t1, t1 is 'i'
add $t2, $a0, $zero #loads f in t2, t2 is 'j'
for1: slt $t3, $t2, $a1 #checks if j < l
beq $t3, $zero, exit
sll $t4, $t2, 2 #t4 stores 4 * j
add $t4, $t4, $s0 #t4 stores ad. of v[j]
lw $t5, 0($t4) #t5 stores value of v[j]
slt $t3, $t5, $s1 #checks if v[j] < pivot
beq $t3, $zero, bfor #jumps to next repetition of the loop
add $a0, $t1, $zero #a0 is i
add $a1, $t2, $zero #a1 is j
jal swap #call swap
addi $t1, $t1, 1 # i++
j bfor #continue loop
bfor: lw $a1, 4($sp) #restores a1
addi $t2, $t2, 1 # j++
j for1 #continue loop
exit: add $a0, $t1, $zero #a0 is i
lw $a1, 4($sp) #restores initial a1
jal swap #call swap
add $v0, $t1, $zero #return i
lw $ra, 0($sp) #restore initial ra
addi $sp, $sp, 8
jr $ra
It is stated that f and l are stored in $a0 and $a1 respectively, the swap function is already created and the vector v is labeled in the memory. I cant understand where my mistake is and any help is welcome. Thanks in advance!
partition:
addi $sp, $sp, -16 #adjust stack for 4 items
sw $ra, 0($sp) #store ra in stack
sw $s0, 4($sp) #store s0
sw $a0, 8($sp) #store a0
sw $a1, 12($sp) #store a1
la $s0, v #load address of v0 in s0
sll $t0, $a1, 2 #t0 stores 4 * l
add $t0, $t0, $s0 #t0 stores ad. of v[l]
lw $t1, 0($t0) #t1 contains v[l] (t1 pivot)
add $t2, $a0, $zero #t2 is f (t2 i)
add $t3, $a0, $zero #t3 is f (t3 j)
for1: slt $t4, $t3, $a1 #sets 1 to t4 if j < l
beq $t4, $zero, exit
sll $t5, $t3, 2 #t5 is 4 * j
add $t5, $t5, $s0 #t5 stores ad. of v[j]
lw $t6, 0($t5) #t6 has the value of v[j]
slt $t4, $t6, $t1 #sets 1 to t4 if v[j] < pivot
beq $t4, $zero, bfor
add $a0, $t2, $zero #a0 is i
add $a1, $t3, $zero #a1 is j
addi $sp, $sp, -12 #adjust stack for 3 items
sw $t1, 0($sp)
sw $t2, 4($sp)
sw $t3, 8($sp) #store pivot, i, j in stack
jal swap #call swap
lw $t1, 0($sp)
lw $t2, 4($sp)
lw $t3, 8($sp) #restores pivot, i, j before the call
addi $sp, $sp, 12 #return items to stack
lw $a1, 12($sp) #restore initial a1
addi $t2, $t2, 1 #i++
j bfor
bfor: addi $t3, $t3, 1 #j++
j for1 #continue loop
exit: add $a0, $t2, $zero #a0 is i
addi $sp, $sp, -4 #adjust stack for 1 item
sw $t2, 0($sp) #store i
jal swap #calls swap
lw $v0, 0($sp) #returns i
addi $sp, $sp, 4 #returns item to stack
lw $ra, 0($sp) #restore initial ra in stack
lw $s0, 4($sp) #restore initial s0
lw $a0, 8($sp) #restore initial a0
lw $a1, 12($sp) #restore initial a1
addi $sp, $sp, 16 #return items to stack
jr $ra
I found the solution. Thank you to those who replied.
The important change is that the values in registers $t1, $t2, and $t3 are preserved across the call to swap, because swap modifies them.
Related
Recently, I am learning MIPS. Professor give us an example of his code, asking us to find out the value. My problem is that after I type in his code, the run time exception comes out at " lw $t1, 0($s0) ".
line 8: Runtime exception at 0x0040001c: address out of range 0x00000000
Can someone helps me?
addi $t7, $zero,0x10010000
lw $t0, 20($t7)
srl $t0, $t0, 16
andi $t0, $t0, 0x00000FF
andi $s0, $t7, 16
lw $t1, 0($s0)
andi $t1, $t1, 0x0000FF00
srl $t1, $t1, 8
addi $s0, $s0, -8
lw $t2, 0($s0)
sll $t2, $t2, 24
srl $t2, $t2, 24
addi $s0, $zero, 1
sll $s0, $s0, 4
lw $t3, 0($t7)
srlv $t3, $t3, $s0
andi $t3, $t3, 255
addi $s0, $zero, 1
sll $s0, $s0, 2
add $s0, $s0, $t7
lw $t4, 0($s0)
andi $t4, $t4, 0x00FF0000
srl $t4, $t4, 16
I am working with mergesort mips operating on Mars
i run it, the array is arranged but it has some troubles and it announces that error in : invalid program counter value: 0x00000000
Go: execution terminated with errors
please help me
thank you very much
.data # Data declaration section
text1: .asciiz "My array"
text2: .asciiz "Sorted Array"
newline: .asciiz "\n"
space: .asciiz " "
Myarray: .word 9 5 6 8 7 2
mangx: .word 0:20
mangy: .word 0:20
.text
main:
la $a0, Myarray # load dia chi myarray $a1
addi $t1, $zero,4 # size=20
and $a1, $zero, $zero # l=0 a1
addi $a3,$zero,3 #r=19
li $v0, 4
la $a0, text1 #in text1
syscall
li $v0, 4
la $a0, newline #in newline
syscall
and $t4, $zero, $zero # khoi tao i=0 $t4
j LoopPrintUn # nhay den ham in myarray
LoopPrintUn:
la $a0, Myarray # load dia chi myarray $a1
#While (i < length)
slt $t6, $t4, $t1 # neu i < size thi $t6=1, neu khong thi $t6=0
beq $t6, $zero, call # neu $t6=0 thi re qua ham in Sortedarray
# Load Array[i] and print it
sll $t0, $t4, 2 # i * 4
add $t6, $a0, $t0 # chuyen den dia chi phan tu thu i
li $v0, 1 # ham in integer
lw $a0, 0($t6) # load gia tri tai dia chi thu i
syscall # in
li $v0, 4
la $a0, space
syscall #in khoang trang
addi $t4, $t4, 1 # i ++
j LoopPrintUn # lap ham in myarray
MergeSort:
addi $sp, $sp, -20
sw $ra,0($sp)
sw $a3, 12($sp) # r
sw $a1, 8($sp) # l
sw $a0, 4($sp) # a[]
slt $t6, $a1, $a3 # neu l < r $t6=1 neu khong $t6=0
beq $t6, $zero, Exit2 # $t6=0 thi exit2
add $t1, $a1, $a3 # temp1 = l + r
div $t1, $t1, 2 # temp1 = temp1 / 2
add $a3,$t1,$zero
sw $t1,16($sp)
# add $a3, $a2, $zero # m=(l+r)/2 (m thay cho cho r trong mergesort(arr,l,r))
jal MergeSort # mergesort(arr,l,m)
lw $a1,16($sp) #load m
addi $a1,$a1,1
lw $a3,12($sp)#load r
#addi $sp, $sp, 24
jal MergeSort # mergesort(arr,m+1,r)
lw $a0, 4($sp)
lw $a1, 8($sp)
lw $a2, 16($sp)
lw $a3, 12($sp)
jal merge # merge(a,l,m,r)
Exit2:
lw $ra,0($sp)
lw $a0, 4($sp)
lw $a1, 8($sp)
lw $a2, 16($sp)
lw $a3, 12($sp)
addi $sp, $sp, 20
jr $ra
merge:
addi $sp,$sp,-32
sw $ra,0($sp)
sw $s0,4($sp) # bien i
sw $s1,8($sp) # bien j
sw $s2,12($sp) # bien k
sw $s3,16($sp) # bien n1
sw $s4,20($sp) # bien n2
sw $s5,24($sp) # mang X
sw $s6,28($sp) # mang y
la $s5,mangx
la $s6,mangy
sub $s3,$a2,$a1 # m-l
addi $s3,$s3,1 #m-l+1
sub $s4,$a3,$a2 # r-m
move $t0,$0 # bien dem o
move $t1,$0 # bien dem p
loop1:
slt $t3,$t0,$s3 # so sanh o<n1
beq $t3,$0,loop2 # go to toi loop 2 neu $t3=0
sll $t2,$t0,2 # o*4
add $t2,$s5,$t2 # X[0]
add $t5,$a1,$t0 # l+o
sll $t5,$t5,2 # (l+o)*4
add $t5,$a0,$t5 # arr[l+o]
lw $t5,0($t5)
sw $t5,0($t2) # x[o]=arr[l+o]
addi $t0,$t0,1 #o++
j loop1
loop2:
# xuly
slt $t4,$t1,$s4 # so sanh o<n1
beq $t4,$0,exit # go to toi exit neu $tt4=0
addi $t6,$a2,1 # m+1
add $t6,$t6,$t1 #m+1+p
sll $t6,$t6,2 #(m+1+p)*4
sll $t7,$t1,2 # p*4
add $t6,$a0,$t6 # arr[m+1+p]
lw $t6,0($t6)
add $t7,$s6,$t7 # y[p]
sw $t6,0($t7) # y[p]=arr[m+1+p]
addi $t1,$t1,1 #o++
j loop2
#-------------
exit:
move $s0,$0 #i=0
move $s1,$0 # j=0
add $s2,$a1,$0 #k=l
while:
slt $t0,$s0,$s3 #i<n1
beq $t0,$0,loop3
slt $t1,$s1,$s4 #j<n2
beq $t1,$0,loop3
sll $t2,$s0,2 #i*4
sll $t3,$s1,2 #j*4
add $t2,$s5,$t2 #x[i]
lw $t2,0($t2)
add $t3,$s6,$t3 #y[j]
lw $t3,0($t3)
slt $t6,$t2,$t3 # x[i]<y[j]
beq $t6,$0,else #x[i] >y[j] thì làm else còn x[i] <= y[j] lam if
sll $t7,$s2,2 #k*4
add $t7,$a0,$t7 #arr[k]
sw $t2,0($t7) #arr[k]=x[i]
addi $s0,$s0,1 #i++
addi $s2,$s2,1 #k++
else:
sll $t7,$s2,2 #k*4
add $t7,$a0,$t7 #arr[k]
sw $t3,0($t7) #arr[k]=y[j]
addi $s1,$s1,1 #j++
addi $s2,$s2,1 #k++
j while
loop3:
slt $t0,$s0,$s3 #i<n1
beq $t0,$0,loop4
sll $t1,$s2,2 #k*4
add $t1,$a0,$t1 #arr[k]
sll $t2,$s0,2 #i*4
add $t2,$s5,$t2 #x[i]
lw $t2,0($t2)
sw $t2,0($t1) #arr[k]=x[i]
addi $s0,$s0,1 #i++
addi $s2,$s2,1 #k++
j loop3
loop4:
slt $t0,$s1,$s4 #j<n2
beq $t0,$0,exit1
sll $t1,$s2,2 #k*4
add $t1,$a0,$t1 #arr[k]
sll $t2,$s1,2 #j*4
add $t2,$s6,$t2 #y[j]
lw $t2,0($t2)
sw $t2,0($t1) #arr[k]=y[j]
addi $s1,$s1,1 #j++
addi $s2,$s2,1 #k++
j loop4
exit1:
lw $ra,0($sp)
addi $sp,$sp,32
jr $ra
call:
jal MergeSort
I'm writing a mips program to calculate exponent, but get an error:
Runtime exception at 0x00400158: fetch address not aligned on word boundary 0x400fffff
Any ideas?
The problem line is lw $v0, 0($t0) # $v0 = rev_binary[idx]
.data
prompt1: .asciiz "Enter a base: "
prompt2: .asciiz "Enter an exponential: "
b: .word 0
e: .word 0
rev_binary: .word 0
i: .word 0
result: .word 1
.text
main:
la $a0, prompt1 # print prompt1
addi $v0, $0, 4
syscall
addi $v0, $0, 5 # read b
syscall
move $t0, $v0 # $t0 = b
la $a0, prompt2 # print prompt2
addi $v0, $0, 4
syscall
addi $v0, $0, 5 # read e
syscall
move $t1, $v0 # $t1 = e
addi $sp, $sp, -8 # 2 * 4 = 8 bytes
sw $t1, 4($sp)
sw $t0, 0($sp)
jal power
move $a0, $v0
addi $v0, $0, 1
syscall # print the return value
j end
power:
# save $ra and $fp
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $fp, 0($sp)
#copy $sp to $fp
addi $fp, $sp, 0
# allocate local variables
addi $sp, $sp, -20
# save $sp into $fp
move $fp, $sp
#rev_binary = [ 0 ] * e
lw $a0, 32($sp)
sll $a0, $a0, 2 # $a0 = 4 * e(e << 2)
addi $a0, $a0, 4 # $a0 = 4 * e + 4
addi $v0, $0, 9 # $v0 = 9
syscall # allocate memory
sw $v0, 0($sp)
lw $t0, 32($sp) # $t0 = e
sw $t0, 0($v0) #store the size of rev_binary
sw $0, 4($sp)
while0:
# while e != 0
lw $t0, e # $t0 = e
beq $t0, $0, while0_end # break if e == 0
# e_half = e >> 1
lw $t0, 32($sp) # allocate e_half
srl $t1, $t0, 1 # $t1 = $t0 >> 1
sw $t1, 8($sp) # e_half = $t1
# rev_binary[i] = e - 2 * e_half
lw $t0, 8($sp) # $t0 = e_half
sll $t0, $t0, 1 # $t0 = e_half * 2
lw $t1, e # $t1 = e
sub $t0, $t1, $t0 # $t0 = e - e_half * 2
lw $t1, 4($sp) # $t1 = i
sll $t1, $t1, 2
addi $t1, $t1, 4 # $t1 = i * 4 + 4
lw $t2, rev_binary # $t3 = the address of rev_binary
add $t1, $t1, $t2 # $t1 = the address of rev_binary[i]
sw $t0, 0($t1) # rev_binary[i] = $t0
# i += 1
lw $t0, 4($sp) # $t0 = i
addi $t0, $t0, 1 # $t0 += 1
sw $t0, 4($sp) # i = $t0
# e = e_half
lw $t0, 8($sp) # $t0 = e_half
sw $t0, 32($sp) # e = $t0
addi $sp, $sp, 4 # release local variable n_half
j while0
while0_end:
# rev_binary = rev_binary[:i]
lw $t0, 0($sp) # $t0: the address of rev_binary
lw $t1, 4($sp) # $t1 = i
sw $t1, 0($t0) # set the size of rev_binary to i
# result = 1
addi $t0, $0, 1 # $t0 = 1
sw $t0, 12($sp) # result = $t0
# for j in range(i - 1, -1, -1):
addi $sp, $sp, -4 # allocate j
lw $t0, i # $t0 = i
addi $t0, $t0, -1 # $t0 -= 1
sw $t0, 0($sp) # j = $t0 = i - 1
# idx = len(rev_binary) - 1
lw $t0, 4($sp) # $t0 = i = len(rev_binary)
addi $t0, $t0, -1 # $t0 -= 1
sw $t0, 16($sp) # idx = $t0
while1: # while idx >= 0
lw $t0, 16($sp) # idx = $t0
blt $t0, $0, while1_end # break if idx < 0
# result = result * result
lw $t0, 12($sp) # t0 = result
mul $t0, $t0, $t0
sw $t0, 12($sp) # $t0 = result
# if rev_binary[idx]
lw $t0, 16($sp) # $t0 = idx
sll $t0, $t0, 2 # $t0 = idx * 4
addi $t0, $t0, 4 # $t0 = idx * 4 + 4
lw $t1, 0($sp) # $t1 = the address of rev_binary
add $t0, $t1, $t0 # $t0 = the address of rev_binary[idx]
lw $v0, 0($t0) # $v0 = rev_binary[idx]
if:
beq $t0, $0, end_if # end if if (idx == 0)
# result = result * b
lw $t0, 12($sp) # $t0 = result
lw $t1, 28($sp) # $t1 = b
mul $t0, $t0, $t1 # $t0 = $t0 * $t1
sw $t0, 12($sp) # result = $t0
end_if:
# idx = idx - 1
lw $t0, 16($sp) # $t0 = idx
addi $t0, $t0, -1 # $t0 -= 1
sw $t0, 16($sp) # idx = $t0
j while1 # loop
while1_end:
lw $v0, 12($sp) # $v0 = result
lw $t0, 20($sp) # reset old $fp
move $fp, $t0
lw $t0, 24($sp) # reset old $ra
move $ra, $t0
addi $sp, $sp, 36 # release local variables
jr $ra # return
end:
# exit
You can only load words from aligned addresses. The address also looks strange since it has 0xffff in the end.
Looking at the code you have
lw $t1, 0($sp) # $t1 = the address of rev_binary
but before that you store j to (0)$sp, so that can't contain the address of rev_binary. Maybe you need
la $t1, rev_binary # $t1 = the address of rev_binary
instead?
I am trying to translate my fibonacci c code to MIPS assembly code, however, when I run the MIPS code to my MIPS simulator it doesn't seem to end.
C Code:
int fibo(int n)
{
if(n<2) return 1;
else f(n-1)+f(n-2);
}
int main()
{
fibo(5);
}
Assembly Code:
main: addi $sp, $sp, -4
sw $ra, 0($sp)
addi $a0, $zero, 5
jal fibo;
lw $ra, 0($sp)
addi $sp, $sp, 4
fibo: addi $sp, $sp, -12
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $ra, 8($sp)
slti $t0, $a0, 2
beq $t0, $zero, ELSE
addi $v0, $zero, 1
jr $ra
ELSE: addi $s0, $a0, 0
addi $a0, $a0, -1
jal fibo;
addi $s1, $v0, 0
addi $a0, $s0, -2
jal fibo
add $s1, $s1, $v0
j EXIT
EXIT: lw $s0, 0($sp)
lw $s1, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra
A few issues here.
First, your main function was missing its terminating jr $ra, meaning that main fell through into fibo after being executed.
Second, in your if statement, where you return 1, you call jr $ra directly, which means the stack goes unrestored. I swapped this out with a call to j EXIT.
Finally, at the end of your else you add f(n-1) and f(n-2) into $s1. This register should be $v0 as you are intending to return this result.
The fixed up code is as follows:
main:
addi $sp, $sp, -4
sw $ra, 0($sp)
addi $a0, $zero, 5
jal fibo
move $a0 $v0
li $v0 1
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
fibo:
addi $sp, $sp, -12
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $ra, 8($sp)
slti $t0, $a0, 2
beq $t0, $zero, ELSE
addi $v0, $zero, 1
j EXIT
ELSE:
addi $s0, $a0, 0
addi $a0, $a0, -1
jal fibo
addi $s1, $v0, 0
addi $a0, $s0, -2
jal fibo
add $v0, $s1, $v0
EXIT:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra
Here is my code for the same,but it is written in RISC-V
.text
#x5 me n hai
#x7 me output
li x5 7
li x6 2
jal x1 FIB
j EXIT
FIB:
bge x5 x6 REC
addi x7 x5 0
jalr x0 0(x1)
#Handled Base Case
REC:
# Handling Rec. Case
addi sp sp -12
sw x1 0(sp)
sw x5 4(sp)
addi x5 x5 -1
jal x1 FIB
sw x7 8(sp)
lw x5 4(sp)
addi x5 x5 -2
jal x1 FIB
lw x13 8(sp)
add x7 x13 x7
lw x1 0(sp)
addi sp sp 12
jalr x0 0(x1)
EXIT:
In addition to the answer of Konrad (https://stackoverflow.com/a/23185991/3729904), I noticed that there is an error in your algorithm. If n<2 the fib algorithm should return n instead of 1.
This can be fixed changing the line:
addi $v0, $zero, 1
into:
add $v0, $zero, $a0
So i have to write a bubblesort programm in mips using QtSpim for a class but i get an Exception 4 [Adress error in inst/data fetch]. I have searched in other topics and use the .align 2 directive before defining an array for 5 integers but it still isn't getting fixed.
Here's the code:
.text
.globl main
main:
la $t1, array #sets the base adress of the array to t1
la $a0, in_prompt
li $v0, 4
syscall
li $t2, 0 #init to 1
read_loop:
beq $t2, 5, read_key #break if t2 = 5
la $a0, num_prompt
li $v0, 4
syscall #"Give number"
#move $a0, $t2
#li $v0, 1
#syscall #current number to be read
li $v0, 5 #read int. 5 times
syscall
sw $v0, ($t1) #move input from v0 to the array
addi $t1, $t1, 4 #move t1 to the next position in the array
addi $t2, $t2, 1 #increment counter (t2) by 1
j read_loop
read_key:
la $a0, search_q
li $v0, 4
syscall #print query prompt
li $v0, 5
syscall
move $t3, $v0 #number we're looking for (KEY)
move $a0, $t1 #array to pass as arguement
jal bubblesort
move $a0, $v0 #move into a0 the sorted array
move $a1, $t3 #move the KEY into a1 as arguement
jal binarysearch
move $t1, $a0 #move into t1 the array
move $t0, $a1 #move into t0 the KEY
move $t3, $v0 #move into t3 the result of binarySearch
beq $t3, -1, not_found #if key was not found
move $a0, $t0
li $v0, 1
syscall
la $a0, found_pr
li $v0, 4
syscall
j print_array
not_found:
move $a0, $t0
li $v0, 1
syscall
la $a0, not_found_pr
li $v0, 4
syscall
j print_array
print_array:
li $t2, 1 #init to 1
print_loop:
beq $t2, 5, EXIT
lw $a0, ($t1)
li $v0, 1
syscall
addi $t1, $t1, 4
addi $t2, $t2, 1
j print_loop
EXIT:
li $v0, 10
syscall
##############binarysearch#################
binarysearch:
addi $sp, $sp, -24 #reserve space for 6 elements
sw $a0, 0($sp) #push a0 (array)into stack
sw $a1, 4($sp) #push a1(KEY) into stack
sw $s0, 8($sp) #push s0
sw $s1, 12($sp) #push s1
sw $s2, 16($sp) #push s2
sw $ra, 20($sp) #push ra
sw $s3, 24($sp) #push s3
li $s0, 0 #low = 0
li $s1, 4 #high = 4
while:
bgt $s0, $s1, exit_search #if low > high branch
move $a1, $s0 #move into a1 low
move $a2, $s1 #move into a2 high
jal calc_middle #jump to calc_middle
move $s2, $v0 #move into s2 the return value of calc_middle
lw $a0, 0($sp) #restore into a0 the 1st stack el(array)
lw $a1, 4($sp) #restore into a1 the 2nd stack el(KEY)
add $a0, $a0, $s2 #move the array to middle
lw $s3, ($a0) #load into s3 the a[middle]
beq $a1, $s3, exit_search_found #break if KEY == a[middle]
blt $a1, $s3, less_t_middle #if the key is less than the middle element
addi $s0, $s2, 1 #if the key is greater than the middle element set new low
j while
less_t_middle:
addi $s1, $s2, -1 #new high
j while
exit_search_found:
move $v0, $s2 #return found
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 24
jr $ra
exit_search:
li $v0, -1 #return -1
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 24
jr $ra
##############calc_middle##################
calc_middle:
add $a1, $a1, $a2
sra $a1, $a1, 1
move $v0, $a1
jr $ra
##############bubblesort###################
bubblesort:
addi $sp, $sp, -12
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
li $s2, 4 #j = 5
li $s1, 0 #i = 0
outer_loop:
addi $s2, $s2, -1 #i = j - 1
blt $s2, $zero, exit_sort
inner_loop:
bgt $s1, $s2, outer_loop #if i > j - 1
lw $s3, 0($a0) #load into s3 the a[i]
lw $s4, 4($a0) #load into s4 the a[i+1]
bgt $s3, $s4, swap #if a[i] > a[i+1]
addi $s1, $s1, 1 #i++
j inner_loop
swap: move $s0, $s3 #tmp = a[i]
move $s3, $s4 #a[i] = a[i+1]
move $s4, $s0 #a[i+1] = tmp
addi $a0, $a0, 4 #point to the next element ????
addi $s1, $s1, 1 #i++
j inner_loop
exit_sort:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
addi $sp, $sp, 8
move $v0, $a0 #pass into v0 the sorted array
jr $ra
.data
.align 2
array: .space 20
in_prompt: .asciiz "Enter 5 numbers:\n"
num_prompt: .asciiz "Give number: "
search_q: .asciiz "what are you looking for?\n"
not_found_pr: .asciiz " not found in array: \n"
found_pr: .asciiz " found in array: \n"