How could a program that allows the sum of the elements of a vector (A [] = {1,2,3,4,5,6,7}) of 7 elements which is in memory. And the result is stored in a variable and stored in memory.
How could a program that allows the sum of the elements of a vector (A [] = {1,2,3,4,5,6,7}) of 7 elements which is in memory. And the result is stored in a variable and stored in memory.
The first thing you will have to do is take the label. The label is obtained with the instruction $ x, tag, so that:
la $t1 , A
Dejará en A la dirección de memoria.
Sabemos que hay 7 elementos.
li $t2, 7
Deja en $t2 el número de iteraciones del bucle que irá sumando.
li $t3,0
Éste será el contenido del contador de iteraciones.
li $t4, 0
Éste será el acumulado.
Creamos una etiqueta para volver a sumar.
for:
beq $t3,$t2,fin_for #Si no se cumple que $t3 es = 7 sigue el bucle.
lw $t5,0($t1) # Cargamos en $t5 el contenido en la posicion 0 de la posición de memoria en $t1.
add $t4,$t4,$t5 #Acumulamos.
addi $t1,$t1,4 #No sé si se escribe así pero vamos sumamos 4 a $t1 para ir a la siguiente posición del array, aquí es importante decir que si tienes un array por ejemplo de caracteres será un solo byte lo que tengas que avanzar y no cuatro. Los enteros ocupan 4 bytes en mips.
addi $t3,$t3,1 #Avanzamos el contador
j for #Saltamos a la etiqueta de antes si o si, arriba se hará la comprobación de salida.
fin_for:
la $t1,varResultado
sw $t4,0($t1) # Escribimos en la posicion de memoria de la variable resultado el acumulado.
I hope it works for you, there are details that are not talked about, you must have a statement of a word in the data for the result etc ... but let's go the idea is this, cheer up the program and enjoy assembler!