简体   繁体   English

如何为汇编MIPS指令使用for循环?

[英]How can I use a for loop for an Assembly MIPS instruction?

So I have been busy with assembly lately, and I am stuck with using a for loop. 因此,我最近一直在忙于汇编,而我一直坚持使用for循环。 I have searched through the internet, but I don't understand any of it. 我已经在互联网上进行搜索,但我一无所知。 I have to loop SLL $1,$1,4 # sll operation for X amount of times. 我必须循环SLL $1,$1,4 # sll operation X次。 How can I do this? 我怎样才能做到这一点?

#generated assembly code for SIMPL
@include "Mips.wasm"
.data   MyRegisters:    REGISTERS
0:  WORD    zero    0      
1:  WORD    temp    0      
2:  WORD     0      
3:  WORD     0      
4:  WORD     0      
5:  WORD     0      
6:  WORD     0      
7:  WORD     0      
8:  WORD     0      
9:  WORD     0      
10: WORD     0      
29: WORD    sp      0      
31: WORD    ra     
.data   MyMemory:   DATAMEM
50: WORD stack  # start of stack
0:  WORD    a       0      
1:  WORD    b       0      
2:  WORD    result  0      
.code   MyCode: MIPS,MyMemory
    J       INIT   
L1: # main
    LUI     $1     , 1          # storing numeric into reg
    ORI     $1     , $1     , 0   
    SW      $1     , a      , $0        # assignment of var
    # expr;
    LUI     $1     , 0          # storing numeric into reg
    ORI     $1     , $1     , 1000      
    SW      $1     , b      , $0        # assignment of var
    # expr;
    LW      $1     , b      , $0        # storing var into reg
    LW      $2     , b      , $0        # storing var into reg
    SLL     $1     , $1     , 4         # sll operation
    SW      $1     , result , $0        # assignment of var
    # expr;
    LUI     $1     , 0          # storing numeric into reg
    ORI     $1     , $1     , 1      
    #return int value   # stack ret value
    JR      $31     # return
INIT:   # Start of our program
    J       L1      # jump to the main code
END:

There are no loops in assembler, that's a C (or similar language) concept. 汇编器中没有循环,这是C(或类似语言)的概念。

Instead, there are conditional branch instructions used to jump based on a test. 相反,有条件分支指令用于根据测试进行跳转。

 li    $t0, 10
 li    $t1, 0
 loop:
 addi  $t1, $t1, 1
 bne   $t1, $t0, loop

This would roughly correspond to a C "for" of: 这将大致对应于C的“ for”:

for( int n = 0; n < 10; ++n ) {}

Where other code MIPS code doing actual work (that which would appear in the brackets in C) would be situated between the loop: label and the addi instruction. 其他执行实际工作的代码MIPS代码(将出现在C括号中的代码)将位于loop:标签和addi指令之间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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