Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:02:34+00:00 2026-06-13T10:02:34+00:00

C++ program to be converted to mips assembly language #include <iostream> using namespace std;

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:02:35+00:00Added an answer on June 13, 2026 at 10:02 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I converted my huge IOS program to ARC using xcode auto conversion. After that
Data comes from outside of the program as long array has to be converted
An assembly program is converted to binary code and I ran a simple code
'??' gets converted into '^' if I compile mn VC++ program and run it
I am trying to polish up a program a program that I have converted
When a program is compiled it is converted to machine code which can be
I have a java program using objects containing huge arrays of floats. Thing is
I converted a program from IDL into CUDA that performs some calculations on a
My program is a converted python file to exe file. The problem with this
I'm running a C# program (console application, soon to be converted to a Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.