3

This is only my second time dealing with MIPS assembly (i.e. or any kind of assembly), so please be gentle. So I made a multiply function for MIPS from scratch. That was easier than I thought -- I tested it and it works perfectly for one value. Unfortunately, I am COMPLETELY lost when arrays come into the picture.

I do not even know how to get started. I feel retarded because I can't ask a specific question because I don't understand the big idea. I allocated space, have constant values for the arrays, but don't really know how to:

A.) Load the constant values (e.g. 5,2,3,10,7) into the arrays.

B.) Get my outer loop going.

My code is below and all I need is a way to get my outer loop going. Any ideas??

/*
Name: MrPickl3
Date: October 10, 2013
Purpose:  Program creates a multiply function from scratch.  Uses two arrays to
      test the program.
*/

#include <xc.h>

. data

X: .space 80
Y: .space 80
N: .space 4
MAC_ACC .word 0x00000000

    .text
    .globl main

main:
    li t0, 0x00000000 //i = 0
    li t1, 0x00000005 //Offset of array
    li t2, MAC_ACC //Mac_acc (i.e. product register)
    lw t9, 0(t2) //Refers to MAC_ACC's data
    la t3, X //Address of X[0]
    lw t4, 0(t3) //Data of X
    la t5, Y //Address of Y[0]
    lw t6, 0(t5) //Data of Y

loop:
    addiu t0, t0, 4 //i++

//t4 = x[i]
//t6 = y[i]
//t7 = counter

mult:
    beq  t6, 0, loop  //Check if y = 0.  Go to loop, if so.
    andi t7, t6, 1    /*We want to know the nearest power of two.
                      We can mask the last bit to
                      test whether or not there is a power of two
                      left in the multiplier.*/
    beq  t7, 0, shift //If last bit is zero, shift
    addu t9, t9, t4   //Add multiplicand to product

shift:
    sll t3, t3, 1 //Multiply x[i] by 2
    srl t4, t4, 1 //Multiply y[i] by 2

lab2_done:
    j lab2_done
    nop

.end main

X_INPUT: .word 5,2,3,10,7
Y_INPUT: .word 6,0,8,1,2
N_INPUT: .word 5
MrPickle5
  • 522
  • 4
  • 9
  • 31
  • What do you mean by "load into the arrays"? The values 5,2,3,10,7 are already in the `X_INPUT` array, because you put them there when declaring the array. Did you mean "loading _from_ the arrays"? – Michael Oct 11 '13 at 07:32

1 Answers1

1

It sounds like you're trying to figure out how to access the i'th element of an array, when the syntax you're seeing is lw $t4, 0($t3). I think you already know that you could get the next word with lw $t4, 4($t3), but you're stuck on how to make that index dynamic.

The trick is that you don't change the immediate value (0, 4, 8 etc). Instead, you change the contents of the register that, in the examples above, points to the first word in the array.

Here's the code I wrote for an assignment in my CompArch class to implement a simple do-while loop that initializes the members of an array to zero. We're told that $s0 has been loaded with the address of the first word in the array.

I get the offset to the element I want, multiply by 4 (shift left twice), and then add that offset to $s0 (the first word). Now, $t1 points to the int I want to set. All I have to do is store the value ($zero) in the address pointed to by $t1.

        .text 
partC:  # Implement a do-while loop (0-100)
        add $t0, $zero, $zero # i=0
Cstart: # Get offset to current int
        sll $t1, $t0, 2  # *4
        add $t1, $s0, $zero
        sw $zero, ($t1)
        add $t0, $t0, 1  # i++
        blt $t0, 100, Cstart # i < 100
Cdone:  add $v0, $zero, 10 # terminate program
        syscall 

Note that the syntax sw $zero, ($t1) is just a pseudo-op for sw $zero, 0($t1)

Hope this helps with the underlying concept!

RobertB
  • 946
  • 1
  • 9
  • 22
  • You don't need to shift and add to redo the indexing inside the loop, just increment a pointer (and `bne` against an end-pointer you calculate once outside the loop). Also, you're doing `t1 = s0 + 0` every time, overwriting the `i<<2` in t1. (You avoided that bug in [MIPS Assembly language traversing an array](https://stackoverflow.com/q/19575260), but those loops have `j` at the bottom instead of `bne`, so neither of these Q&As makes a good duplicate for an efficient idiomatic loop over an array. :/) – Peter Cordes Mar 27 '21 at 13:10