0

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1757759
  • 41
  • 2
  • 5

2 Answers2

2

There are several errors in your code. Regarding the storing of items in the array, you have to add the base address of the array with the index (multiplied by 4) to get the address of the item to be stored.

Assuming that $s0 holds a and $s2 holds b, to store D[a] = a + b you would:

la $t1, array
sll $t0, $s0, 2
addu $t2, $t1, $t0  # $t2 is &D[a]
add $s1, $s0, $s2    # $s1 = a + b
sw $s1, 0($t2)  # D[A] = a + b

Note that you didn't reserve enough memory to hold 10 items in array D, assuming an int is 32 bits-wide, then each item is 4 bytes long, therefore you should reserve 40 bytes...

You should also ensure that the array is properly aligned at a word boundary. To do that, you can instruct the assembler to do the alignment for you with the .align 2 directive, e.g:

.align 2
array: .space 40
gusbro
  • 22,357
  • 35
  • 46
  • Thx, but I thought "addiu" command was for: register, register, integer? I get this error wen I use "addiu": "$t0": operand is of incorrect type. Also, when I subsititue "add" or "addu" command for "addiu" I get the following error: 'Runtime exception at 0x00400024: store address not aligned on word boundary 0x1001000a' – user1757759 Oct 22 '12 at 22:00
  • @user1757759: Yes, it should be `addu` (add unsigned). Regarding the error you are receiving, I added some info about it. You have to align the array using `.align` directive – gusbro Oct 22 '12 at 22:42
  • One more question, how can I print the values in the array to check like I did in my c++ preogram? `li $v0, 1 la $a0, array syscall ` – user1757759 Oct 23 '12 at 04:03
  • @user1757759: You will have to iterate over the array reading each integer to print them. Suppose `$t0` points to the address of the current item, you would do `li $v0, 1 lw $a0, $t0(0) syscall` and then `addiu $t0, $t0, 4` to move the pointer `$t0` to the next element – gusbro Oct 23 '12 at 14:24
  • You are executing ’ lw $a0, 0($t0)’ which is wrong because $t0 contains the value 4 – gusbro Nov 03 '12 at 02:55
0
intN;
cout<<"Enter the array size: ";
cin>>N; //size must be less than MAX_SIZE

int one[MAX_SIZE]; 
int two[MAX_SIZE]; 
cout<<"Ente the elements of array one:"<<endl;
for(inti = 0; i<N;i++)
    cin>>one[i];

cout<<"Ente the elements of array two:"<<endl;
for(inti = 0; i<N;i++)
    cin>>two[i];

int result = compare(one,two,N);

if(result == 0)
    cout<<"Array one is Equal to array two"<<endl;
elseif(result==-1)
    cout<<"Array one is less than array two"<<endl;
elseif(result==1)
    cout<<"Array one is greater than array two"<<endl;
  • 1
    Hi @user4749717, welcome to StackOverflow. The OP wanted their "*C++ program to be converted to mips assembly language*", but you have answered with some C++ code, which is not what was sought. Please reread the question and update your answer accordingly if you have an answer. – Wai Ha Lee Apr 04 '15 at 17:00