MIPS Assembly - sw, add, sub, etc

2.4k views Asked by At

I'm attempting to calculate the span of 10 numbers, but I'm stuck before I can begin. I am able to load the set into memory using my LW commands, but after than NOTHING is working. When I do a dump after execution NOTHING works. The 10 values are there but that is it. How am I supposed to add, subtract, or execute any command for that matter? Nothing is being stored, period.

# This program computes and prints the span of a set of ten integers Set.

.data
Set:  .word 9, 17, -7, 3, -27, 25, 42, 26, 8, -60
Span: .alloc 1

.text
        lw $1, Set($0)
        lw $2, Set($1)
        lw $3, Set($2)
        lw $4, Set($3)
        lw $5, Set($4)
        lw $6, Set($5)
        lw $7, Set($6)
        lw $8, Set($7)
        lw $9, Set($8)
        lw $10, Set($9)
        add $11,$2,$3       
        sw $11, Span($0)
        jr $31     

My add command does nothing in this example, nor does the stor command. What am I doing wrong? Do I have to preallocated every single spot I want to use in memory? ($11) for example. I've tried numerous commands and none do anything beyond my final lw command.

1

There are 1 answers

0
Konrad Lindenbach On

Here's what I wrote in an attempt to create equivalent code to what you're trying to do:

.data
Set:  .word 9, 17, -7, 3, -27, 25, 42, 26, 8, -60
Span: .space 4 #allocate 4 bytes

.set noat #needed to use $1
.text
main:
    la $12  Set    #load the address of set
    lw $1,  0($12) #load first number into $1
    lw $2,  4($12) #load second number into $2
    lw $3,  8($12) #etc
    lw $4,  12($12)
    lw $5,  16($12)
    lw $6,  20($12)
    lw $7,  24($12)
    lw $8,  28($12)
    lw $9,  32($12)
    lw $10, 36($12)

    add $11,$2,$3 #add second and third number, store in $11       

    la $13  Span   #load the address of Span
    sw $11, 0($13) #store the sum in Span
    jr $31

You didn't really say exactly what was going wrong (program crashes? refuses to assemble?) so I'm not sure what advice to give, but your use of registers is suspicious -- especially $1 i.e. $at which my assembler won't even let me use without turning of the warning; this register is reserved for use by the assembler.