Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a novice to MIPS

I'm trying to develop a program which takes an input from a ten word array, increments it by two, stores the result back into a register $v0. Which will hold the incremented value. The program should stop when it encounters a value of 0 from the array.

The first 9 elements of the array will start from 1 up to 9, with the tenth being a 0.

Another register will also be used to hold the sum of all incremented values.

I can also not make use of any pseudo code.

This is the code i have so far

.data
array:
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0
.text
lui $a0, 0x1001 #load address 0x1001000 of the array into register $a0
jal increment #call the procedure
increment:
lw $a2, 0($a0) #load word from register $a0 into $a2
beq $a2, $zero, else #if word loaded is 0 goto else
addi $a2, $a2, 2 #add two
sw $a2, 0($a0) #store result from $a2 back into $a0
addi $a0, $a0, 1 #add 1 to $a0 to point to the next value in the array
add $v0, $a2, $zero #add the result from $a2 into $v0
addi $v0, $zero, 1
syscall
jr $a0
j increment #jump back to the procedure
else:
add $v1, $a0, $zero #once the 0 is found, store the final sum from $a0 into $v1
addi $v1, $zero, 1
syscall

I feel like the logic is correct but the semantics of the code are lacking

The code may seem like a bit of a mess, another area I'm also unsure about is the index, i'm supposed to use another register to hold the 0 based index of the array?

If you can't fix it, then could you give me some pointers? Thanks
Posted
Updated 17-Mar-14 13:53pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900