简体   繁体   中英

printing a right triangle in mips

This is the code I wrote to try to solve this. My code to endl; is not getting looped at all, and it seems to only print two stars for any number entered.

li $t1,1 #start row at 1
li $t2,1 #start col at 1
li $t3,0 #temp=0

outer:
bgt $t1,$t0,done  #row < user input
addi $t1,$t1,1 #row++



inner:
bgt $t2,$t1,done  #col<row
addi $t2,$t2,1 #col++

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

addi $t3,$t3,1 #adds 1 to temp after every print

beq $t2,$t3,outer #if col = temp counter  jump to outer

j inner #restart loop

#code to endl;
la $a0,endl
li $v0,4
syscall

j outer #restart loop

Here is an example of the expected output:

Please enter the edge length of the base of right triangle: 5 
* 
** 
*** 
**** 
***** 

You do not need to use multiple branching statements in a single loop. The reason your code only prints two stars is that you are not resetting the counter of the inner loop to 1. Hence it loops twice and then just exits the loop.

Here is my code and it does exactly what you want:

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

.text
    main:
        li $v0, 4       # print the prompt
        la $a0, prompt
        syscall

        li $v0,5            #take user input
            syscall
    
        move $s0, $v0     # move the input to $s0

        li $t0, 0       # load 0 at t0

    outerLoop:
        beq $t0, $s0, end   #(for i=0;i<=baseLength;i++)
                    #if t0=s0 branch to end
        
        addi $t0, $t0, 1    # increment i

        li $t1, 1       #load 1 at t1

        jal changeLine      #jump to changeLine

    innerLoop:
        bgt $t1, $t0, outerLoop  #(for j=0;j<=i;j++)
                     # if t1=t0 branch to outerLoop

        li $v0, 4       # print star
        la $a0, star
        syscall 
    
        addi $t1, $t1, 1    # increment j

        j innerLoop     # jump to innerLoop

    changeLine: 
        li $v0, 4       # new line
        la $a0, newLine
        syscall 

        jr $ra          # jump to after the call instruction

    end:
        li $v0, 10      # end of program
        syscall

.data prompt: .asciiz "Please enter the edge length of the base of right triangle: " ExitMsg: .asciiz "Exiting the program." star: .asciiz "*" newLine: .byte '\\n' .text ## Your mainline program will ask the user the edge length of the base of right triangle. input:
li $v0, 4 la $a0, prompt syscall

li $v0,5            #take in input
    syscall

    #This will take the edge length of the base of the right triangle as argument $a0.
    move $a0, $v0           #move the input to a0

    # If user enter 0 or a negative number, the program exits.
    bgtz $a0, printTriangle     #branch if greater than zero

    li $v0, 4
la $a0, ExitMsg
syscall

    li $v0,10           #end program
    syscall

    # Otherwise, the program will pass the edge length value in $a0 to the printTriangle procedure, which will print the triangle as described.
    printTriangle:          #Write a procedure called printTriangle. 
    li $t0, 1           #keep track vervicle
    li $t1, 1           #keep track Horizontal
    move $t3, $a0           #INPUT INT as t3

    # It will then print the triangle with stars. 
    loop:
    li $v0, 4
    la $a0, star
    syscall

    beq $t0, $t1, exitLoop          #if counter 0 = counter 1, exitLoop to new line
    #print an *
    addi $t0, $t0, 1            #otherwise incriment t0 location in array
    j loop                  #looping up to beginning

exitLoop:   
    li $v0, 4               #print newline
    la $a0, newLine
    syscall

    beq $t1, $t3, input             #if t1 = INPUT INT as t3, The mainline code will then loop back to ask the user for a new edge length.

    #else
    li $t0, 1               #set t0 back to 0
    addi $t1, $t1, 1            # incriment t1 location in array    
    j loop  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM