Print pattern in MIPS - loops

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

Related

Translating from c to mips assembly

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

User ask how many input you want to take and then show their sum in mips

I want to make a program in mips, in which the user will enter the number of inputs he wants to take. Finally the program will print the sum of the inputs.
Here is my code:
.data
myMessage: .asciiz "ENTER numbers you want to sum\n"
value: .asciiz "ENTER Value \n"
sum : .word 0
.text
li $v0, 4
la $a0, myMessage
syscall
li $v0, 5
syscall
move $t0, $v0 #num of time user will enter num
la $t1, 0 #count value first initiallize to 0
see:
bne $t1,$t0,add #checking if count is less than the num of value
li $v0, 1 #printing sum finally
la $a0, ($s2)
add:
li $v0,4
la $a0,value
syscall
li $v0,5
syscall
move $t3,$v0
la $a1, sum #load address of 'bal' in '$a1'
lw $s3, 0($a1) #load sum from '$a1' to '$s2' (initially 0)
add $s3, $s3, $t3 #adding the sum
sw $s2, 0($a1) #load latest sum ('$s2') in .word balance ('$a1')
addi $t1,$t1,1 inc in count
j see
The problem is that the program does not stop after the wished number of inputs and continues to ask for new input.
.data # Data declaration section
instring1: .asciiz "ENTER numbers you want to sum\n"
instring2: .asciiz "Enter value\n"
outstring: .asciiz "Sum: "
.text#
main:
#Print instring1
li $v0, 4 # system call code for printing string = 4
la $a0, instring1 # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
#Taking n as input
li $v0, 5 # read int
syscall
move $s0, $v0 # the result of the syscall is stored in v0
# we move it to t0 to prevent overwriting
#Creation heap memory
mul $t1, $s0, 4
li $v0, 9
move $a0, $t1
syscall
move $s1, $v0
#Print instring2
li $v0, 4 # system call code for printing string = 4
la $a0, instring2 # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
li $s2, 0 # $s2 is the index, and loop induction variable
Start_Input:
bge $s2, $s0, End_Input
li $v0, 5 # Read integer values
syscall
mul $t0, $s2, 4 # $t0 is the offset
add $t1, $s1, $t0 # $t1 is the address of desired index
sw $v0, ($t1) # store the value in the array
addi $s2, $s2, 1 # increment the index
j Start_Input
End_Input:
add $t0, $zero, $zero # i is initialized to 0, $t0 = 0
add $s2, $zero, $zero # sum = 0
beq $s0, $zero, print_sum # if number = 0 then goto print_sum
Loop: #stuff
mul $t1, $t0, 4
add $t2, $s1, $t1
lw $t3, ($t2)
add $s2, $s2, $t3
addi $t0, $t0, 1 # i ++
slt $t1, $t0, $s0 # $t1 = 1 if i < n
bne $t1, $zero, Loop # go to Loop if i < n
#Print Sum
print_sum:
li $v0, 4 # system call code for printing string = 4
la $a0, outstring # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
li $v0 1 # print int
move $a0 $s2
syscall
#Exit Syscall
EXIT:
li $v0 10# exit
syscall
This is working code. Comment provided to understand code.
.data
myMessage:.asciiz "ENTER numbers you want to sum\n"
value:.asciiz "ENTER Value: "
show: .asciiz "\nSum is: "
.text
li $v0,4
la $a0,myMessage
syscall
li $v0,5
syscall
move $t0,$v0 #num of time user will enter num
la $t1, 0 #count value first initiallize to 0
la $t5, 0
see:
bne $t1,$t0,add #checking if count is less than the num of value
li $v0, 4
la $a0, show
syscall #print message "Sum is: "
li $v0, 1
move $a0, $t5
syscall #print the result
j end
add:
li $v0,4
la $a0,value
syscall
li $v0, 5
syscall
add $t5, $t5, $v0
sub $t0, $t0, 1
j see
end:

MIPS - Error when printing data into array

So im attempting to put numbers 1-200 in an array. The array is at the bottom of the code. I keep getting errors when i try to print this, can anyone spot the error with me putting in the numbers? The numbers in the array are supposed to go from 1-200, so $t3 increments each time. Any advice would be greatly appreciated.
main:
.text
lw $t0, numbers_size # loads the size of the array into the loop
la $t1, numbers # $t1 gets the number array
li $t2, 0 # loop control variable
li $t3, 1 # value stored inside numbers
li $v0, 4
la $a0, test_2 # print before loop
syscall
number_for:
bge $t2, $t0, end_for
sw $t3, ($t1)
add $t1, $t1, 4
add $t2, $t2, 1
add $t3, $t3, 1
j number_for
end_for:
lw $t0, numbers_size # loads the size of the array into the loop
la $t1, numbers # $t1 gets the number array
li $t2, 0 # loop control variable
j print_numbers
.data
test: .asciiz "Printing numbers:"
test_2: .asciiz "Before loop:"
numbers:
.word 0:200
numbers_size:
.word 200
This may help you:
.text
main:
lw $t0, numbers_size # loads the size of the array into the loop
la $t1, numbers # $t1 gets the number array
li $t2, 0 # loop control variable
li $t3, 1 # value stored inside numbers
li $v0, 4
la $a0, test_2 # print before loop
syscall
j number_for
li $v0, 10
syscall
number_for:
bge $t2, $t0 end_for
sw $t3, ($t1)
addi $t1, $t1, 4
addi $t2, $t2, 1
addi $t3, $t3, 1
b number_for
end_for:
lw $t0, numbers_size # loads the size of the array into the loop
li $t1, 0
li $t2, 0 # loop control variable
buc:
bge $t2, $t0 end
lw $a0, numbers($t1) # Printing integer from array
li $v0,1
syscall
la $a0, space #load a space: " "
li $v0, 4 #print string
syscall
addi $t1, $t1, 4
addi $t2, $t2, 1
b buc
end:
jr $ra
.data
test: .asciiz "Printing numbers:"
test_2: .asciiz "Before loop:\n"
space: .asciiz " "
numbers_size:
.word 200
numbers:
.word 0:200

MIPS: reading 2d array and printing it

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...

Adding to an array in a subroutine(MIPS/Assembly)

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.

Resources