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...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If ( n is even) { n = n / 2; } else { n = 3 * n + 1; } In this case, n is to be input by the user (assume they input a non-negative value), the conditional is performed, and the resulting n is to be output. Again, use the system calls for input, output, and exiting the...
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
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Finally, write a script named word_counts.sh that prints out how many words are on each line...
Finally, write a script named word_counts.sh that prints out how many words are on each line of standard input, as well as the total number of words at the end. The script should take no arguments. Instead, the script must work by reading every line of standard input (using a while loop) and counting the number of words on each line separately. The script is intended to work with standard input coming from a pipe, which will most often come...
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...