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
Related
Write a MIPS program that defines a one-dimensional array of 10 integers in the static area of the data segment, asks the user to input all 10 array elements, computes, and displays their sum.
.data
arr: .word 1,2,3,4,5,6,7,8,9,10
msg: .asciiz "Result: "
.text
.globl main
main:
addi $t0, $zero, 0 # clear i
addi $t1, $zero, 0 # clear sum
ori $t2, $zero, 10 # Initializing t2 to its constant value 10
la $t3, arr # load address of array into t4
loop:
slt $t4, $t0, $t2 # compare, $t4 = i < sum ? 1 : 0
beq $t4, $zero, end # if i is not < 10, exit the loop
lw $t4, 0($t3) # load current array element into t4
add $t1, $t1, $t4 # add it to sum
add $t0, $t0, 1 # increment i
add $t3, $t3, 4 # increment current array element pointer
j loop
end:
addi $v0, $zero, 4 # Now we print out result: string
la $a0, msg
syscall
addi $v0, $zero, 1 # followed by the actual sum (which is in t1)
add $a0, $t1, $zero
syscall
jr $ra
.end main
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 have to make a program that fills an array with 30 integers entered from keyboard.Then the user type 'c' to copy the array to an other array.i've done with the first step but i cant manage to copy the array to another.
Here is my code
.data
msg1: .asciiz "> "
msg2: .asciiz "type 'c' to copy \n>"
.align 2
array: .space 400
.text
main:
la $t3 array
loop:
la $a0, msg1 #output message 1
li $v0, 4
syscall
li $v0, 5 #read integer input
syscall
move $t0, $v0
beq $t0, -99, endloop #loop until user types -99
beq $t1,30,endloop #get user input up to 30 times
addi $t1, $t1, 1 #counter
sw $t0,($t3)
addi $t3,$t3,4
b loop #loop until it reaches 30
endloop:
la $a0, msg2 #output message 2
li $v0, 4
syscall
li $v0, 12 #read character input
syscall
beq $v0, 'c', COPY
j NEXT
COPY:
NEXT:
The most primitive way to do it is to
la $t1, dst_array
la $t3, src_array
addu $t0, $t3, 30*4 # setup a 'ceiling'
copy_loop:
lw $at, 0($t3)
sw $at, 0($t1)
addu $t1, $t1, 4
addu $t3, $t3, 4
blt $t1, $t0, copy_loop # if load pointer < src_array + 30*4
However, some implementations of MIPS don't use forwarding, and therefore you have to wait until $at is written back. For that purpose, there may be either a stall (which you could get rid off)
subu $t1, $t1, 4
copy_loop:
lw $at, 0($t3)
addu $t1, $t1, 4
addu $t3, $t3, 4
sw $at, 0($t1)
or a load delay slot, which usually takes 1 cycle, making it
copy_loop:
lw $at, 0($t3)
addu $t1, $t1, 4
sw $at, 0($t1)
addu $t3, $t3, 4
Generally speaking, it depends :)
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.
I try to add numbers to a array and then I try to print those numbers, but when I add mine numbers and I try to print and then I got different numbers. How is that possible?
My code is:
#----------------------------------- Array Vullen -----------------------------------------------------
.data
question1_msg: .asciiz "How much integer do you want to give?\n"
question2_msg: .asciiz "give a number?\n"
.text
question_numbers:
la $a0, question1_msg #load the question in $a0
li $v0, 4
syscall
answer_numbers:
li $v0, 5 #read the answer of previous question
syscall
move $t0, $v0
move $t9, $t0
move $t8, $t0
generate_array:
sll $t0, $t0, 2 #create array
move $a0, $t0
li $v0, 9
syscall
move $t3, $v0 #put the stack pointer in a temperay register
move $t4, $v0
add_numbers_array:
bge $t1, $t9, Call_procedure # if $t1 >= $t0 then exit
#ask questions
la $a0, question2_msg #load the question in $a0
li $v0, 4
syscall
#read numbers
li $v0, 5
syscall
move $t2, $a0
#add number en go to the next array point
sw $t2, ($t3)
add $t3, $t3, 4
add $t1, $t1, 1
#get back to the begin of the loop
b add_numbers_array
#-------------------------------------Array Printen------------------------------------------------
Call_procedure:
li $t1, 0
la $a1, ($t8) # load the couple of numbers
la $a2, ($t4) # load the starting adress of the array
jal Print
b exit
Print:
bge $t1, $a1, return # if $t1 >= $t0 then exit
lw $t2, ($a2) #load integer and print
move $a0, $t2
li $v0, 1 # print the number in the array
syscall
addu $a2, $a2, 4 #increase the sp
addi $t1, $t1, 1 #increase the number printed
b Print
return:
jr $ra
exit:
li $v0 , 10 # let the code end
syscall
I see 2 errors:
1.
#read numbers
li $v0, 5
syscall
move $t2, $v0
This should be $v0, not $a0
2.
move $a1, $t8
move $a2, $t4
instead of
la $a1, ($t8) # load the couple of numbers
la $a2, ($t4) # load the starting adress of the array