MIPS Basic For循环 [英] MIPS Basic For Loop
问题描述
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"
让我们遵循控制流程:
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
im trying to implement this java code into MIPS assembly language and i am quite confused. this is what i have so far:
java code:
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
assembly code:
.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"
Let's follow the flow of control:
for ( int i = 0; i < n; i++ ) { body }
int i = 0;
while ( i < n ) {
body
i++;
}
Now if we nest one for-loop inside another loop, we still have to follow this pattern. Nesting of control structures in assembly is the same, you fully nest one control structure inside the other. Do not intermix piece parts from nested constructs and outer constructs. Fully start and finish the inner control structure before resuming the outer one.
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
这篇关于MIPS Basic For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!