Writing a MIPS assembly program that sums two integer arrays

Page 1 of 1

2 Replies - 21963 Views - Last Post: 08 November 2012 - 06:51 AM

#1 832954

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 30-October 12

Writing a MIPS assembly program that sums two integer arrays

Posted 31 October 2012 - 07:19 PM

I am trying write a MIPS program named that sums two arrays of 4-byte integers. Each array is supposed to have the same number of elements, with a maximum of 10 elements. The program must have the following parts:

1. Setup. Use assembler directives to allocate spaces in memory to hold three integer arrays A, B, and C. Each has a maximum of 10 4-byte integers.

2. Input. Prompt the user for the size of the arrays (assuming that the user inputs a size no more than 10). And then prompt the user the input arrays A and B. Use the subroutine mentioned below to let the user input the integers for each array.

3. Calculation. Calculate the sum C = A + B (i.e., vector sum) by calling the subroutine mentioned below. Store the results in the space you allocated in memory for the array C.

4. Output. Use the subroutine mentioned below to print out the array C.

5. Required subroutines:
a. A subroutine which accepts two parameters to let the user input the integers for an array. The first parameter should be the base address of the array and the second parameter should be the size of the array.
b. A subroutine which accepts four parameters to let the user input the two integer arrays and calculate the sum of the two arrays (i.e., vector sum) and store the sum in the third integer array. The first and second parameters should be the base addresses of the two arrays to be added; the third parameter should be the base address of the sum array; and the fourth parameter should be the size of the array.
c. A subroutine which accepts two parameters to let the user output the integers in an array. The first parameter should be the base address of the array and the second parameter should be the size of the array.

Here is what I worked on so far:
```#data segment

.data

numbers:.word 2,1,5,3,10

count: .word 5

ans1: .asciiz "sum = "

endl: .asciiz "\n"

#text segment

.text

main:

la \$a0, numbers         # load array address

lw \$a1, count           # load number of elements

jal sum                 # call sum function

move \$a0, \$v0           # call print

jal print

j finish                # finish

sum:

bge \$t2, \$a1, done      #if t2>=\$a1, goto done

lw  \$t0, 0(\$a0)         #load value at addr a0 to t0

add \$t1, \$t1, \$t0       # sum = sum + array[i]

addi \$a0, \$a0, 4        # add addr by 4 to get the address of   next value in the array

addi \$t2, \$t2, 1        # i = i + 1

j sum

done:

move \$v0, \$t1           # move result to v0

jr   \$ra                #go to return addr

print:

# code to print ansl, sum and end

# deleted for brevity

jr \$ra                  # return

finish:

li \$v0, 10    # exit

syscall

```

The output is supposed to look something like this:

Input the size of array (up to 10): 4
Input array A.
Element [0] of the array: 2
Element [1] of the array: -4
Element [2] of the array: -12
Element [3] of the array: 5
Input array B.
Element [0] of the array: 1
Element [1] of the array: 7
Element [2] of the array: 17
Element [3] of the array: -23
The sum of A and B is array C with:
Element [0] of the array: 3
Element [1] of the array: 3
Element [2] of the array: 5
Element [3] of the array: -18

This post has been edited by macosxnerd101: 31 October 2012 - 07:24 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

Replies To: Writing a MIPS assembly program that sums two integer arrays

#2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11759
• Posts: 44,191
• Joined: 27-December 08

Re: Writing a MIPS assembly program that sums two integer arrays

Posted 31 October 2012 - 07:25 PM

Moved to Assembly.
Was This Post Helpful? 0

#3 turboscrew

• D.I.C Addict

Reputation: 133
• Posts: 891
• Joined: 03-April 12

Re: Writing a MIPS assembly program that sums two integer arrays

Posted 08 November 2012 - 06:51 AM

What's the value of t2 when you first enter the subroutine "sum"?
How about t1?

I suppose this:
numbers:.word 2,1,5,3,10
is "A".

Where are B and C?

Also should the vectors be 10 upto 10 numbers, not 5?
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}