C++ program to be converted to mips assembly language
#include <iostream>
using namespace std;
int main() {
int a = 1;
int b = 1;
int array[a];
while ( a < 10)
{
array[a] = b + a;
cout << array[a] << endl; // print elements in array //check values in array
a +=1;
}
system("pause");
return 0;
}
Mips assembly language program
.data
str: .ascii "abcdef"
array: .space 20
.text
main:
li $s0, 1 # a = 1
li $s2, 1 # b = 1
loop:
la $t1, array
slti $t0, $s0, 3 # t0<- 1 if a < 3
beq $t0, $zero, exit
sll $t0, $s0, 2 # t1<- 4*a
add $t1, $t1, $t0 # new base addr
add $t2, $s2, $s0 # t2<- a+b
sw $t1, 0($t2) # D[a]=a+b
addi $s0, $s0, 1 # a = a +1
j loop # goes to loop: label
exit:
li $v0, 10 # v0<- (exit)
syscall
I've tried lw, sw, lb, sb to put the value of register $t2 in the array, but I continue to get an error when I single step run program in Mars compiler
Updated Mips assembly language program
.data
str: .ascii "abcdef"
.align 2
array: .space 40
.text
main:
li $s0, 1 # a = 1
li $s2, 1 # b = 1
loop:
la $t1, array
slti $t0, $s0, 3 # t0<- 1 if a < 3
beq $t0, $zero, exit
sll $t0, $s0, 2 # t1<- 4*a
addu $t2, $t1, $t0 # new base addr
add $s1, $s0, $s2 # s1<- a+b
sw $s1, 0($t2) # D[a]=a+b
li $v0, 1 # load appropriate system call code into register $v0;
# code for printing integer is 1
lw $a0, 0($t0)
addiu $t0, $t0, 4
syscall # call operating system to perform print operation
addi $s0, $s0, 1 # a = a +1
j loop # goes to loop: label
exit:
li $v0, 10 # v0<- (exit)
syscall
I run this program and I get this error: "address out of range 0x00000004" I want to print the values of my array to check if it is right.