Question

I'm trying to write a nested loop in Mips Assembly that prints out, for example, if...

I'm trying to write a nested loop in Mips Assembly that prints out, for example, if user input x was 5:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

(spaces in between)

So far my code is :


li $v0, 5   #Ask for integer
syscall
move $t5, $v0

addi $t0, $zero, 0


For1:
   slt $t1, $t0, $t5
   beq $t1, $zero, Exit
   add $s0, $s0, $t0
   addi $t0, $t0, 1
   li $v0, 1
   move $a0, $t0
   syscall
   j For1

Exit:
   li $v0, 10
   syscall

And this currently accepts user input integer and prints out from 1 to n without space (i.e. 12345)

How would I implement and incorporate the second For for the nested loop so I would achieve the desired result?

Homework Answers

Answer #1

Please up vote,comment if any query . Thanks for Question . Be safe .

Note : check attached image for output ,code compiled and tested in MARS MIPS simulator.

Program Plan :

  1. read input $v0 and store in $t5
  2. outer loop run from 1($t0) to $t5(inclusive)
  3. inside outer loop load $t1=1
  4. run inner loop from $t1=1 to $t0(inclusive)
  5. print value of $t1 and space
  6. increment $t1
  7. after loop ends increment $t0 (i) outer loop index
  8. terminate program .

Program :

.data
   prompt : .asciiz "enter a integer(n): "
   space :   .asciiz " "
   newLine: .asciiz "\n"
  
  

.text #code section
   main: #main function
  
       #print prompt string
       la $a0,prompt
       li $v0,4
       syscall
      
       #syscall 5 to read input integer
       li $v0, 5   #Ask for integer
       syscall
      
       addi $t5, $v0,0 #move user input into $t5

       addi $t0, $zero, 1 #outer loop index i=1


       for1: #outer loop
              bgt $t0, $t5, Exit #if t0(x)>t5(x) go to exit label
              li $t1,1 #loop index j=1 inner loop
           for2:   #inner loop
              bgt $t1,$t0,endOfFor2   #if j($t1)>i($t0) go to endOfFor2 loop
             
              #print j
              li $v0,1 #syscall 1 to print integer
              addi $a0,$t1,0 #add $a0=$t1+0
              syscall
             
              #print space
              la $a0,space #load address of space in $a0
              li $v0,4 #syscall 4 to print string
              syscall
             
              #increment j inner loop index
              addi $t1,$t1,1
              j for2 #jump to for2
         
           endOfFor2:#if inner loop ends
                  la $a0,newLine #print new line
                  li $v0,4 #syscall 4
                  syscall
                
                   addi $t0,$t0,1 #increment i outer loop index
                 
               j for1 #jump to for1 label
         
         
         

       Exit:#terminate program
           li $v0, 10 #syscall 10 to terminate program
           syscall

Output :

Please up vote ,comment if any query . Be safe .

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is a homework assignment for Computer Architecture and some question use MIPS Assembly language. 1.    ...
This is a homework assignment for Computer Architecture and some question use MIPS Assembly language. 1.     In the following MIPS assembly code, translate all the instructions to their corresponding machine code in hexadecimal format. This code is stored in the memory from address 0x1fff0000. Loop: sw $t1, 4($s0)        addi $t1, $t1, -1    sll $t1, $t1, 2        bne $t1, $s5, Exit    addi $s0, $s0, 4          j Loop Exit: … 2.     Find the MIPS...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i is saved in the array ascend[], in order. Compile and run the program; it should print 0 4 5. In this exercise, you’ll try to translate the C++ program to MIPS. Some of the more tedious parts are already given in Assignment3.F19.s. You won’t have to write the data allocation sections, some of the initializations, and the output loop at the end. So the...
Without using move or li, write MIPS assembly language using MARS simulator to print a half...
Without using move or li, write MIPS assembly language using MARS simulator to print a half pyramid depending on a value n of a user input. Such that if n = 5 were entered the following would be printed: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT