I'm working on a program which loops through an array of 10 numbers. The first 9 elements have values higher than 0, the 10th has a value of 0. The loop should break when the 0 is encountered.
i=0;
while(A[i]!=0)
{
A[i]=A[i]+1;
i++;
}
I know I can use 'beq' to break the loop if the value of the register is equal to 0. However I don't know enough about manipulating values in the memory.
It's my first time using MIPS and you'll see it's a mess. If you can't fix it for me, can you give me some pointers?
.data #by default, the "data segment" starts at address 0x10010000
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0
.text #instructions start below
# MIPS assembly code
lui $a0, 0x1001 # $a0 = 0x10010000
addi $a1, $zero, 0 # i = 0
jal increment # call the procedure
Here's where I'm most lost:
increment:
lui $a0, 0x1001 # $a0 = 0x10010000
beq $a0, $zero, else # if $a0 holds 0 goto 'else'
addi $a0, $a0, 2 # +2
addi $a1, $zero, 1 # i = i + 1
jr $ra #jump to caller
$v0 should hold the sum of all the incremented values.
else:
add $a0, $v0, $zero #copy result as input to syscall
addi $v0,$zero,1 #service 1 for syscall is print integer
syscall
Finishes with an infinite loop.
infinite: j infinite
To load a value from a memory, you need to call one of the load instructions, (
lw
,lh
orlb
for word, half-word and byte). for example:to write a value in memory, you use one of the store commands, for example:
loading an address into a register is done using la, for example
Now, to manipulate the value in the array, you need to combine the three instructions from above:
And another point:
You wrote
addi $a1, $zero, 1 # i = i + 1
but this is wrong. What you did is to store the result of$zero + 1
which is1
into$a1
. In order to increment$a1
, you need to writeaddi $a1, $a1, 1
which is "store the result of$a1 + 1
into$a1
.