在汇编中用跳转表编写嵌套循环

时间:2014-02-26 03:07:17

标签: java c loops assembly mips

我有一些代码需要翻译成Assembly。我是大会新手,我感到很困惑,我觉得很难写。我已经阅读了一些教程,但在编译代码时仍然遇到问题。我需要一些帮助 - 这是原始代码和我在Assembly中的代码。我确定如何解决这些问题:我在汇编代码中给出了一些解释我想要做的事情 - 我需要知道我是否已经离开了

原始代码:

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

我的汇编代码

.data 
jumpTab: .word lab0, lab1, lab2, lab3 #Jumptable
nums: .word 0,1,2,3 #array declaration of integers
numberx: .ascii "X equals: "

.text
 main:

 addi $t0, $0, 0 #Declare I
 addi $t1, $0, 0 #Declare X
 ori  $t2, $t0, 4 # Constant Value 4
 ori  $t4, $t0, 1   # Constant Value 1
la $t3, nums    #initialize Array Nums


forloop: bgt $t0, $t2 end  #branch if i > four to end:
 add $t0, $t0, $t0 Loop:  #add i++ and go to loop:

loop: bgt  $t3, $t4 Else  #if Array Index (i) is more than 1 constant 1 branch 
   add $t1, $t1, 1

Else: 
la $t5, JumpTable
sll $t5, $t6, 2 #offset
jr $t7

lab0:
   la $t1, 2   #load address set x=2
   j Print
lab1:
   la $t1, 3     #load address set x=3
   j Print
lab2:
    la $t1, 5   #load address set x=5
    j Print
lab3:
    la $t1, 7   #load address set x=7
    j Print

 print: #print out X = 

 li $v0, 4
   syscall


    end:   

0 个答案:

没有答案