无法正确获得三角形图案

时间:2019-07-13 09:03:33

标签: assembly mips qtspim

我不确定我在哪里做错三角形图案。 我正在尝试使三角形图案的数字增加,其间具有空格,以使其看起来像直角三角形,但是使用此代码,它只能连续打印数字。

.data
input:    .asciiz "Enter the length of your triangle: "
pattern:  .asciiz "your desired PATTERN :) "
gap:     .asciiz "   \n"
again_str:  .asciiz "wanna try again? (y/n)"
Invalid: .asciiz "you have entered invalid input"
exit: .asciiz "\n EXIT"

.text
.globl main
.ent main
main:
li $v0,4
la $a0,input
syscall             #input message printed

li $v0,5
syscall              #input taken 

move $t0,$v0         #input of length of the pattern saved here

#saving values in registers for further calculations
addi $t1,$0,2        #saving value 2 for calculation purpose
addi $t2,$0,-2       #saving value -2 for calculation purpose
addi $t3,$0,1        #saving value 1 for calculation purpose 

#moving saved values from temporary registers to saved registers
move $s5,$t0      #input moved to $s5
move $s6,$t1      #value 2
move $s7,$t2      #value -2
move $s8,$t3      # value 1


jal patt


.globl patt
.ent patt
patt:

mult $s5,$s6
mflo $s0
add $s1,$s0,$s7      #variable K saved here which is to be used to print 
the gaps between numbers

#trying 
li $t4,0            #set i=0
li $t5,0        #set j=0
li $t6,0        #set k=0            

For1:


slt $s2,$0,$s5       #test for n>0
beq $s2,$0,For3
addi $s5,$s5, -1      #makes a loop by decrementing the value by step 
size of 1

For2:

slt $s3,$0,$s1            #check if k has reached to zero
beq $s3,$0,PRINT_GAP
li $v0,4
la $a0,gap
syscall     
addi $s1,$s1,-1

For3:             #number printing occurs here
li $v0,1
move $a0,$s5
syscall              #numbers are printed here

slt $s4,$0,$a0           # check if range is exceeded
beq $s4,$0,EXIT    #if given input has reached to limit zero then go to 
again
addi $s5,$s5,-1
j For2

PRINT_GAP:
li $v0,4             #gaps between numbers are printed
la $a0,gap
syscall

j For3


EXIT:
li $v0,4
la $a0,exit
syscall
li $v0,10
syscall

没有错误,但没有显示如下所示的预期输出。

5 5 5 5 5
 4 4 4 4
  3 3 3
   2 2
    1

0 个答案:

没有答案