代码跳过If语句

时间:2016-04-07 11:40:01

标签: mips

这是从Java语言转换而来的代码。

我试图在 function loadFile(file, callback) { var reader = new FileReader(); reader.onload = function() { console.log(file.name); // var output = document.createElement('input'); output.type = 'image'; output.classList.add('image-responsive'); output.classList.add('col-xs-12'); output.name = file.name; // output.id = file.name; // output.src = reader.result; var x = document.getElementById('OrigName'); x.appendChild(output); return callback(output); }; reader.readAsDataURL(file); } 上运行代码,但它跳过if语句,即使条件应该输入if语句。

QtSpim

Java代码:

.text
main: 
    lui  $s0, 0x1001   # S0 = &(A[0])
    addi $s1, $s0, 24  # S1 = &(B[0])
    addi $s2, $s0, 64  # S2 = &(C[0])
    add  $t1, $t1, $0  #  i=0
    addi $t0, $0,  1   # t0 = 1
    sw   $t0, 0($s2)   # c[0] =1

    loop: 
        slti $t2, $t1, 5
        beq  $t2, $0,  exit
        sll  $t3, $t1, 2      # t3= i*4
        add  $t4, $t3, $s1    #  t4=  &(B[i])
        add  $t5, $t3, $s2    # t5 =  &(C[i]
        lw   $s3, 0($t4)      # B[i]
        lw   $s4, 0($t5)      # C[i]
        add  $t7, $t3, $s0    # t7=  &(A[i])
        lw   $s5, 0($t7)      # A[i]

        slt  $t6, $s3, $s4    #  B[i]< C[i]
        beq  $t6, $0,  else
        addi $s4, $s4, -1     #  C[i]-1
        sw   $s4, 0($t7)
        addi $t1, $t1, 1
        j loop

        else: 
        addi $t0, $s3,1       # B[i]+1
        sw   $t0, 0($t7)
        addi $t0, $s3, 3      # B[i]+3
        sw   $t0, 0($t5)
        or   $t0, $s3, $s4    # B[i]OR C[i]
        addi $t0, $t0, -1
        sw   $t0, 0($t4)
        addi $t1, $t1, 1
       j loop

    exit: syscall

.data
A:
B: .word -9, 9, 2, 3, 8
c: .word  1, 8, 1, 2, 3

1 个答案:

答案 0 :(得分:0)

有一些错误。我用&#34; BUG:&#34;注释了您的来源,创建了一个清理和工作版本,打印出阵列,并创建了一个C程序[[IMO]是一个更好的原型语言ASM]

这是您的原始来源,只是注释,没有错误修复[请原谅无偿风格清理]:

# mipsjava -- java conversion
#
# int A[] = new int[5];
# int B[] = new int[5];
# int C[] = new int[5];
#
# C[0] = 1;
# for (int i = 0; i <= 4; i++) {
#   if (B[i] < C[i]) {
#       A[i] = C[i] - 1;
#   }
#   else {
#       A[i] = B[i] + 1;
#       C[i] = B[i] + 3;
#       B[i] = B[i] | C[i] - 1;
#   }
# }

    .text

    .globl  main
main:
    # BUG: wrong way to do handle the array addresses
    lui     $s0,0x1001              # s0 = &(A[0]) BUG: hardwired address
    addi    $s1,$s0,24              # s1 = &(B[0])
    addi    $s2,$s0,64              # s2 = &(C[0])

    add     $t1,$t1,$0              # i = 0 (BUG: this does _not_ set a zero)
    addi    $t0,$0,1                # t0 = 1
    sw      $t0,0($s2)              # c[0] = 1

loop:
    slti    $t2,$t1,5
    beq     $t2,$0,exit

    sll     $t3,$t1,2               # t3 = i*4
    add     $t4,$t3,$s1             # t4 = &(B[i])
    add     $t5,$t3,$s2             # t5 = &(C[i]

    lw      $s3,0($t4)              # B[i]
    lw      $s4,0($t5)              # C[i]
    add     $t7,$t3,$s0             # t7 = &(A[i])

    lw      $s5,0($t7)              # A[i]

    slt     $t6,$s3,$s4             # B[i] < C[i]
    beq     $t6,$0,else

    addi    $s4,$s4,-1              # C[i] - 1
    sw      $s4,0($t7)

    addi    $t1,$t1,1
    j       loop

else:
    addi    $t0,$s3,1               # B[i] + 1
    sw      $t0,0($t7)

    addi    $t0,$s3,3               # B[i] + 3
    sw      $t0,0($t5)

    # BUG: uses _old_ C value and _not_ the one just stored
    or      $t0,$s3,$s4             # B[i] OR C[i]
    addi    $t0,$t0,-1
    sw      $t0,0($t4)

    addi    $t1,$t1,1
    j       loop

exit:
    syscall

    .data

# BUG: the A array has _zero length -- this does _not_ match the java code
A:
B:          .word       -9,9,2,3,8
c:          .word       1,8,1,2,3

此处有更正版本[带打印输出]:

# mipsjava -- java conversion
#
# int A[] = new int[5];
# int B[] = new int[5];
# int C[] = new int[5];
#
# C[0] = 1;
# for (int i = 0; i <= 4; i++) {
#   if (B[i] < C[i]) {
#       A[i] = C[i] - 1;
#   }
#   else {
#       A[i] = B[i] + 1;
#       C[i] = B[i] + 3;
#       B[i] = (B[i] | C[i]) - 1;
#   }
# }

    .text

# main -- main program
#
# registers:
#   s0 -- base address of A array
#   s1 -- base address of B array
#   s2 -- base address of C array
#
#   t1 -- for loop index value ("i")
#
#   t0 -- resuable
#   t2 -- reusable
#   t3 -- reusable
#
#   t4 -- &B[i]
#   t5 -- &C[i]
#   t7 -- &A[i]
#
#   s3 -- B[i] value
#   s4 -- C[i] value
#   s5 -- A[i] value
#
    .globl  main
main:
    la      $s0,A                   # s0 = &A[0]
    la      $s1,B                   # s1 = &B[0]
    la      $s2,C                   # s1 = &C[0]

    li      $t1,0                   # i = 0

    # c[0] = 1
    li      $t0,1                   # get a one
    sw      $t0,0($s2)              # c[0] = 1

loop:
    slti    $t2,$t1,5               # i < 5?
    beq     $t2,$0,done             # no, fly

    sll     $t3,$t1,2               # t3 = i*4

    add     $t7,$s0,$t3             # t7 = &A[i]
    add     $t4,$s1,$t3             # t4 = &B[i]
    add     $t5,$s2,$t3             # t5 = &C[i]

    ###lw       $s5,0($t7)              # A[i]
    lw      $s3,0($t4)              # B[i]
    lw      $s4,0($t5)              # C[i]

    slt     $t6,$s3,$s4             # B[i] < C[i]?
    beq     $t6,$0,else             # no, fly

    subi    $s4,$s4,1               # get C[i] - 1
    sw      $s4,0($t7)              # set A[i]

    addi    $t1,$t1,1               # i += 1
    j       loop

else:
    addi    $t0,$s3,1               # get B[i] + 1
    sw      $t0,0($t7)              # set A[i]

    addi    $t0,$s3,3               # get B[i] + 3
    sw      $t0,0($t5)              # set C[i]

    or      $t0,$t0,$s3             # get B[i] | C[i]
    subi    $t0,$t0,1               # get (B[i] | C[i]) - 1

    sw      $t0,0($t4)              # set B[i]

    addi    $t1,$t1,1               # i += 1
    j       loop

done:
    la      $a0,msg_A
    la      $a1,A
    jal     show

    la      $a0,msg_B
    la      $a1,B
    jal     show

    la      $a0,msg_C
    la      $a1,C
    jal     show

exit:
    li      $v0,10                  # exit syscall
    syscall

# show -- show array
#
# arguments:
#   a0 -- symbol string
#   a1 -- array base
#
# registers:
#   t2 -- iteration count
show:
    li      $v0,4                   # syscall: print string
    syscall

    li      $t2,5                   # get count

showloop:
    li      $v0,4                   # syscall: print string
    la      $a0,msg_space
    syscall

    lw      $a0,0($a1)              # get arr[i]
    addi    $a1,$a1,4               # advance array pointer to next element

    li      $v0,1                   # syscall: print integer
    syscall

    subi    $t2,$t2,1               # count -= 1 -- are we done?
    bgtz    $t2,showloop            # no, loop

    li      $v0,4                   # syscall: print string
    la      $a0,msg_nl
    syscall

    jr      $ra                     # return

    .data

A:          .space      20
B:          .word       -9,9,2,3,8
C:          .word       1,8,1,2,3

msg_A:      .asciiz     "A:"
msg_B:      .asciiz     "B:"
msg_C:      .asciiz     "C:"

msg_space:  .asciiz     " "
msg_nl:     .asciiz     "\n"

这是我用作参考的C程序[带打印输出]:

#include <stdio.h>

int A[5];
int B[5] = {-9,9,2,3,8};
int C[5] = {1,8,1,2,3};

void
show(const char *sym,int *arr)
{

    printf("%s:",sym);

    for (int i = 0; i < 5; i++) {
        printf(" %d",arr[i]);
    }

    printf("\n");
}

int
main(int argc,char **argv)
{
    int lt;

    C[0] = 1;

    for (int i = 0; i < 5; i++) {
        lt = (B[i] < C[i]);
        printf("main: i=%d B=%d C=%d lt=%d\n",i,B[i],C[i],lt);

        if (lt) {
            A[i] = C[i] - 1;
        }

        else {
            A[i] = B[i] + 1;
            C[i] = B[i] + 3;
            B[i] = (B[i] | C[i]) - 1;
        }
    }

    show("A",A);
    show("B",B);
    show("C",C);
}