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 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:
.data
p1: .asciiz "\nEnter the data for a sorted array: "
p2: .asciiz "\nEnter a value to search for: "
p3: .asciiz " is not found"
p4: .asciiz " is found at "
array: .space 404
.text
main:
la $t0, array # a1 = addr of first int
#Store Array Values
store:
li $v0, 4 # system call code for print_str
la $a0, p1 # address of string to print
syscall # print the first prompt
li $v0, 5 # system call for read_int
syscall # read int to store
move $t1, $v0 # store int
beq $t1, 0, bsearch # once sentinel val hit, go to search
sub $t9, $t0, 4 # set index to the first bit of the last number
j storeVal # store s0 in array
storeVal:
sw $t1, array($t0) # store value in array
addi $t0, $t0, 4 # inc index
j store # continue to add integers
#Binary Search
bsearch:
li $v0, 4 # system call code for print_str
la $a0, p2 # address of string to print
syscall # print the second prompt
li $v0, 5 # system call for read_int
syscall # read int to find
move $a0, $v0 # store int to find
beq $a0, 0, end # once sentinel val hit, go to end prog
la $a2, array # a1 = addr of first int
la $a2, ($t9) # a2 = addr of last int
subu $sp, $sp, 4 # 4 bytes 4 stack
sw $ra, 4($sp) # save return addr
subu $t0, $t9, $s0 # size of array
bnez $t0, searchVal # if not 0, search
move $v0, $a1 # addr of only entry
lw $t0, ($v0) # load entry
beq $a0, $t0, found # if = to int, print found
j notFound # not found
searchVal:
sra $t0, $t0, 3 # comp offset of middle m:
sll $t0, $t0, 2 # t0 = 4(t1/8)
addu $v0, $a1, $t0 # middle m
lw $t0, ($v0) # t0 = m
beq $a0, $t0, found # if = to int, print found
blt $a0, $t0, left # search left
j right # search right
right:
addu $a1, $v0, 4 # search right
jal bsearch
beq $a0, $t0, found # if = to int, print found
j notFound # not found
left:
move $a2, $v0 # search left of m
jal bsearch
beq $a0, $t0, found # if = to int, print found
j notFound # not found
#Print
found:
li $v0, 1 # system call code for print_int
move $a0, $s2 # integer to print (counter
syscall # print it
li $v0, 4 # system call code for print_str
la $a0, p4 # address of string to print
syscall # print the answer part 2
j bsearch # continue to search for more integers
notFound:
li $v0, 1 # system call code for print_int
move $a0, $s2 # integer to print (counter)
syscall # print it
li $v0, 4 # system call code for print_str
la $a0, p3 # address of string to print
syscall # print the answer part 2
j bsearch # continue to search for more integers
#END PROG
end:
li $v0, 10
syscall
I keep getting an error when I attempt to load words into my array, "Error in /../Proj.asm line 58: Runtime exception at 0x00400040: store address not aligned on word boundary 0x200200c2.
57) storeVal:
58) sw $t1, array($t0) # store value in array
59) addi $t0, $t0, 4 # inc index
60) j store # continue to add integers
Line 58 being the sw $t1, array($t0) one. I have tried setting t0 (the line right after .main) the way shown, and setting it to $zero with
addi $t1, $zero, 0
neither of the options will let me set an item into the array, and I cannot find a guide or tutorial to save my life... Also while we're at it, can anyone see if the rest of the code is at least close? Thanks!
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
I am having an issue with the following code I am using for an assignment.. Note, I do not want anybody just giving me code, I am really trying to understand MIPS. I am using the QTSpim simulator to run my MIPS code.
The following code is supposed to allow the user to enter 10 integers from the keyboard, then take those integers and sum the ones that are less than the first inputted integer (ie. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 would sum all numbers except 10, to equal 45). Then, the program should output the array in the order it was given. Currently, my code is allowing the user to enter 10 integers, but then funny things happen. It sums the numbers in a way that I cannot follow (the most common sums somehow being 0, 4, and 50), and will then only print out 4 integers for the array list (which seems to be as follows: firstNumber, secondNumber, lastNumber, 10) I am really confused as to why this happens. Also, for some instances of integers, it will create an infinite loop outputting 0.
I've been at this for hours, can somebody please give me some advice or pointers?
All help is appreciated. Thanks!
# DATA DECLARATION
.data
request: .asciiz "Enter an integer:\n"
sumLine: .asciiz "The sum is: "
aList: .asciiz "The array contains the following: \n"
return: .asciiz "\n"
array: .word 0
aLength: .word 10
input: .word 0
count: .word 0
count2: .word 0
count3: .word 0
sum: .word 0
next: .word 0
first: .word 0
one: .word 1
# PROGRAM CODE
.text
.globl main
# PROGRAM EXECUTION
#--------------------------------------------------------------------------
# Procedure main
# Description: Initializes registers and prints the final sum.
# parameters: $s0 = address of array, $t0 = length
# return value: $v0 = sum
# registers to be used: $s0, $t0, $t1, $t2, $t4, $t5, $v0
#--------------------------------------------------------------------------
main:
la $s0, array # s0 = array
lw $t0, aLength # t0 = aLength
lw $t1, count # t1 = count
lw $t2, input # t2 = input
lw $t3, count2 # t3 = count2
lw $t4, count3 # t4 = count3
lw $t5, sum # t5 = sum
lw $t6, first # t6 = first
lw $t7, next # t7 = next
lw $t9, one # t9 = one
beq $t1, $zero, readArray # if count=0, goto readArray procedure
la $a0, sumLine # load line to print
li $v0, 4 # print sum output line
syscall
lw $a0, sum # load sum to print
li $v0, 1 # print sum
syscall
la $a0, return # load line to print
li $v0, 4 # print return line
syscall
la $a0, aList # load line to print
li $v0, 4 # print the array list line
syscall
j printArray
#--------------------------------------------------------------------------
# Procedure readArray
# Description: Reads integers from the keyboard.
# parameters: $s0 = address of array, $t0 = length
# return value: --------
# registers to be used: $v0, $a0, $t0, $t1, $t2, $s0
#--------------------------------------------------------------------------
readArray:
beq $t1, $t0, sumSmallerThanFirst # if t1=t0 (count = aLength) goto sum procedure
la $a0, request # load line to print
li $v0, 4 # print request line
syscall
li $v0, 5 # read integer from keyboard
syscall
sw $v0, input # store integer to input
lw $t2, input # t2 = input
sw $t2, 0($s0) # array[i] = t2
addi $s0, $s0, 4 # increment array (i++)
addi $t1, $t1, 1 # increment count (count+1)
sw $t1, count # store count to t1
beq $t1, $t9, store # if t1=t9 (count = one) goto store
j readArray
store:
lw $t6, 0($s0) # t6 = array[i]
sw $t6, first # t6 = first
j readArray
#--------------------------------------------------------------------------
# Procedure sumSmallerThanFirst
# Description: Sums the inputted integers if they are < the first integer.
# parameters: $s0 = address of array, $t0 = length
# return value: ----------
# registers to be used: $s0, $t0, $t3, $t5, $t6, $t7, $t8, $0
#--------------------------------------------------------------------------
sumSmallerThanFirst:
la $s0, array
beq $t3, $t0, main # if count=length, goto main
lw $t7, 0($s0) # t7 = array[i]
sw $t7, next # t7 = next
slt $t8, $t7, $t6 # if t7<t6, t8=1
addi $s0, $s0, 4 # array[i++]
addi $t3, $t3, 1 # count+1
sw $t3, count2 # store count2 to t3
beq $t8, $zero, sumSmallerThanFirst # if t8=0, goto top sum
add $t5, $t5, $t7 # t5=t5+t6 (sum = sum + array[i])
sw $t5, sum # store sum to t5
j sumSmallerThanFirst
#--------------------------------------------------------------------------
# Procedure printArray
# Description: Prints out the array of inputted integers.
# parameters: $s0 = address of array, $t0 = length
# return value: -------------
# registers to be used: $v0, $t0, $t4, $t6, $s0
#--------------------------------------------------------------------------
printArray:
beq $t4, $t0, Exit # if count=length, goto Exit
lw $t7, 0($s0) # t7 = array[i]
sw $t7, next # t7 = next
lw $a0, next # load array[i] to print
li $v0, 1 # print array[i]
syscall
la $a0, return # load line to print
li $v0, 4 # print return line
syscall
addi $s0, $s0, 4 # array[i++]
addi $t4, $t4, 1 # count+1
sw $t4, count3 # store count3 to t4
j printArray
Exit:
jr $ra # return
in the same position as you on what i think is the exact same assignment.
regarding the array....
if you defined array as space it'll give you some weird issues... If you define it as word. You won't have such problem. I guess this is because of the difference alignment for space and word in the kernel because I did notice the different operation in the kernel level.
however... regarding the sum.... i keep getting ridiculous answers, mainly either 0, 1, or -1.