比较数组元素。切换语句/ if else - 汇编 - MIPS

时间:2011-09-14 05:45:05

标签: assembly mips

这是我的第一个项目,抱歉我缺乏知识,这是我试图复制的代码。

   int A[3] = {0, 1, 2}; 
    int x; 
    for ( int i = 0; i < 3; i++ ) 
    { 
         if ( A[i] > 1 )
         x = 1; 
         else 
         { 
                switch ( A[i] )  
                { case 0: 
                       x = 2; 
                  case 1: 
                       x = 3; 
                }  
         } 
         printf(“%d”, x); 
}

这是我在汇编时的代码。

   main:    li $s0, 0 
            sw $s0, ($s7) #int A[0] = {0};  
            li $s0, 1 
            sw $s0, 4($s7) #int A[1] = {1};  
            li $s0, 2 
            sw $s0, 8($s7) #int A[2] = {2}; 
            li $s1, 1  #initialize x = 1
            li $s0, 0 #initialize i = 0
            li $s2, 3 # constant 3
            li $s3, 1 # constant 1


    start:  blt $s0, $s2, for   
            j finish

    for:    add $t1,$s0,$s0 #reg $t1 = 2*i
            add $t1,$t1,$t1 #reg $t1 = 4*i
            add $t0,$t1,$s7  

    bgt $t0, $s3, exone  #a[i] is greater than 1 how to increment the array up?
    #switch statement
    lw  $t0, 0($t1)
    jr  $t0
    j  start

        L0: add $s0, $s0, 1
            li $s1, 2
            j  print

        L1: add $s0, $s0, 1
            li $s1, 3
            j  print


        exone:  add $s0, $s0, 1
            li $s1, 1
            j  print


        print:  li $v0, 1 # print integer 
             move $a0, $s1 # what to print is stored at $s1
             syscall
             j  start



         finish:  li $v0, 10 # exit system call 
               syscall 

我不确定我在哪里出错它编译但是没有给我输出我想要的输出或任何输出。

根据一些信息,我更新了我的代码。

    main:   add $s0,$zero, $zero
         li $s7, 200
         sw $s0, 0($s7) #int A[0] = {0};  
         addi $s0, $s0, 1
         sw $s0, 4($s7) #int A[1] = {1};  
         addi $s0, $s0, 1
         sw $s0, 8($s7) #int A[2] = {2}; 
         li $s1, 0  #initialize x = 0
         li $s0, 0 #initialize i = 0
         li $s2, 3 # constant 3
         li $s3, 1 # constant 1

    #check to enter the for loop
    for:    blt  $s0, $s2, enter   
                j finish
    #enter the for loop
    enter:    add $t1,$s0,$s0 #reg $t1 = 2*i
          add $t1,$t1,$t1 #reg $t1 = 4*i
          add $t0,$t1,$s7 #reg A[i]
          lw  $t2, 0($t0)   
          bgt $t2, $s3, exone  #a[i] is greater than 1 check
          #switch statement
          jr  $t2
          #just in case jump back to for loop
          j  for

          #address for the switch statements
      L0:     add $s0, $s0, 1
              li $s1, 2
          j  print

      L1:     add $s0, $s0, 1
          li $s1, 3
          j  print
         #address for the if else statement
      exone:  add $s0, $s0, 1
          li $s1, 1
          j  print


     print:  li $v0, 1 # print integer 
         move $a0, $s1 # what to print is stored at $s1
         syscall
         j  for



     finish:  li $v0, 10 # exit system call 
          syscall 

输出应为“231”。

3 个答案:

答案 0 :(得分:1)

      # [snip]
      add $t0,$t1,$s7 #reg A[i]
      lw  $t2, 0($t0)   
      bgt $t2, $s3, exone  #a[i] is greater than 1 check
      #switch statement
      jr  $t2

这是错误的jr表示跳转寄存器,它跳转到$t2中包含的地址(此时包含a[i],此处为0,1或2 - 不跳转到的好地址。

你可以做很多事来纠正这个问题(我假设你的意思是你的C代码中有break个语句),但是这里有一些快速未经测试的代码,用一组if-模仿交换机陈述:

li $t3, 0
beq $t2, $t3, L0 # if (a[i] == 0) goto L0
li $t3, 1
beq $t2, $t3, L1 # if (a[i] == 1) goto L1
j print # else fall through

答案 1 :(得分:0)

泵房: 首先要确定你想做什么,你的程序没有任何正确的目标 虽然你应该注意一些点,

  • switch-case应该具有“default”,否则它可能会创建一个大的 问题。
  • 变量应始终在程序

  • 的顶部声明
  • 不在循环中。相同类型的变量应该一起声明。

答案 2 :(得分:0)

.data
 array:  .word   0 : 3 
.text
.globl main 


main:    la $s7, array  
         add $s0, $zero, $zero
         sw $s0, 0($s7) #int A[0] = {0};  
         addi $s0, $s0, 1
         sw $s0, 4($s7) #int A[1] = {1};  
         addi $s0, $s0, 1
         sw $s0, 8($s7) #int A[2] = {2}; 
         add  $s1, $zero, $zero  #initialize x = 0
         add $s0, $zero, $zero #initialize i = 0
         li $s2, 3 # constant 3
         li $s3, 1 # constant 1

 #check to enter the for loop
 for:   blt  $s0, $s2, enter   
        j finish
 #enter the for loop
 enter: add $t1,$s0,$s0 #reg $t1 = 2*i
        add $t1,$t1,$t1 #reg $t1 = 4*i
        add $t0,$t1,$s7 #reg A[i]
        lw  $t2, 0($t0) 
        bgt $t2, $s3, exone  #a[i] is greater than 1 check
 #switch statement I would like to know how to properly do this with a jr
        beq $t2, $zero, L0
        beq $t2, $s3, L1
 #just in case jump back to for loop
        j  for

 #address for the switch statements
  L0:   add $s0, $s0, 1
        li $s1, 2
         j  print

  L1:   add $s0, $s0, 1
        li $s1, 3
        j  print

  #address for the if else statement
   exone:    add $s0, $s0, 1
         li $s1, 1
         j  print


 print:      li $v0, 1 # print integer 
         move $a0, $s1 # what to print is stored at $s1
         syscall
         j  for



 finish:  li $v0, 10 # exit system call 
      syscall 

所以这个工作并不完全是我想要的,但是它的工作原理我想在switch语句中使用一个jr,但我还没有。