嵌套 For 循环多条件 Mips

Nested For Loop Multiple Conditions Mips

我有一个带有多个条件的嵌套 for 循环,要使用 MIPS 进行转换。我的具体问题是如何从 C 代码中分解这一行以便在 MIPS 转换的 1 个分支语句中完成该指令? --> if((a[i] > 0) && (b[j] > 0))

*我更新了代码以分解 if 条件。在火星上 运行 时出现错误。错误是:

第 28 行:0x00400074 处的运行时异常:获取地址未在字边界 0x10010069

上对齐

C代码在这里:

    int i, j, M, N, count;
    int a[];
    int b[];

    for(i=0; i<M; i++) {
    for(j=0; j<N; j++) {
        if((a[i] > 0) && (b[j] > 0)) {
            count = count + 1;
        }
      }
    }

我这里有:

.data
a: .space 100   # Declare a array with 100 bytes of data 
b: .space 100   # Declare b array with 100 bytes of data
i: .word 0  # Declare i counter
j: .word    # Declare j counter

.text

addi $t0, $zero, 10 # $t0 variable with score 10
addi $s1, $zero, 0 # $s1 variable with array index 0
sw $t0, a($s1) # Store 10 into a array at location 

addi $t1, $zero, 15 # $t0 variable with score 15
addi $s2, $zero, 0 # $s2 variable with array index 0
sw $t1, b($s2) # Store 15 into b array at location 0

lw $t2, i($zero) # Initialize loop index i to 0
lw $t3, j($zero) # Intialize loop index j to 0
addi $s2, $zero, 5 # Initialize M to 5
addi $s3, $zero, 5 # Initialize N to 5
addi $s4, $zero, 0 # Initialize count to 0

OuterLoop:  beq $t2, $s2, Exit # i=M Exit the loop
    lw $t0, a($s1) # Load value from a array
    bgt $t0, 0, InnerLoop # if a[i] > 0

InnerLoop:  beq $t3, $s3, Exit # j=N Exit the loop
    lw $t1, b($s2) # Load value from b array
    bgt $t1, 0, Increment # if b[j] > 0

Increment:  addi $s4, $s4, 1 # Increment count by 1
    addi $t2, $t2, 1 # Increment i by 1
    addi $t3, $t3, 1 # Increment j by 1
    j OuterLoop     

Exit:   

我认为 if((a[i] > 0) && (b[j] > 0)) 不能在一个分支中完成,因为当第一个操作数为真(非零)时,不能计算 && 的第二个操作数。

这是代表 if((a[i] > 0) & (b[j] > 0)) count = count + 1;(未测试)

的 MIPS 代码示例
    # assuming that i = $t2, j = $t3, count = $s4
    sll $t4, $t2, 2 # calculate offset of a[i]
    lw $t5, a($t4) # load a[i]
    sll $t4, $t3, 2 # calculate offset of b[j]
    lw $t6, b($t4) # load b[j]
    slt $t4, $zero, $t5 # $t4 = (a[i] > 0)
    slt $t7, $zero, $t6 # $t7 = (b[j] > 0)
    and $t4, $t4, $t7 # $t4 = (a[i] > 0) & (b[j] > 0)
    beq $t4, $zero, NotTrue # skip increment if the condition is false
    sll $zero, $zero, 0 # nop: prevent increment from being executed when the condition is false
    addi $s4, $s4, 1 # count = count + 1
NotTrue:

更新: 修正其他错误后,您的代码应该是这样的:

.data
a: .space 100   # Declare a array with 100 bytes of data 
bb: .space 100   # Declare b array with 100 bytes of data

.text
main:
    addi $t0, $zero, 10 # $t0 variable with score 10
    addi $s1, $zero, 0 # $s1 variable with array index 0
    sw $t0, a($s1) # Store 10 into a array at location 

    addi $t1, $zero, 15 # $t0 variable with score 15
    addi $s2, $zero, 0 # $s2 variable with array index 0
    sw $t1, bb($s2) # Store 15 into bb array at location 0

    addi $t2, $zero, 0 # Initialize loop index i to 0

    addi $s2, $zero, 5 # Initialize M to 5
    addi $s3, $zero, 5 # Initialize N to 5
    addi $s4, $zero, 0 # Initialize count to 0

OuterLoop:  beq $t2, $s2, Exit # i=M Exit the loop

    addi $t3, $zero, 0 # Intialize loop index j to 0
InnerLoop:  beq $t3, $s3, ExitInnerLoop # j=N Exit the loop

    sll $t4, $t2, 2 # calculate offset of a[i]
    lw $t5, a($t4) # load a[i]
    sll $t4, $t3, 2 # calculate offset of b[j]
    lw $t6, bb($t4) # load b[j]
    slt $t4, $zero, $t5 # $t4 = (a[i] > 0)
    slt $t7, $zero, $t6 # $t7 = (b[j] > 0)
    and $t4, $t4, $t7 # $t4 = (a[i] > 0) & (b[j] > 0)
    beq $t4, $zero, NoIncrement # skip increment if the condition is false
    nop # prevent increment from being executed when the condition is false
    addi $s4, $s4, 1 # Increment count by 1
NoIncrement:

    addi $t3, $t3, 1 # Increment j by 1
    j InnerLoop
    nop
ExitInnerLoop:
    addi $t2, $t2, 1 # Increment i by 1
    j OuterLoop
    nop

Exit:
    # exit program
    addi $v0, $zero, 10
    syscall

我将标签 b 更改为 bb 因为我的 QtSpim 抱怨它使用操作码作为标签。