嵌套的For循环多条件MIP [英] Nested For Loop Multiple Conditions Mips

查看:28
本文介绍了嵌套的For循环多条件MIP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的带有多个条件的for循环要使用MIPS进行转换。我的具体问题是,为了在MIPS转换的1个分支语句中完成该指令,我如何将此行从C代码中拆分出来?-->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抱怨它使用操作码作为标签。

这篇关于嵌套的For循环多条件MIP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆