Taylor Bigham
Taylor Bigham

Reputation: 117

MIPS Explanation: sw

The Background

I am currently working on small MIPS program for a homework assignment, and in the process learning some of the language. I am extremely new to this, and as such I am very unsure of myself when it comes to even the most basic aspects of the operations that I'm performing. Furthermore, my instructor insists on not using pseudocode in our assignments, which is leading to a lot of difficulty in understanding how to accomplish certain tasks.

The Assignment

My assignment question is this: Suppose you want to compute iteratively (in a loop) the first 20 Biggie numbers, B(i) = 2i + 17i and store them sequentially in an array B whose base address in MIPS memory is stored in register $s0. Please write the MIPS code (fully commented) to compute B(i), 1 < i < 20.

My Solution

What I currently have:

.globl main
main:

.data
BiggieArray:
    .space 80                   #4 bytes per word * 20 biggie numbers = 80 bytes reserved
.text
addi    $s1, $zero, 1           #$s1 tracks i, initialize i with value of 1
addi    $s2, $zero, 21      #$s2 holds 21, for use in comparing with the value of i
addi    $s3, $zero, 2           #$s3 holds the value for the first mult
addi    $s4, $zero, 17      #$s4 holds the value for the second mult

STARTLOOP:
beq     $s1, $s2, ENDLOOP       #compares i ($s1) with $s2.  If they are equal, i > 20, so branch to the end of the loop.

add     $t0, $s1, $s1        #find 2i
add     $t0, $t0, $t0        #find 4i, use as offset for BiggieArray
addi    $t0, $t0, -4            #adjusts to start from index 0 instead of index 1

mult    $s1, $s3                #Calculates 2i
mflo    $s5                #$s5 holds 2i
mult    $s1, $s4                #Calculates 17i
mflo    $s6                #$s6 holds 17i

add     $s7, $s5, $s6        #$s7 holds 2i+17i

add     $t1, $s0, $t0        #$t1 holds the current address of BiggieArray[i]

sw      $t1, 0($s7)         #stores the value 2i+17i into RAM ?????

addi    $s1, $s1, 1         #increment i

j   STARTLOOP

ENDLOOP:

My Question

I realize that I don't currently have $s0 initialized to anything, but that's not what's giving me issues. What I'm confused about is how I would store that value of 2i+17i back into BiggieArray. Any help or very simple explanation of how sw works would be greatly appreciated.

Upvotes: 3

Views: 714

Answers (1)

Jim Buck
Jim Buck

Reputation: 20726

In your example, you have the registers reversed. It should be:

sw $s7, 0($t1) # since s7 holds the value you want to store, and t1 holds the mem address where you want to store it

Upvotes: 2

Related Questions