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 6571629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:54:26+00:00 2026-05-25T14:54:26+00:00

This is my first project sorry for my lack of knowledge, this is the

  • 0

This is my first project sorry for my lack of knowledge, this is the code I’m trying to replicate.

   int A[3] = {0, 1, 2}; 
    int x; 
    for ( int i = 0; i < 3; i++ ) 
    { 
         if ( A[i] > 1 )
         x = 1; 
         else 
         { 
                switch ( A[i] )  
                { case 0: 
                       x = 2; 
                  case 1: 
                       x = 3; 
                }  
         } 
         printf(“%d”, x); 
}

This is the code that I have in assembly.

   main:    li $s0, 0 
            sw $s0, ($s7) #int A[0] = {0};  
            li $s0, 1 
            sw $s0, 4($s7) #int A[1] = {1};  
            li $s0, 2 
            sw $s0, 8($s7) #int A[2] = {2}; 
            li $s1, 1  #initialize x = 1
            li $s0, 0 #initialize i = 0
            li $s2, 3 # constant 3
            li $s3, 1 # constant 1


    start:  blt $s0, $s2, for   
            j finish

    for:    add $t1,$s0,$s0 #reg $t1 = 2*i
            add $t1,$t1,$t1 #reg $t1 = 4*i
            add $t0,$t1,$s7  

    bgt $t0, $s3, exone  #a[i] is greater than 1 how to increment the array up?
    #switch statement
    lw  $t0, 0($t1)
    jr  $t0
    j  start

        L0: add $s0, $s0, 1
            li $s1, 2
            j  print

        L1: add $s0, $s0, 1
            li $s1, 3
            j  print


        exone:  add $s0, $s0, 1
            li $s1, 1
            j  print


        print:  li $v0, 1 # print integer 
             move $a0, $s1 # what to print is stored at $s1
             syscall
             j  start



         finish:  li $v0, 10 # exit system call 
               syscall 

I’m not sure where I’m going wrong it compiles but doesn’t give me the output I want or any output for that matter.

Based on some info I have updated my code.

    main:   add $s0,$zero, $zero
         li $s7, 200
         sw $s0, 0($s7) #int A[0] = {0};  
         addi $s0, $s0, 1
         sw $s0, 4($s7) #int A[1] = {1};  
         addi $s0, $s0, 1
         sw $s0, 8($s7) #int A[2] = {2}; 
         li $s1, 0  #initialize x = 0
         li $s0, 0 #initialize i = 0
         li $s2, 3 # constant 3
         li $s3, 1 # constant 1

    #check to enter the for loop
    for:    blt  $s0, $s2, enter   
                j finish
    #enter the for loop
    enter:    add $t1,$s0,$s0 #reg $t1 = 2*i
          add $t1,$t1,$t1 #reg $t1 = 4*i
          add $t0,$t1,$s7 #reg A[i]
          lw  $t2, 0($t0)   
          bgt $t2, $s3, exone  #a[i] is greater than 1 check
          #switch statement
          jr  $t2
          #just in case jump back to for loop
          j  for

          #address for the switch statements
      L0:     add $s0, $s0, 1
              li $s1, 2
          j  print

      L1:     add $s0, $s0, 1
          li $s1, 3
          j  print
         #address for the if else statement
      exone:  add $s0, $s0, 1
          li $s1, 1
          j  print


     print:  li $v0, 1 # print integer 
         move $a0, $s1 # what to print is stored at $s1
         syscall
         j  for



     finish:  li $v0, 10 # exit system call 
          syscall 

The output should be “231”.

  • 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-05-25T14:54:26+00:00Added an answer on May 25, 2026 at 2:54 pm
          # [snip]
          add $t0,$t1,$s7 #reg A[i]
          lw  $t2, 0($t0)   
          bgt $t2, $s3, exone  #a[i] is greater than 1 check
          #switch statement
          jr  $t2
    

    This is wrong jr means Jump Register and it jumps to the address contained in $t2 ( which at this point contains a[i], which here is 0, 1 or 2 — not great addresses to jump to).

    You could do a lot of things to correct this (I’ll assume you meant to have break statements in your C code), but here’s some quick untested code that mimics the switch with a set of if-statements:

    li $t3, 0
    beq $t2, $t3, L0 # if (a[i] == 0) goto L0
    li $t3, 1
    beq $t2, $t3, L1 # if (a[i] == 1) goto L1
    j print # else fall through
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first android project so please help I am trying to create
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>
Hi this is my first time trying to push a project up to Heroku
This is my first time that I am working on a big project for
sorry if this is a dumb Q, this is my first Rails3 project... For
For my first Django project I'm trying to make an app that lets users
this is my first python project. I am having issues setting up a project
This is my first azure project and I'm not sure if I'm doing something
This is my first applet project and for some reason when I try to
This was my first Sinatra project - link shortener but I am stuck with

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.