Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write this in MIPS Write a program that will print a triangle to the cons

ID: 3707673 • Letter: P

Question

Please write this in MIPS

Write a program that will print a triangle to the console. .Write a procedure called printTriangle. This will take the edge length of the base of the right triangle as argument Sa0. It will then print the triangle with stars. For instance, if five is given by the user as an input, the program will print stars starting with one star up to five stars to form Your mainline program will ask the user the edge length of the base of right triangle. If user enter 0 or a negative number, the program exits. Otherwise, the program will pass the edge length value in $a0 to the printTriangle procedure, which will print the triangle as described. The mainline code will then loop back to ask the user for a new edge length. The example execution and the shape of the triangle is given below. Note: You need to submit your source code on blackboard. Your source needs to have comments that explain your implementation. You need to include the following set of #Your Name and ld #Assignment # (Example: Homework #7) Example Execution and the Output: Please enter the edge length of the base of right triangle: 5 Please enter the edge length of the base of right triangle:8 Please enter the edge length of the base of right triangle:0 Exiting the program.

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


.data
prompt: .asciiz "Please enter the edge length of the base of right triangle: "
star: .asciiz "*"
newline: .asciiz " "

.text

input:
#prompt and read int
li $v0, 4
la $a0, prompt
syscall


#read int and store in $t0
li $v0, 5
syscall
move $t0, $v0

ble $t0, 0, exit

#pass arguments and call printTriangle
move $a0, $t0
jal printTriangle
b input
exit:
#exit
li $v0, 10
syscall


#----------------------

printTriangle:
#save the value of $s0 and copy a0 into s0

sub $sp, $sp, 4
sw $s0, ($sp)
move $s0, $a0

li $t1, 1 #line counter
loop1:
bgt $t1, $s0, end_loop1 #for each line

li $t2, 1 #star counter
loop2:
bgt $t2, $t1, end_loop2 #for each star on current line

#print star
li $v0, 4
la $a0, star
syscall

#increment star counter for current line
add $t2, $t2, 1
b loop2

end_loop2:
#print new line
li $v0, 4
la $a0, newline
syscall
#increment line counter
add $t1, $t1, 1
b loop1
end_loop1:

#restore s0 from stack
lw $s0, ($sp)
add $sp, $sp, 4
jr $ra


output
-------
Please enter the edge length of the base of right triangle: 5
*
**
***
****
*****
Please enter the edge length of the base of right triangle: 8
*
**
***
****
*****
******
*******
********
Please enter the edge length of the base of right triangle: 0

-- program is finished running --