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
Using MIPS Assembly language, sort 3 integers from greatest to smallest. Do this without using an...
Using MIPS Assembly language, sort 3 integers from greatest to smallest. Do this without using an array and instead use conditional branches, using the algorithm below: 1. Get Numbers From User 2.IF(#1 < #2) Swap the two numbers 3.IF(#2 < #3) Swap the two numbers 4.IF(#1 < #3) Swap the two numbers 5. Display Numbers in from greatest to smallest 6. End Program I have not been able to figure out the transfer of control when using branches and am...
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction...
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction and write the decimal values of each instruction field. addi $v0, $zero, 0 loop: lw $v1, 0($a0) sw $v1, 0($a1) sll $a0, $a0, 2 add $a1, $a1, $a0 beq $v1, $zero, loop 1b. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds...
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
problem 1 Write a program that asks the user for an integer and then prints out...
problem 1 Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop. in c plus plus
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a string, get a string from the user, open file, close file, read file, and allocate heap memory. You can use more macros than these if you like. Main Program File: Allocate 1024 bytes of dynamic memory and save the pointer to the area. The main program is a loop in which you ask the user for a filename. If they enter nothing for the...
Write the corresponding MIPS I instruction to the C++ code segment below. for (; i <...
Write the corresponding MIPS I instruction to the C++ code segment below. for (; i < 100; i ++) s = s + (2*i + 1); Use the table below for variable-register association Variable i s Register $t0 $t1 Variables s has been initialized with 0; i has been initialized with 1. No need to write instruction for their initializations. The instructions corresponding to the for-loop is labeled as "LOOP". The instructions following the for-loop is labeled by "DONE" (no...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT