将程序的一部分重构为循环以使其更小

Refactor part of a program to a loop to make it smaller

我写了一些代码,但是太长了。我想也许我可以通过将这部分放入循环中来使我的程序更小一些。我尝试将 0 更改为某些寄存器,例如 $f9 并递增它,但它没有用。

有人知道我该怎么做吗?

windfact:   .float 1.201, 1.036, 2.320, 5.026, 6.321, 1.0215
newLine:    .asciiz "\n"

la  $t0, windfact           # put address of list into $a1
l.s $f12, 0($t0) 
li  $v0, 2           
syscall                     # This will print 1.192173
li       $v0, 4                 # system call code for print string
la       $a0, newLine           # load addr of newLine in $a0
syscall

l.s $f12, 4($t0) 
li  $v0, 2           
syscall                     
li       $v0, 4                 
la       $a0, newLine           
syscall

l.s $f12, 8($t0) 
li  $v0, 2           
syscall                     
li       $v0, 4                 
la       $a0, newLine           
syscall

l.s $f12, 12($t0) 
li  $v0, 2           
syscall                     
li       $v0, 4                 
la       $a0, newLine           
syscall

l.s $f12, 16($t0) 
li  $v0, 2           
syscall                     
li       $v0, 4                 
la       $a0, newLine           
syscall

l.s $f12, 20($t0) 
li  $v0, 2           
syscall                     
li       $v0, 4                 
la       $a0, newLine           
syscall                                

谢谢

看起来您只需要遍历数组,将每个连续的值加载到 $f12。一种方法是将指针递增到数组中,而不是递增索引。

在 C 中,就像做

float *array_end = array+size;   // keep this in a register to compare against
for(float *p = array ; p < array_end ; p++ )
    do_stuff(*p);

相对于

for(int i=0 ; i < size ; i++ )
    do_stuff(array[i]);

在您使用的 ABI 中,syscall 指令是否保留其输入寄存器?如果是这样,您只需设置 $f12$a0 一次,无论您是否完全展开(像现在一样)。

否则,您将不得不将循环 pointer/counter 溢出到系统调用的内存中。