I have spent the greater part of today trying to properly translate this c code to mips and I'm struggling on the formatting and the second half of the pattern. The second part of the pattern breaks in a new way everytime i make a small change to the code. Im lookin to learn so any other tips regarding mips and mistakes made would help a ton.
.data
prompt: .asciiz "Please enter the number of Columns: "
s: .asciiz " "
n: .asciiz "\n"
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0,5
syscall
move $s0, $v0 #$s0 holds N
li $s3, 0 #$s3 holds I
li $s4, 0 #$s4 holds J
li $s5, 1 #s5 holds K
add $s2, $zero, $s0 #holds N-i
li $s2, 0
add $s2, $zero, $s0
addi $s2, $s2, 1
sub $s2, $s2, $s3
for1:
blt $s0,$s3, bottom
addi $s3, $s3, 1
li $s4, 0
j for2
for2:
blt $s3, $s4,for3
li $v0, 4
la $a0, s
syscall
addi $s4, $s4, 1
li $s2, 0
add $s2, $zero, $s0
addi $s2, $s2, 1
sub $s2, $s2, $s3
li $s5 , 1
j for2
for3:
blt $s2, $s5,newline1
li $t0, 0
add $t0, $zero, $s5
li $v0, 1
move $a0, $t0
syscall
li $t0, 0
addi $s5, $s5, 1
j for3
bottom:
li $s3, 1 #$s3 holds I
li $s4, 1 #$s4 holds J
li $s5, 1 #s5 holds K
add $s2, $zero, $s0 #holds N-i
addi $s2, $s2, 1
sub $s2, $s2, $s3
for4:
blt $s0,$s3, exit
addi $s3, $s3, 1
li $s4, 0
li $s2, 0
add $s2, $zero, $s0
sub $s2, $s2, $s3
j for5
for5:
blt $s2, $s4,for6
li $v0, 4
la $a0, s
syscall
addi $s4, $s4, 1
li $t2, 1
addi $t2, $s3, 0
li $s5 , 1
j for5
for6:
blt $t2, $s5,newline2
li $t3, 2
addi $t3,$s3, 0
sub $t3,$t3,$s5
li $v0, 1
move $a0, $t3
syscall
addi $s5, $s5, 1
j for6
newline1:
li $v0, 4
la $a0, n
syscall
j for1
newline2:
li $v0, 4
la $a0, n
syscall
j for4
exit:
li $v0, 10
syscall
here is the c code:
#include <stdio.h>
int main(void) {
int n;
printf("Please enter the number of columns:"); // [0.25 mark]
scanf("%d",&n); // [0.25 mark]
//printing the upper part of the pattern.
for(int i=0;i<n;i++){ // [4 mark]
for(int j=0;j<i;j++) { // [4 mark]
printf(" "); // [0.25 mark]
}
for(int k=1;k<=n-i;k++){ // [4 mark]
printf("%d", k); // [0.5 mark]
}
printf("\n"); // [0.25 mark]
}
//printing the lower part of the pattern.
for(int i=1;i<n;i++){ // [4 mark]
for(int j=1;j<n-i;j++){ // [4 mark]
printf(" "); // [0.25 mark]
}
for(int k=1;k<=i+1;k++){ // [4 mark]
printf("%d", (i+2 - k)); // [0.5 mark]
}
printf("\n"); // [0.25 mark]
}
return 0; // [0.25 mark]
}
Any help with this is greatly apprciated!
update working code
.data
prompt: .asciiz "Please enter the number of Columns: "
s: .asciiz " "
n: .asciiz "\n"
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0,5
syscall
move $s0, $v0 #$s0 holds N
li $s3, 0 #$s3 holds I
li $s4, 0 #$s4 holds J
li $s5, 1 #s5 holds K
add $s2, $zero, $s0 #holds N-i
li $s2, 0
add $s2, $zero, $s0
addi $s2, $s2, 1
sub $s2, $s2, $s3
for1:
ble $s0,$s3, bottom
li $s4, 0
j for2
for2:
blt $s3, $s4,for3
li $v0, 4
la $a0, s
syscall
addi $s4, $s4, 1
li $s2, 0
add $s2, $s0, 0
sub $s2, $s2, $s3
li $s5 , 1
j for2
for3:
blt $s2, $s5,newline1
li $t0, 0
add $t0, $zero, $s5
li $v0, 1
move $a0, $t0
syscall
li $t0, 0
addi $s5, $s5, 1
j for3
bottom:
li $s3,1 #$s3 holds I
li $s4, 1 #$s4 holds J
li $s5, 1 #s5 holds K
li $s2, 0
add $s2, $s0, 0 #holds N-i
j for4
for4:
blt $s0, $s3, exit
li $s2, 0
add $s2, $s0, 0
sub $s2, $s2, $s3
li $s4, 1
j for5
for5:
blt $s2, $s4,for6
li $v0, 4
la $a0, s
syscall
addi $s4, $s4, 1
li $t1, 1
li $t2, 2
add $t1, $t1, $s3
add $t2, $t2,$s3
li $s5, 1
j for5
for6:
blt $t1, $s5,newline2
li $t3, 0
sub $t3, $t2,$s5
li $v0, 1
move $a0, $t3
syscall
addi $s5, $s5, 1
j for6
newline1:
addi $s3, $s3, 1
li $v0, 4
la $a0, n
syscall
j for1
newline2:
addi $s3, $s3, 1
li $v0, 4
la $a0, n
syscall
j for4
exit:
li $v0, 10
syscall
This is the proper solution to the question. The mark comments are for grading purposes as this is an assignment. The issue I ran to was I was incrementing I within the first for loop moving it to the newline function helped fix the issue I was having. The pattern was only meant to reach 1-9. If there are any new questions ill try my best to answer them.
.data
prompt: .asciiz "Please enter the number of Columns: "
s: .asciiz " "
n: .asciiz "\n"
.text
main:
li $v0, 4
la $a0, prompt # asks column amount
syscall
li $v0,5
syscall
move $s0, $v0 #$s0 holds N
li $s3, 0 #$s3 holds I
li $s4, 0 #$s4 holds J #declares locations for variables
li $s5, 1 #s5 holds K
add $s2, $zero, $s0 #holds N-i
li $s2, 0
add $s2, $zero, $s0
addi $s2, $s2, 1 # sets up n-i
sub $s2, $s2, $s3
for1: # first for loop
ble $s0,$s3, bottom # break when less than to bottom func
li $s4, 0 # resets j to 0 eveery iteration
j for2 #jumps to second loop
for2:
blt $s3, $s4,for3 # breaks when less than to for3
li $v0, 4 #prints spaces
la $a0, s
syscall
addi $s4, $s4, 1 # increments J
li $s2, 0
add $s2, $s0, 0
sub $s2, $s2, $s3 #updates n-i
li $s5 , 1
j for2 #repeats loop
for3:
blt $s2, $s5,newline1 #break when less than to newline1
li $t0, 0
add $t0, $zero, $s5
li $v0, 1
move $a0, $t0 #prints numbers (K)
syscall
li $t0, 0
addi $s5, $s5, 1
j for3 # repeats loop
bottom:
li $s3,1 #$s3 holds I
li $s4, 1 #$s4 holds J #sets variables for second 3 loops
li $s5, 1 #s5 holds K
li $s2, 0
add $s2, $s0, 0 #holds N-i
j for4
for4:
blt $s0, $s3, exit #break to exit when less than
li $s2, 0
add $s2, $s0, 0
sub $s2, $s2, $s3 #sets up n-i
li $s4, 1 #sets j to 1
j for5
for5:
blt $s2, $s4,for6 #break to sixth loop
li $v0, 4
la $a0, s #prints spaces
syscall
addi $s4, $s4, 1 #increments j
li $t1, 1
li $t2, 2 #setting up i+1 and i+2
add $t1, $t1, $s3
add $t2, $t2,$s3
li $s5, 1 #sets k to 1
j for5 #repeats loop
for6:
blt $t1, $s5,newline2 #breaks to newline 2 func
li $t3, 0
sub $t3, $t2,$s5 #prints numbers
li $v0, 1
move $a0, $t3
syscall
addi $s5, $s5, 1
j for6 #repeats loops
newline1:
addi $s3, $s3, 1 #increments i
li $v0, 4
la $a0, n #prints new line
syscall
j for1 # jumps to for loop 1
newline2:
addi $s3, $s3, 1 #increments i
li $v0, 4 #prints new line
la $a0, n
syscall
j for4 #jumps to for loop 4
exit: #ends program
li $v0, 10
syscall
Related
How to print the following two patterns in MIPS:
a)
1
12
123
1234
12345
b)
1
22
333
4444
55555
Thanks!!
Part a)
.data
prompt: .asciiz "enter range: "
space: .asciiz " "
newline: .asciiz "\n"
.text
.globl main
main:
#li $t2, 0
li $t1, 6
li $t2, 1
li $t3, 6
for:
beq $t2, $t3, end
addi $t2, $t2, 1
li $t0, 1 # count = 0
while:
beq $t0, $t2, newlineloop
move $a0, $t0
# printing result
li $v0, 1
syscall
addi $t0, $t0, 1
j while
newlineloop:
li $v0, 4
la $a0, newline
syscall
j for
end:
li $v0, 10
syscall
Part b)
.data
prompt: .asciiz "enter range: "
space: .asciiz " "
newline: .asciiz "\n"
.text
.globl main
main:
#li $t2, 0
li $t1, 6
li $t2, 0
li $t3, 5
for:
beq $t2, $t3, end
addi $t2, $t2, 1
li $t0, 0 # count = 0
while:
beq $t0, $t2, newlineloop
move $a0, $t2
# printing result
li $v0, 1
syscall
addi $t0, $t0, 1
j while
newlineloop:
li $v0, 4
la $a0, newline
syscall
j for
end:
li $v0, 10
syscall
.data
newl: .asciiz "\n"
.text
#Code example
.globl main
main:
li $t0,1
li $t1,6
loop:
beq $t0,$t1,endloop
li $t2,1
loop2:
beq $t2,$t1,endloop2
move $a0,$t2
li $v0,1
syscall
addi $t2,1
j loop2
endloop2:
addi $t0,1
li $v0,4
la $a0,newl
syscall
j loop
endloop:
li $t0,1
li $t1,7
li $t2,1
loop3:
beq $t0,$t1,endloop3
li $t2,1
loop4:
beq $t2,$t0,endloop4
move $a0,$t2
li $v0,1
syscall
addi $t2,1
j loop4
endloop4:
li $v0,4
la $a0,newl
syscall
addi $t0,1
j loop3
endloop3:
li $v0,10
syscall
I am trying to fix this problem for hours!!!!!
When I enter 8 integers for the array and then enter 0 to finish reading, when the loop section starts it goes to infinity loop!!
I can't find where's the problem, when I remove the addiu from the $s2 array it works fine but doesn't store the $t1 on the array!
Please HELP!!
.data
array: .space 32
percentage_Array: .space 32
newLine: .asciiz "\n"
.text
main:
la $s1, array
la $s2, percentage_Array
read_numbers:
li $v0, 5
syscall
sw $v0, 0($s1)
addiu $s1, $s1, 4
beqz $v0, continue
j read_numbers
continue:
la $s1, array
loop:
lw $t0, 0($s1)
addiu $s1, $s1, 4
beqz $t0, exit
move $a1, $t0
jal calculate_Ones_and_Bits
move $s6, $v0
move $s7, $v1
#put the total number of ones
add $s0, $s0, $s7
#calculate the percentage
addi $t5, $zero, 100
mult $s7, $t5
mflo $t1
div $t1, $s6
mflo $t1
#-----I THINK HERE IS MY PROBLEM------
#Put the percentages on percentage_Array array
sw $a1, 0($s2)
addiu $s2, $s2, 4
#Check which percentages are greater than 50 and print the numbers
which fulfill the condition
slti $t2, $t1, 50
beq $t2, 1, loop
li $v0, 1
move $a0, $t0
syscall
#lines 66,67,68 are extra just for printing in new line more clearly
li $v0, 4
la $a0, newLine
syscall
j loop
exit:
#lines 73,74,75 are also extra just to print the value of s0, it is
printed here so it
#will be executed after the loop has finished
li $v0, 1
move $a0, $s0
syscall
#tell the OS to exit/finish
li $v0, 10
syscall
I'm working on a sorting program that sorts an array of integers, for which I copy the array entered in by the user into a new array to be mutated. However, I'm not sure if I'm doing it correctly.
.globl main
.data
input: .asciiz "Enter the size of the array: \n"
entries: .asciiz "Enter the elements of the array, one line at a time: \n"
output: .asciiz "Original array and then sorted array: \n"
space: .asciiz " "
.text
main:
subi $sp, $sp 32
sw $ra, 0($sp)
sw $t0, 4($sp) # the size of the array
sw $t4, 8($sp) # the number 4
sw $t1, 12($sp) # temporary
sw $t2, 16($sp) # array original
sw $t3, 20($sp) # specific element
sw $s1, 24($sp) # copied array
sw $t5, 28($sp) # number to copy
la $a0, input
li $v0, 4
syscall
# get the size
li $v0, 5
syscall
move $t0, $v0
# allocate space for the array on the heap
li $t4, 4
mul $t1, $t0, $t4
li $v0, 9
move $a0, $t1
syscall
move $t2, $v0
li $s0, 0
la $a0, entries
li $v0, 4
syscall
read_array:
# read element
li $v0, 5
syscall
move $t3, $v0
# place in right address
mul $t1, $s0, $t4
add $t1, $t2, $t1
sw $t3, 0($t2)
addi $s0, $s0, 1
blt $s0, $t0, read_array
li $s0, 0
gnome_sort:
# allocate space on heap for copy
mul $t1, $t0, $t4
li $v0, 9
move $a0, $t1
syscall
move $s1, $v0
mul $s2, $t4, $t0
add $s3, $s1, $s2
copy_array:
lw $t5, 0($t2)
sw $t5, 0($s1)
add $t2, $t2, $t4
add $s1, $s1, $t4
blt $s1, $s3, copy_array
li $s0, 0
while_loop:
bgt $s0, $t0, finish_sort
beq $s0, $zero, increase_i
sw $s4, 0($s1)
sw $s5, -4($s1)
bge $s4, $s5, increase_i
j swap_elements
increase_i:
addi $s0, $s0, 1
j while_loop
swap_elements:
la $a0, input
li $v0, 4
syscall
sw $t6, 0($s1)
sw $t7, -4($s1)
lw $t7, 0($s1)
lw $t6, -4($s1)
subi $s0, $s0, 1
j while_loop
finish_sort:
li $s0, 0
la $a0, output
li $v0, 4
syscall
j print_original
print_original:
bge $s0, $t0, print_sorted
lw $s6, 0($t2)
li $v0, 1
move $a0, $s6
syscall
la $a0, space
li $v0, 4
syscall
addi $s0, $s0, 1
j print_original
print_sorted:
li $s0, 0
loop:
bge $s0, $t0, finish
lw $s6, 0($s1)
li $v0, 1
move $a0, $s6
syscall
la $a0, space
li $v0, 4
syscall
addi $s0, $s0, 1
j loop
finish:
li $v0, 10
syscall
After testing this in QTSpim, it seems that you're going out of allocated memory on the line sw $s4, 0($s1)
in the while_loop. This is because $s1 at that point is past the end of the second array you allocated. If you want to use that memory, you will have to syscall for it.
The way you copy the array using lw and sw is correct.
here is the code in C
Void mm ( double a[][]), double b[][], double c[][])
{
int i, j, k;
for (i=0; i! = 3; i = i+1){
for (j = 0; j! = 3; j = j+1){
d[i][j] = a[i][j] ;
for (k = 0; k! = 3; k = k+1){
d[i][j] = d[i][j] + b[i][k] * c[k][j];
}
}
}
}
also keep in mind that I have to read the data of arrays a,b,c and then print array d
and here is my assembly code
.data
arrA: .space 72
arrB: .space 72
arrC: .space 72
arrD: .space 72
bracA: .asciiz "a("
bracB: .asciiz "b("
bracC: .asciiz "c("
bracD: .asciiz "d("
comma: .asciiz ","
endBrac: .asciiz ")= "
nextLine: .asciiz "\n"
.text
.globl main
main:
addi $sp, $sp, -32 #Make space of stack
sw $ra, 20($sp) #Store $ra
la $a1, arrA #load base of Array a
la $a2, arrB #load base of Array b
la $a3, arrC #load base of Array c
la $t7, arrD #load base of Array d
li $s0, 1 #set i
li $s1, 1 #set j
li $s2, 1 #set k
li $s7, 1 #counter
loopi:
slti $t0, $s0, 4 #Check i<4
beq $t0, $zero, nextArr #If i=4 go nextArr
loopj:
slti $t0, $s1, 4 #Check j<4
beq $t0, $zero, nextRow #If i=4 go nextRow
slti $t1, $s7, 4 #Check counter
beq $t1, $zero, contB #if counter >=4 go to contB
li $v0, 4 #Prepare for print string
la $a0, bracA #Load adress of string "a("
syscall
j cont
contB:
slti $t1, $s7, 7
beq $t1, $zero, contC #if counter>=7 go to contC
li $v0, 4
la $a0, bracB
syscall
j cont
contC:
slti $t1, $s7, 10
beq $t1, $zero, calcD
li $v0, 4
la $a0, bracC
syscall
cont:
li $v0, 1 #prepare to print integer
move $a0, $s0 #print i
syscall
li $v0, 4
la $a0, comma
syscall
li $v0, 1
move $a0, $s1 #print j
syscall
li $v0, 4
la $a0, endBrac
syscall
sll $t0, $s0, 1 #t0=2*i
add $t0, $t0, $s0 #t0=3*i
add $t0, $t0, $s1 #t0=3*i +j
sll $t0, $t0, 3 #t0=(3*i +j)*8 create double precision
slti $t1, $s7, 4
beq $t1, $zero, useA2
add $t0, $t0, $a1 #a1=base address of a array
j contLi
useA2:
slti $t1, $s7, 7
beq $t1, $zero, useA3
add $t0, $t0, $a2 #a2=base address of a array
j contLi
useA3:
add $t0, $t0, $a3 #a3=base address of a array
contLi:
li $v0, 7
syscall
s.d $f0, 0($t0) #store $t0 to $f0 as floating point
l.d $f12, 0($t0)
li $v0, 3 #get ready to print double
syscall
addi $s1, $s1, 1 #increase j
li $v0, 4
la $a0, nextLine
syscall
j loopj
nextRow:
addi $s7, $s7, 1 #increase counter
addi $s0, $s0, 1 #increase i
li $s1, 1 #set j=1
j loopi
nextArr:
li $s0, 1 #set i=1
li $s1, 1 #set j=1
j loopi
calcD:
li $s0, 1 #set i,j,k=1 for calculations
li $s1, 1
li $s2, 1
li $t0, 0
li $t1, 0
li $t2, 0
li $t3, 0
outLoop:
slti $t0, $s0, 4 #Loop that count i
beq $t0, $zero, printRes
inLoop1:
slti $t0, $s1, 4 #loop that count j
beq $t0, $zero, finInLoop1
sll $t0, $s0, 1 #t0=2*i
add $t0, $t0, $s0 #t0=3*i
add $t0, $t0, $s1 #t0=3*i +j
sll $t0, $t0, 3 #t0=(3*i +j)*8 (double precision)
add $t0, $t0, $a1 #a1=base address of a array
sll $t1, $s0, 1 #t3=2*i
add $t1, $t1, $s0 #t3=3*i
add $t1, $t1, $s1 #t3=3*i +j
sll $t1, $t1, 3 #t3=(3*i +j)*8 for double precision
add $t1, $t1, $t7 #t4=base address of d array
mtc1 $zero,$f0 #f8=sum to be stored in d[i][j](initial value before k loops=0)
cvt.d.w $f0, $f0
mov.d $f8, $f0
l.d $f2, 0($t0) #Load $t0 to $f2
inLoop2:
slti $t4, $s2, 4 #Loop that count k
beq $t4, $zero, finInLoop2
sll $t2, $s0, 1 #t1=2*i
add $t2, $t2, $s0 #t1=3*i
add $t2, $t2, $s2 #t1=3*i +k
sll $t2, $t2, 3 #t1=(3*i +k)*8 (double precision)
add $t2, $t2, $a2 #a2=base address of b array
sll $t3, $s2, 1 #t2=2*k
add $t3, $t3, $s2 #t2=3*k
add $t3, $t3, $s1 #t2=3*k +j
sll $t3, $t3, 3 #t2=(3*k +j)*8 for double precision
add $t3, $t3, $a3 #a3=base address of c array
#So far, t0=address of a[i][j], t1=address of b[i][k], t2=address of c[k][j] and t3=address of d[i][j]
l.d $f4, 0($t2) #Load $t2 to $f4
l.d $f6, 0($t3) #Load $t3 to $f6
mul.d $f10, $f4, $f6
add.d $f8, $f8, $f10
addi $s2, $s2, 1
j inLoop2
finInLoop2:
add.d $f8, $f8, $f2
s.d $f8, 0($t1)#store total sum in d[i][j]
addi $s1, $s1, 1
li $s2, 1 #k=1 to start loop for next j
add.d $f8, $f8, $f0
j inLoop1
finInLoop1:
addi $s0, $s0, 1
li $s1, 1# j=1 to start loop for next i
j outLoop
printRes:
li $s0, 1 #initial value of i=1
li $s1, 1 #initial value of j=1
li $s2, 1 #initial value of k=1
printLoop1:
slti $t0, $s0, 4
beq $t0, $zero, Exit
slti $t0, $s1, 4
beq $t0, $zero, printLoop2
li $v0, 4
la $a0, nextLine
syscall
li $v0, 4
la $a0, bracD
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 4
la $a0, comma
syscall
li $v0, 1
move $a0, $s1
syscall
li $v0, 4
la $a0, endBrac
syscall
sll $t0, $s0, 1 #t0=2*i
add $t0, $t0 $s0 #t0=3*i
add $t0, $t0, $s1 #t0=3*i +j
sll $t0, $t0, 3 #t0=(3*i +j)*8 (double precision)
add $t0, $t0, $t7 #t4=base address of d array
l.d $f12, 0($t0)
li $v0, 3 #get ready to print double
syscall
addi $s1, $s1, 1
j printLoop1
printLoop2:
addi $s0, $s0, 1
li $s1, 1
j printLoop1
Exit:
jr $ra
when I read the data
the program prints instantly what I have written
and it doesn't print in the end the d array
I'm a beginner in mips assembly so there might be some major problems with my code
but I can't find them...
I'm trying to add values to an array inside a subroutine. I can get the first valid value(positive number and divisible by 3) into the array, but the next valid entries don't work. I can enter a valid number and then enter an invalid number and the program works fine, but two valid numbers makes Spim stop working. I've spent a few hours trying to figure it out but no luck. The jumping from one subroutine is a requirement for the assignment, I have a working program but lacks all the unnecessary(in my opinion) subroutines.
.data
array1: .word 80
EnterARVal: .asciiz "Please enter a number:\t"
space: .asciiz " "
errormessage: .asciiz "*****Error: "
notpos: .asciiz " is not a positive number.\n"
notdiv3: .asciiz " is not divisible by 3.\n"
numadded: .asciiz " added to array.\n"
EnterElem: .asciiz "Enter number "
ARReverse: .asciiz "The contents of the array in reverse orders is:\n"
InvalidAR: .asciiz "Invalid number of array elements, please try again.\n"
.text
main:
la $s0, array1 #array1 pointer
li $t0, 1
begin:
jal readNum #go to readNum subroutine
add $a0, $0, $v0 #stores readNum input to $a0
jal verifySize #jumps to verifySize subroutine
add $t1, $v1, $0 #stores 0 or 1 value to $t1
beq $t1, $0, begin #starts over if t1 is 0 or false
beq $t1, $t0, numok #goes to numok if t1 is 1 or true
numok: add $a0, $0, $a0
add $a1, $0, $s0
jal createArray
j exit
readNum: li $v0, 4
la $a0, EnterARVal
syscall
li $v0, 5
syscall
add $v0, $v0, $0
j $ra
verifySize: add $t1, $0, $a0
li $t2, 20
li $t3, 1
li $t4, 0
li $t5, 1
slt $t6, $t1, $t3
beq $t6, $t3, toolow
sgt $t7, $t1, $t2
beq $t7, $t3, toohigh
beq $t7, $t4, oknum
oknum:
add $v1, $t5, $0
j $ra
toolow:
li $v0, 4
la $a0, InvalidAR
syscall
add $v1, $t4, $0
j $ra
toohigh:
li $v0, 4
la $a0, InvalidAR
syscall
add $v1, $t4, $0
j $ra
createArray: add $s1, $a0, $0
add $s0, $a1, $0
li $t0, 0 #counter
li $t2, 1
add $a0, $s1, $0
li $v0, 1
syscall
makingarray: beq $t0, $s1, arraydone
jal readNum #go to readNum subroutine
add $a0, $v0, $0 #stores number from readNum to $a0
jal checkNumPositive #jump to checkNumPositive subroutine
add $t1, $v0, $0
beq $t1, $0, positivenum #if number is positive go to positivenum
beq $t1, $t2, notpositive
positivenum:
jal divisibleBy3
add $t4, $v0, $0
beq $t4, $0, notdivisibleby3
sw $a0, 0($s0)
li $v0, 1
syscall
li $v0, 4
la $a0, numadded
syscall
add $s0, $s0, 4
add $t0, $t0, 1
j makingarray
arraydone:
add $v0, $s0, $0
j $ra
notpositive:
li $v0, 4
la $a0, notpos
syscall
j makingarray
notdivisibleby3:
li $v0, 4
la $a0, notdiv3
syscall
j makingarray
#reverseArray:
divisibleBy3: add $t0, $a0, $0
li $t1, 3
div $t0, $t1
mfhi $t2
mflo $t3
seq $t4, $t2, $0
add $v0, $t4, $0
j $ra
checkNumPositive: li $t0, 0
slt $t1, $a0, $0 #set t1 to 1 if number is less than 0
add $v0, $t1, $t0
j $ra
exit: li $v0, 10
syscall
Any tips with how I can fix createArray is appreciated, thanks.
Your primary problem is you used .word 80 which only reserves a single word with value 80. You probably meant .space 80 to reserve space for up to 20 words (which seems to be the limit enforced in your code).
Further problem is you are not following conventions about which registers need to be preserved.
For example, you use $t0 as counter in createArray and that's not preserved across subroutines, not by convention and de facto not by your code (both divisibleBy3 and checkNumPositive destroy it).
Similar problem with not properly preserving $ra in nested subroutine calls, as such the return address for createArray is overwritten by the subroutines invoked from there.
I assume the intent of the assignment was to teach you about these nuances.