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

发布于 2024-12-04 14:28:21 字数 3023 浏览 0 评论 0原文

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

   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”。

This is my first project sorry for my lack of knowledge, this is the code I'm trying to replicate.

   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); 
}

This is the code that I have in assembly.

   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 

I'm not sure where I'm going wrong it compiles but doesn't give me the output I want or any output for that matter.

Based on some info I have updated my code.

    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 

The output should be “231”.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦情居士 2024-12-11 14:28:21
      # [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
      # [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

This is wrong jr means Jump Register and it jumps to the address contained in $t2 ( which at this point contains a[i], which here is 0, 1 or 2 -- not great addresses to jump to).

You could do a lot of things to correct this (I'll assume you meant to have break statements in your C code), but here's some quick untested code that mimics the switch with a set of if-statements:

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
私野 2024-12-11 14:28:21

泵房:
首先确定你想做什么,你的程序没有任何正确的目标。
尽管您应该注意一些要点,例如

  • switch-case 必须具有“默认”,否则可能会产生很大的影响
    问题。
  • 变量应始终在程序顶部声明

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

Pumphouse:
First be sure what you want to do,your program does not have any proper aim.
All though you should have to note some points like,

  • switch-case should must have "default" otherwise it may create a big
    problem.
  • Variables should be declared always on the top of program

  • not in the loop. variables of same type should be declare together.

囚我心虐我身 2024-12-11 14:28:21
.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 但我还没有完全做到这一点。

.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 

So this works it wasn't exactly how I wanted to do it but it works I wanted to use a jr in the switch statement but I'm not quite there yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文