[MIPS Basic For Loop

问题描述 投票:0回答:1

im试图将这个Java代码实现为MIPS汇编语言,我很困惑。这是我到目前为止所拥有的:

java代码:

          for (int c = 1; c <= rows; c++) {
              number = highestValue; // reset number to the highest value from previous line

              for (i = 0; i < c; i++) {
                  System.out.print(++number + " ");
              }

              highestValue = number; // setting the highest value in line

汇编代码:

.text    # tells the program where the code begins

    move $t0, $zero    # t0=0  
    move $t1, $zero    # this will be similar to "int i = 0"    
    move $t2, $zero    # this will be similar to "number = 0"  
    move $t3, $zero    # this will be similar to "highestValue = 0"
    add $t4, $zero, 1  # this is our c
    move $t5, $a1      # this is what will have our user input thru command line (rows!)

    # biggest for-loop for(int c = 1; c <= rows; c++)
    for:
    bgt $t4, $a1, exit
    move $t2, $t3

        for1:   # for(i = 0; i < c; i++)
        bgt $t1, $t4, exit1    # if c is greater than i 

        li $v0, 5 
        la $t2, printNum
        syscall 

        add $t1, $t1, $1         # increment by 1

        j for1

        exit1:

        move $t2, $t3

.data   # tells to store under .data line (similar to declaring ints)
    printNum: .ascii z "$t2"
assembly mips
1个回答
0
投票

让我们遵循控制流程:

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

int i = 0;
while ( i < n ) {
    body
    i++;
}

现在,如果我们将一个for循环嵌套在另一个循环中,则仍然必须遵循这种模式。装配体中控制结构的嵌套是相同的,您可以将一个控制结构完全嵌套在另一个中。不要混合嵌套结构中的零件。在恢复外部控制之前,完全开始完成内部控制结构。

for ( int i = 0; i < n; i++ ) {
    for ( j = 0; j < m; j++ ) {
       body
    }
}

int i = 0;
while ( i < n ) {
    int j = 0;   <----- this must be here; we cannot hoist this outside of the outer loop
    while ( j < m ) {
        body
        j++;
    }
    i++;    <--------- your missing this
} <---------- and this
© www.soinside.com 2019 - 2024. All rights reserved.