首页 > 解决方案 > 删除前导和尾随空白 mips?

问题描述

所以我想出了这个程序,它基本上编码了一个数字模式,并且数字必须在彼此之间加上标签,例如:

1 1 1

但最后一个“1”也有一个标签,我需要删除它。这就是我的代码用于制表符的样子:我在我的 for 循环结束之前使用它,所以它可以增加多少次。我真的不知道从哪里开始创建一个不使用选项卡打印最后一个数字的条件

li $v0, 11      #this is for tabbing the numbers 
        li $a0, 9   
        syscall

标签: mips

解决方案


您没有提供足够的代码来给出完整的答案,但是有几种方法可以省略打印最后一个选项卡:

如果您知道您正在处理最后一项,您可以跳过打印选项卡代码,例如,假设您在一个 while 循环中,您循环的 while$t0与 不同$t1,那么您可以编写:

while_loop:
   # .... do something
   beq $t0, $t1, skip
   # your code to print tab
   li $v0, 11      #this is for tabbing the numbers 
   li $a0, 9   
   syscall
skip:
   # ... something else
   bne $t0, $t1 while_loop  % this is the condition to keep in the loop

如果选项卡的打印是您在循环中做的最后一件事,那么您可以简化一下:

while_loop:
   # .... do something
   beq $t0, $t1, while_loop
   # your code to print tab
   li $v0, 11      #this is for tabbing the numbers 
   li $a0, 9   
   syscall
   b  while_loop  

另一种方法是在循环开始时打印选项卡,为第一次迭代保存。如果您正在迭代寄存器上的某些值并且知道某些初始值不会被重复,这很有用。在这个例子中,我将只使用一个所谓的备用寄存器:

li $t7, 0  # $t7 will only have 0 on the first iteration of the loop
while_loop:
  beq $t7, $zero, skip
  # your code to print tab
  li $v0, 11      #this is for tabbing the numbers 
  li $a0, 9   
  syscall
skip:
  li $t7, 1
% your remaining code here, which at some point goes to the while_loop

推荐阅读