MIPS Basi c для цикла - PullRequest
       4

MIPS Basi c для цикла

0 голосов
/ 05 мая 2020

Я пытаюсь реализовать этот 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"

1 Ответ

2 голосов
/ 05 мая 2020

Давайте проследим поток управления:

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

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

Теперь, если мы вложим один for-l oop в другой l oop, нам все равно придется следовать этому шаблону . Вложенность управляющих структур в сборку такая же, вы полностью вкладываете одну управляющую структуру в другую. Не смешивайте отдельные части из вложенных конструкций и внешних конструкций. Полностью запуск и завершение sh внутренней структуры управления перед возобновлением внешней.

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