简体   繁体   中英

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. 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. 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.

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:

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.

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